Subversion Repositories programming

Rev

Rev 100 | Details | Compare with Previous | Last modification | View Log | RSS feed

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