Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / io / raster / QuickSort.java @ 2312

History | View | Annotate | Download (2.19 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 * 
4
 * Copyright (C) 2004-5. 
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
 * cresques@gmail.com
23
 */
24
package org.cresques.io.raster;
25

    
26
/**
27
 * Ordenaci?n de los elementos de un vector
28
 * @author Robert Sedgewick and Kevin Wayne
29
 */
30
public class QuickSort {
31
        private long comparisons = 0;
32
        private long exchanges   = 0;
33
        
34
        /***********************************************************************
35
         *  Quicksort code from Sedgewick 7.1, 7.2.
36
         ***********************************************************************/
37
        public void quicksort(int [] a) {
38
                quicksort(a, 0, a.length - 1);
39
        }
40
        public void quicksort(int[] a, int left, int right) { 
41
                if (right <= left) return;
42
                int i = partition(a, left, right);
43
                quicksort(a, left, i-1);
44
                quicksort(a, i+1, right);
45
        }
46
        
47
        private int partition(int[] a, int left, int right) {
48
                int i = left - 1;
49
                int j = right;
50
                
51
                while(true) { 
52
                        
53
                        // find item on left to swap
54
                        while (less(a[++i], a[right]))
55
                                ;
56
                        
57
                        // find item on right to swap
58
                        while (less(a[right], a[--j]))
59
                                if (j == left) break;
60
                                
61
                                // check if pointers cross
62
                        if (i >= j) break;
63
                        
64
                        exch(a, i, j);
65
                }
66
                
67
                // swap with partition element
68
                exch(a, i, right);
69
                
70
                return i;
71
        }
72
        
73
        // is x < y ?
74
        private boolean less(int x, int y) {
75
                comparisons++;
76
                return (x < y);
77
        }
78
        
79
        // exchange a[i] and a[j]
80
        private void exch(int[] a, int i, int j) {
81
                exchanges++;
82
                int swap = a[i];
83
                a[i] = a[j];
84
                a[j] = swap;
85
        }
86
}