Statistics
| Revision:

root / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / io / raster / QuickSort.java @ 2249

History | View | Annotate | Download (2.67 KB)

1
/*
2
 * Created on 21-jun-2005
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package org.cresques.io.raster;
48

    
49
/**
50
 * @author Robert Sedgewick and Kevin Wayne
51
 * Ordenaci?n de los elementos de un vector
52
 */
53
public class QuickSort {
54
        private long comparisons = 0;
55
        private long exchanges   = 0;
56
        
57
        /***********************************************************************
58
         *  Quicksort code from Sedgewick 7.1, 7.2.
59
         ***********************************************************************/
60
        public void quicksort(int [] a) {
61
                quicksort(a, 0, a.length - 1);
62
        }
63
        public void quicksort(int[] a, int left, int right) { 
64
                if (right <= left) return;
65
                int i = partition(a, left, right);
66
                quicksort(a, left, i-1);
67
                quicksort(a, i+1, right);
68
        }
69
        
70
        private int partition(int[] a, int left, int right) {
71
                int i = left - 1;
72
                int j = right;
73
                
74
                while(true) { 
75
                        
76
                        // find item on left to swap
77
                        while (less(a[++i], a[right]))
78
                                ;
79
                        
80
                        // find item on right to swap
81
                        while (less(a[right], a[--j]))
82
                                if (j == left) break;
83
                                
84
                                // check if pointers cross
85
                        if (i >= j) break;
86
                        
87
                        exch(a, i, j);
88
                }
89
                
90
                // swap with partition element
91
                exch(a, i, right);
92
                
93
                return i;
94
        }
95
        
96
        // is x < y ?
97
        private boolean less(int x, int y) {
98
                comparisons++;
99
                return (x < y);
100
        }
101
        
102
        // exchange a[i] and a[j]
103
        private void exch(int[] a, int i, int j) {
104
                exchanges++;
105
                int swap = a[i];
106
                a[i] = a[j];
107
                a[j] = swap;
108
        }
109
}