Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
51 irasnyd 1
// Written by Ira Snyder
2
 
3
import java.io.*;
4
 
5
class SortMethods {
52 irasnyd 6
 
7
    // method to sort the array a (passed as a parameter)
8
    // this will sort in O(n^2) time
53 irasnyd 9
    public static void BubbleSort( Comparable[] a ) {
52 irasnyd 10
        //loop from the back of the array moving towards the beginning
51 irasnyd 11
        for( int i=a.length-1; i>0; i-- )
52 irasnyd 12
            //loop from the beginning of the array to position "i"
51 irasnyd 13
            for( int j=0; j<i; j++ )
52 irasnyd 14
                //compare elements next to each other, and swap if necessary
53 irasnyd 15
                if( a[j].compareTo( a[j+1] ) > 0 ) Test.swap(a,j,j+1);
51 irasnyd 16
    }
17
 
53 irasnyd 18
    public static void ImprovedBubbleSort( Comparable[] a ) {
52 irasnyd 19
        boolean inorder=true;
20
 
21
        for( int i=a.length-1; i>0; i-- ) {
22
            inorder=true;
23
 
24
            for( int j=0; j<i; j++ )
53 irasnyd 25
                if( a[j].compareTo(a[j+1]) > 0 ) {
26
                    Test.swap(a,j,j+1);
52 irasnyd 27
                    inorder=false;
28
                }
29
            if(inorder) break;  //array sorted, so bail out
30
        }
31
    }
32
 
33
    // method to sort the array a (passed as a paramter)
34
    // this will sort in O(n^2) time
53 irasnyd 35
    public static void SelectionSort( Comparable[] a ) {
52 irasnyd 36
        //loop from the back of the array moving towards the front
37
        for( int i=a.length-1; i>0; i-- ) {
38
            //placeholder for the maximum element
39
            int m=0;
40
            //find the maximum element in the array
41
            for( int j=1; j<=i; j++ )
53 irasnyd 42
                if( a[j].compareTo(a[m]) > 0 ) m = j;
52 irasnyd 43
 
44
            //swap the "i"th element with the maximum element
53 irasnyd 45
            Test.swap(a,i,m);
52 irasnyd 46
        }
47
    }
48
 
49
    // method to sort the array a (passed as a parameter)
50
    // this will sort in O(n^2) time
53 irasnyd 51
    public static void InsertionSort( Comparable[] a ) {
52 irasnyd 52
        //iterate through the array from the beginning to the end
53
        for ( int i=1; i<a.length; i++ ) {
54
            //store a[i] in a temp location
53 irasnyd 55
            Comparable ai = a[i];
52 irasnyd 56
            int j=i; //start for the inner loop
57
 
58
            //find the place to insert ai, and shift elements down
53 irasnyd 59
            for ( j=i; j>0 && a[j-1].compareTo(ai) > 0; j-- )
52 irasnyd 60
                a[j] = a[j-1]; //shift down
61
 
62
            //insert ai into position a[j]
63
            a[j] = ai;
64
        }
65
    }
66
 
53 irasnyd 67
    private static void SkipInsertionSort( Comparable[] a, int c, int d ) {
68
        for( int i = c+d; i < a.length; i+=d ) {
69
            Comparable ai = a[i];
70
            int j = i;
71
 
72
            while( j > c && a[j-d].compareTo(ai) > 0 ) {
73
                a[j] = a[j-d];
74
                j -= d;
75
            }
76
            a[j] = ai;
77
        }
78
    }
79
 
80
    public static void ShellSort( Comparable[] a ) {
81
        for( int d = a.length/2; d > 0; d /= 2 )
82
            for( int c = 0; c < d; c++ )
83
                //applies Insertion sort to the skip sequence
84
                SkipInsertionSort(a, c, d);
85
    }
86
 
54 irasnyd 87
    public static void MergeSort( Comparable[] a ) {
88
        //call mergesort on the whole array
89
        MergeSortHelper(a,0,a.length);
90
    }
91
 
53 irasnyd 92
    // PRECONDITION: 0 <= p <= q <= a.length
93
    // POSTCONDITION: a[p..q-1] is in ascending order
54 irasnyd 94
    private static void MergeSortHelper( Comparable[] a, int p, int q ) {
53 irasnyd 95
        if (q-p < 2) return;
96
        int m = (p+q)/2;
54 irasnyd 97
        MergeSortHelper(a, p, m);
98
        MergeSortHelper(a, m, q);
53 irasnyd 99
        merge(a, p, m, q);
100
    }
51 irasnyd 101
 
53 irasnyd 102
    // PRECONDITIONS: a[p..m-1] and a[m..q-1] are in ascending order;
103
    // POSTCONDITION: a[p] <= a[p+1] <= ... <= a[q-1]
104
    private static void merge( Comparable[] a, int p, int m, int q ) {
105
        if( a[m-1].compareTo(a[m]) <= 0 ) return;  // a[p..q-1] is already sorted
106
        int i = p, j = m, k = 0;
107
        Comparable[] aa = new Comparable[q-p];
108
 
109
        while (i < m && j < q)
110
            if( a[i].compareTo(a[j]) < 0 ) aa[k++] = a[i++];
111
            else aa[k++] = a[j++];
112
 
113
        if (i < m) System.arraycopy(a, i, a, p+k, m-i);  // shift a[i..m-1]
114
        System.arraycopy(aa, 0, a, p, k);  // copy aa[0..k-1] to a[p..p+k-1];
115
    }
116
 
117
    // PRECONDITION: 0 <= p <= q <= a.length
118
    // POSTCONDITION: a[p] <= a[p+1] <= ... <= a[q-1]
119
    public static void HeapSort( Comparable[] a ) {
120
        for( int i = (a.length-1)/2; i >= 0; i-- )
121
            heapify(a, i, a.length);
122
 
123
        for( int j = a.length-1; j > 0; j-- ) {
124
            Test.swap(a, 0, j);
125
            heapify(a, 0, j);
126
        }
127
    }
128
 
129
    private static void heapify( Comparable[] a, int i, int n ) {
130
        Comparable ai = a[i];
131
        while( i < n/2 ) {                       // a[i] is not a leaf
132
            int j = 2*i+1;                       // a[j] is ai¿s left child
55 irasnyd 133
            if( j+1 < n && a[j+1].compareTo(a[j]) > 0 ) ++j;  // a[j] is ai's larger child
53 irasnyd 134
            if( a[j].compareTo(ai) <= 0 ) break;              // a[j] is not out of order
135
            a[i] = a[j];                         // promote a[j]
136
            i = j;                               // move down to the next level
137
        }
138
 
139
        a[i] = ai;
140
    }
141
 
54 irasnyd 142
    public static void QuickSort( Comparable[] a ) {
143
        //call the real quicksort with the needed parameters
144
        QuickSortHelper(a,0,a.length);
145
    }
146
 
53 irasnyd 147
    // PRECONDITION: 0 <= p <= q <= a.length
148
    // POSTCONDITION: a[p] <= a[p+1] <= ... <= a[q-1]
54 irasnyd 149
    private static void QuickSortHelper( Comparable[] a, int p, int q ) {
53 irasnyd 150
        if (q-p < 2) return;
151
        int j = partition(a, p, q);
54 irasnyd 152
        QuickSortHelper(a, p, j);
153
        QuickSortHelper(a, j+1, q);
53 irasnyd 154
    }
155
 
156
    // RETURNS: index j of pivot element a[j];
157
    // POSTCONDITION: a[i] <= a[j] <= a[k] for p <= i <= j <= k < q;
158
    private static int partition( Comparable[] a, int p, int q ) {
159
        Comparable pivot = a[p];
160
        int i = p, j = q;
161
 
162
        while( i < j ) {
163
            while( j > i && a[--j].compareTo(pivot) >= 0 )
164
            ;  // empty loop
165
 
166
            if( j > i ) { a[i] = a[j]; }
167
 
168
            while( i < j && a[++i].compareTo(pivot) <= 0 )
169
            ;  // empty loop
170
 
171
            if( i < j ) { a[j] = a[i]; }
172
        }
173
 
174
        a[j] = pivot;
175
        return j;
176
    }
177
 
51 irasnyd 178
    public static void arraySwap( Object[] a, int posA, int posB ) {
179
        Object temp = a[posB]; // temp = B
180
        a[posB] = a[posA];     // B = A
181
        a[posA] = temp;        // A = temp
182
    }
183
}
184