Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / swing / treeTable / MergeSort.java @ 40561

History | View | Annotate | Download (3.86 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gui.beans.swing.treeTable;
25
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
26
 *
27
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
28
 *
29
 * This program is free software; you can redistribute it and/or
30
 * modify it under the terms of the GNU General Public License
31
 * as published by the Free Software Foundation; either version 2
32
 * of the License, or (at your option) any later version.
33
 *
34
 * This program is distributed in the hope that it will be useful,
35
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
 * GNU General Public License for more details.
38
 *
39
 * You should have received a copy of the GNU General Public License
40
 * along with this program; if not, write to the Free Software
41
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
42
 *
43
 * For more information, contact:
44
 *
45
 *  Generalitat Valenciana
46
 *   Conselleria d'Infraestructures i Transport
47
 *   Av. Blasco Ib??ez, 50
48
 *   46010 VALENCIA
49
 *   SPAIN
50
 *
51
 *      +34 963862235
52
 *   gvsig@gva.es
53
 *      www.gvsig.gva.es
54
 *
55
 *    or
56
 *
57
 *   IVER T.I. S.A
58
 *   Salamanca 50
59
 *   46005 Valencia
60
 *   Spain
61
 *
62
 *   +34 963163400
63
 *   dac@iver.es
64
 */
65
/* CVS MESSAGES:
66
 *
67
 * $Id: MergeSort.java 13136 2007-08-20 08:38:34Z evercher $
68
 * $Log$
69
 * Revision 1.1  2007-08-20 08:34:46  evercher
70
 * He fusionado LibUI con LibUIComponents
71
 *
72
 * Revision 1.1  2006/10/23 08:18:39  jorpiell
73
 * A?adida la clase treetable
74
 *
75
 *
76
 */
77
/**
78
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
79
 */
80
public abstract class MergeSort extends Object {
81
    protected Object           toSort[];
82
    protected Object           swapSpace[];
83

    
84
    public void sort(Object array[]) {
85
        if(array != null && array.length > 1)
86
        {
87
            int             maxLength;
88
  
89
            maxLength = array.length;
90
            swapSpace = new Object[maxLength];
91
            toSort = array;
92
            this.mergeSort(0, maxLength - 1);
93
            swapSpace = null;
94
            toSort = null;
95
        }
96
    }
97

    
98
    public abstract int compareElementsAt(int beginLoc, int endLoc);
99

    
100
    protected void mergeSort(int begin, int end) {
101
        if(begin != end)
102
        {
103
            int           mid;
104

    
105
            mid = (begin + end) / 2;
106
            this.mergeSort(begin, mid);
107
            this.mergeSort(mid + 1, end);
108
            this.merge(begin, mid, end);
109
        }
110
    }
111

    
112
    protected void merge(int begin, int middle, int end) {
113
        int           firstHalf, secondHalf, count;
114

    
115
        firstHalf = count = begin;
116
        secondHalf = middle + 1;
117
        while((firstHalf <= middle) && (secondHalf <= end))
118
        {
119
            if(this.compareElementsAt(secondHalf, firstHalf) < 0)
120
                swapSpace[count++] = toSort[secondHalf++];
121
            else
122
                swapSpace[count++] = toSort[firstHalf++];
123
        }
124
        if(firstHalf <= middle)
125
        {
126
            while(firstHalf <= middle)
127
                swapSpace[count++] = toSort[firstHalf++];
128
        }
129
        else
130
        {
131
            while(secondHalf <= end)
132
                swapSpace[count++] = toSort[secondHalf++];
133
        }
134
        for(count = begin;count <= end;count++)
135
            toSort[count] = swapSpace[count];
136
    }
137
}
138