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 |
|
|
|
87 |
// PRECONDITION: 0 <= p <= q <= a.length
|
|
|
88 |
// POSTCONDITION: a[p..q-1] is in ascending order
|
|
|
89 |
public static void MergeSort( Comparable[] a, int p, int q ) {
|
|
|
90 |
if (q-p < 2) return;
|
|
|
91 |
int m = (p+q)/2;
|
|
|
92 |
MergeSort(a, p, m);
|
|
|
93 |
MergeSort(a, m, q);
|
|
|
94 |
merge(a, p, m, q);
|
|
|
95 |
}
|
51 |
irasnyd |
96 |
|
53 |
irasnyd |
97 |
// PRECONDITIONS: a[p..m-1] and a[m..q-1] are in ascending order;
|
|
|
98 |
// POSTCONDITION: a[p] <= a[p+1] <= ... <= a[q-1]
|
|
|
99 |
private static void merge( Comparable[] a, int p, int m, int q ) {
|
|
|
100 |
if( a[m-1].compareTo(a[m]) <= 0 ) return; // a[p..q-1] is already sorted
|
|
|
101 |
int i = p, j = m, k = 0;
|
|
|
102 |
Comparable[] aa = new Comparable[q-p];
|
|
|
103 |
|
|
|
104 |
while (i < m && j < q)
|
|
|
105 |
if( a[i].compareTo(a[j]) < 0 ) aa[k++] = a[i++];
|
|
|
106 |
else aa[k++] = a[j++];
|
|
|
107 |
|
|
|
108 |
if (i < m) System.arraycopy(a, i, a, p+k, m-i); // shift a[i..m-1]
|
|
|
109 |
System.arraycopy(aa, 0, a, p, k); // copy aa[0..k-1] to a[p..p+k-1];
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// PRECONDITION: 0 <= p <= q <= a.length
|
|
|
113 |
// POSTCONDITION: a[p] <= a[p+1] <= ... <= a[q-1]
|
|
|
114 |
public static void HeapSort( Comparable[] a ) {
|
|
|
115 |
for( int i = (a.length-1)/2; i >= 0; i-- )
|
|
|
116 |
heapify(a, i, a.length);
|
|
|
117 |
|
|
|
118 |
for( int j = a.length-1; j > 0; j-- ) {
|
|
|
119 |
Test.swap(a, 0, j);
|
|
|
120 |
heapify(a, 0, j);
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
private static void heapify( Comparable[] a, int i, int n ) {
|
|
|
125 |
Comparable ai = a[i];
|
|
|
126 |
while( i < n/2 ) { // a[i] is not a leaf
|
|
|
127 |
int j = 2*i+1; // a[j] is ai¿s left child
|
|
|
128 |
if( j+1 < n && a[j+1].compareTo(a[j]) > 0 ) ++j; // a[j] is ai¿s larger child
|
|
|
129 |
if( a[j].compareTo(ai) <= 0 ) break; // a[j] is not out of order
|
|
|
130 |
a[i] = a[j]; // promote a[j]
|
|
|
131 |
i = j; // move down to the next level
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
a[i] = ai;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
// PRECONDITION: 0 <= p <= q <= a.length
|
|
|
138 |
// POSTCONDITION: a[p] <= a[p+1] <= ... <= a[q-1]
|
|
|
139 |
public static void QuickSort( Comparable[] a, int p, int q ) {
|
|
|
140 |
if (q-p < 2) return;
|
|
|
141 |
int j = partition(a, p, q);
|
|
|
142 |
QuickSort(a, p, j);
|
|
|
143 |
QuickSort(a, j+1, q);
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
// RETURNS: index j of pivot element a[j];
|
|
|
147 |
// POSTCONDITION: a[i] <= a[j] <= a[k] for p <= i <= j <= k < q;
|
|
|
148 |
private static int partition( Comparable[] a, int p, int q ) {
|
|
|
149 |
Comparable pivot = a[p];
|
|
|
150 |
int i = p, j = q;
|
|
|
151 |
|
|
|
152 |
while( i < j ) {
|
|
|
153 |
while( j > i && a[--j].compareTo(pivot) >= 0 )
|
|
|
154 |
; // empty loop
|
|
|
155 |
|
|
|
156 |
if( j > i ) { a[i] = a[j]; }
|
|
|
157 |
|
|
|
158 |
while( i < j && a[++i].compareTo(pivot) <= 0 )
|
|
|
159 |
; // empty loop
|
|
|
160 |
|
|
|
161 |
if( i < j ) { a[j] = a[i]; }
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
a[j] = pivot;
|
|
|
165 |
return j;
|
|
|
166 |
}
|
|
|
167 |
|
51 |
irasnyd |
168 |
public static void arraySwap( Object[] a, int posA, int posB ) {
|
|
|
169 |
Object temp = a[posB]; // temp = B
|
|
|
170 |
a[posB] = a[posA]; // B = A
|
|
|
171 |
a[posA] = temp; // A = temp
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
|