Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / treeTable / MergeSort.java @ 13136

History | View | Annotate | Download (2.87 KB)

1
package org.gvsig.gui.beans.swing.treeTable;
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
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 2
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42
/* CVS MESSAGES:
43
 *
44
 * $Id: MergeSort.java 13136 2007-08-20 08:38:34Z evercher $
45
 * $Log$
46
 * Revision 1.1  2007-08-20 08:34:46  evercher
47
 * He fusionado LibUI con LibUIComponents
48
 *
49
 * Revision 1.1  2006/10/23 08:18:39  jorpiell
50
 * A?adida la clase treetable
51
 *
52
 *
53
 */
54
/**
55
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
56
 */
57
public abstract class MergeSort extends Object {
58
    protected Object           toSort[];
59
    protected Object           swapSpace[];
60

    
61
    public void sort(Object array[]) {
62
        if(array != null && array.length > 1)
63
        {
64
            int             maxLength;
65
  
66
            maxLength = array.length;
67
            swapSpace = new Object[maxLength];
68
            toSort = array;
69
            this.mergeSort(0, maxLength - 1);
70
            swapSpace = null;
71
            toSort = null;
72
        }
73
    }
74

    
75
    public abstract int compareElementsAt(int beginLoc, int endLoc);
76

    
77
    protected void mergeSort(int begin, int end) {
78
        if(begin != end)
79
        {
80
            int           mid;
81

    
82
            mid = (begin + end) / 2;
83
            this.mergeSort(begin, mid);
84
            this.mergeSort(mid + 1, end);
85
            this.merge(begin, mid, end);
86
        }
87
    }
88

    
89
    protected void merge(int begin, int middle, int end) {
90
        int           firstHalf, secondHalf, count;
91

    
92
        firstHalf = count = begin;
93
        secondHalf = middle + 1;
94
        while((firstHalf <= middle) && (secondHalf <= end))
95
        {
96
            if(this.compareElementsAt(secondHalf, firstHalf) < 0)
97
                swapSpace[count++] = toSort[secondHalf++];
98
            else
99
                swapSpace[count++] = toSort[firstHalf++];
100
        }
101
        if(firstHalf <= middle)
102
        {
103
            while(firstHalf <= middle)
104
                swapSpace[count++] = toSort[firstHalf++];
105
        }
106
        else
107
        {
108
            while(secondHalf <= end)
109
                swapSpace[count++] = toSort[secondHalf++];
110
        }
111
        for(count = begin;count <= end;count++)
112
            toSort[count] = swapSpace[count];
113
    }
114
}
115