Subversion Repositories programming

Rev

Rev 55 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 55 Rev 57
Line 1... Line 1...
1
// Written by Ira Snyder
1
// Written by Ira Snyder
-
 
2
// Project #5
2
 
3
 
3
import java.io.*;
4
import java.io.*;
4
 
5
 
5
class SortMethods {
6
class SortMethods {
6
    
7
    
Line 10... Line 11...
10
        //loop from the back of the array moving towards the beginning
11
        //loop from the back of the array moving towards the beginning
11
        for( int i=a.length-1; i>0; i-- )
12
        for( int i=a.length-1; i>0; i-- )
12
            //loop from the beginning of the array to position "i"
13
            //loop from the beginning of the array to position "i"
13
            for( int j=0; j<i; j++ )
14
            for( int j=0; j<i; j++ )
14
                //compare elements next to each other, and swap if necessary
15
                //compare elements next to each other, and swap if necessary
15
                if( a[j].compareTo( a[j+1] ) > 0 ) Test.swap(a,j,j+1);
16
                if( a[j].compareTo(a[j+1]) > 0 ) Test.swap(a,j,j+1);
16
    }
17
    }
17
    
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
18
    public static void ImprovedBubbleSort( Comparable[] a ) {
21
    public static void ImprovedBubbleSort( Comparable[] a ) {
19
        boolean inorder=true;
22
        boolean inorder=true;
20
        
23
        
21
        for( int i=a.length-1; i>0; i-- ) {
24
        for( int i=a.length-1; i>0; i-- ) {
22
            inorder=true;
25
            inorder=true;
Line 54... Line 57...
54
            //store a[i] in a temp location
57
            //store a[i] in a temp location
55
            Comparable ai = a[i];
58
            Comparable ai = a[i];
56
            int j=i; //start for the inner loop
59
            int j=i; //start for the inner loop
57
            
60
            
58
            //find the place to insert ai, and shift elements down
61
            //find the place to insert ai, and shift elements down
59
            for ( j=i; j>0 && a[j-1].compareTo(ai) > 0; j-- )
62
            for ( j=i; j>0 && a[j-1].compareTo(ai) > 0; j-- ) {
60
                a[j] = a[j-1]; //shift down
63
                a[j] = a[j-1]; //shift down
-
 
64
                Test.incSwapCount();
-
 
65
            }
61
            
66
            
62
            //insert ai into position a[j]
67
            //insert ai into position a[j]
63
            a[j] = ai;
68
            a[j] = ai; Test.incSwapCount();
64
        }
69
        }
65
    }
70
    }
66
    
71
    
-
 
72
    // an insertion sort that skips elements as it sorts
67
    private static void SkipInsertionSort( Comparable[] a, int c, int d ) {
73
    private static void SkipInsertionSort( Comparable[] a, int c, int d ) {
68
        for( int i = c+d; i < a.length; i+=d ) {
74
        for( int i = c+d; i < a.length; i+=d ) {
69
            Comparable ai = a[i];
75
            Comparable ai = a[i];
70
            int j = i;
76
            int j = i;
71
            
77
            
72
            while( j > c && a[j-d].compareTo(ai) > 0 ) {
78
            while( j > c && a[j-d].compareTo(ai) > 0 ) {
73
                a[j] = a[j-d];
79
                a[j] = a[j-d]; Test.incSwapCount();
74
                j -= d;
80
                j -= d;
75
            }
81
            }
76
            a[j] = ai;
82
            a[j] = ai; Test.incSwapCount();
77
        }
83
        }
78
    }
84
    }
79
    
85
    
-
 
86
    // this sorts like a comb, with the interval shrinking as
-
 
87
    // we progress in the sort
80
    public static void ShellSort( Comparable[] a ) {
88
    public static void ShellSort( Comparable[] a ) {
81
        for( int d = a.length/2; d > 0; d /= 2 )
89
        for( int d = a.length/2; d > 0; d /= 2 )
82
            for( int c = 0; c < d; c++ )
90
            for( int c = 0; c < d; c++ )
83
                //applies Insertion sort to the skip sequence
91
                //applies Insertion sort to the skip sequence
84
                SkipInsertionSort(a, c, d);
92
                SkipInsertionSort(a, c, d);
85
    }
93
    }
86
    
94
    
-
 
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
87
    public static void MergeSort( Comparable[] a ) {
98
    public static void MergeSort( Comparable[] a ) {
88
        //call mergesort on the whole array
99
        //call the real mergesort on the whole array
89
        MergeSortHelper(a,0,a.length);
100
        MergeSortHelper(a,0,a.length);
90
    }
101
    }
91
    
102
    
92
    // PRECONDITION: 0 <= p <= q <= a.length
103
    // PRECONDITION: 0 <= p <= q <= a.length
93
    // POSTCONDITION: a[p..q-1] is in ascending order
104
    // POSTCONDITION: a[p..q-1] is in ascending order
Line 105... Line 116...
105
        if( a[m-1].compareTo(a[m]) <= 0 ) return;  // a[p..q-1] is already sorted
116
        if( a[m-1].compareTo(a[m]) <= 0 ) return;  // a[p..q-1] is already sorted
106
        int i = p, j = m, k = 0;
117
        int i = p, j = m, k = 0;
107
        Comparable[] aa = new Comparable[q-p];
118
        Comparable[] aa = new Comparable[q-p];
108
        
119
        
109
        while (i < m && j < q)
120
        while (i < m && j < q)
110
            if( a[i].compareTo(a[j]) < 0 ) aa[k++] = a[i++];
121
            if( a[i].compareTo(a[j]) < 0 ) { aa[k++] = a[i++]; Test.incSwapCount(); }
111
            else aa[k++] = a[j++];
122
            else { aa[k++] = a[j++]; Test.incSwapCount(); }
-
 
123
        
-
 
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
        }
112
        
128
        
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];
129
        System.arraycopy(aa, 0, a, p, k);  // copy aa[0..k-1] to a[p..p+k-1];
-
 
130
        Test.incSwapCount(k);
115
    }
131
    }
116
    
132
    
117
    // PRECONDITION: 0 <= p <= q <= a.length
133
    // PRECONDITION: 0 <= p <= q <= a.length
118
    // POSTCONDITION: a[p] <= a[p+1] <= ... <= a[q-1]
134
    // POSTCONDITION: a[p] <= a[p+1] <= ... <= a[q-1]
119
    public static void HeapSort( Comparable[] a ) {
135
    public static void HeapSort( Comparable[] a ) {
Line 123... Line 139...
123
        for( int j = a.length-1; j > 0; j-- ) {
139
        for( int j = a.length-1; j > 0; j-- ) {
124
            Test.swap(a, 0, j);
140
            Test.swap(a, 0, j);
125
            heapify(a, 0, j);
141
            heapify(a, 0, j);
126
        }
142
        }
127
    }
143
    }
128
 
144
    
-
 
145
    //Postcondition: The array a is a Heap
129
    private static void heapify( Comparable[] a, int i, int n ) {
146
    private static void heapify( Comparable[] a, int i, int n ) {
130
        Comparable ai = a[i];
147
        Comparable ai = a[i];
131
        while( i < n/2 ) {                       // a[i] is not a leaf
148
        while( i < n/2 ) {                       // a[i] is not a leaf
132
            int j = 2*i+1;                       // a[j] is ai¿s left child
149
            int j = 2*i+1;                       // a[j] is ai¿s left child
133
            if( j+1 < n && a[j+1].compareTo(a[j]) > 0 ) ++j;  // a[j] is ai's larger child
150
            if( j+1 < n && a[j+1].compareTo(a[j]) > 0 ) ++j;  // a[j] is ai's larger child
134
            if( a[j].compareTo(ai) <= 0 ) break;              // a[j] is not out of order
151
            if( a[j].compareTo(ai) <= 0 ) break;              // a[j] is not out of order
135
            a[i] = a[j];                         // promote a[j]
152
            a[i] = a[j];                         // promote a[j]
-
 
153
            Test.incSwapCount();
136
            i = j;                               // move down to the next level
154
            i = j;                               // move down to the next level
137
        }
155
        }
138
        
156
        
139
        a[i] = ai;
157
        a[i] = ai; Test.incSwapCount();
140
    }
158
    }
141
    
159
    
-
 
160
    //
142
    public static void QuickSort( Comparable[] a ) {
161
    public static void QuickSort( Comparable[] a ) {
143
        //call the real quicksort with the needed parameters
162
        //call the real quicksort with the needed parameters
144
        QuickSortHelper(a,0,a.length);
163
        QuickSortHelper(a,0,a.length);
145
    }
164
    }
146
 
165
 
Line 161... Line 180...
161
        
180
        
162
        while( i < j ) {
181
        while( i < j ) {
163
            while( j > i && a[--j].compareTo(pivot) >= 0 )
182
            while( j > i && a[--j].compareTo(pivot) >= 0 )
164
            ;  // empty loop
183
            ;  // empty loop
165
            
184
            
166
            if( j > i ) { a[i] = a[j]; }
185
            if( j > i ) { a[i] = a[j]; Test.incSwapCount(); }
167
            
186
            
168
            while( i < j && a[++i].compareTo(pivot) <= 0 )
187
            while( i < j && a[++i].compareTo(pivot) <= 0 )
169
            ;  // empty loop
188
            ;  // empty loop
170
            
189
            
171
            if( i < j ) { a[j] = a[i]; }
190
            if( i < j ) { a[j] = a[i]; Test.incSwapCount(); }
172
        }
191
        }
173
        
192
        
174
        a[j] = pivot;
193
        a[j] = pivot; Test.incSwapCount();
175
        return j;
194
        return j;
176
    }
195
    }
177
    
196
    
178
    public static void arraySwap( Object[] a, int posA, int posB ) {
197
    public static void arraySwap( Object[] a, int posA, int posB ) {
179
        Object temp = a[posB]; // temp = B
198
        Object temp = a[posB]; // temp = B