Subversion Repositories programming

Rev

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