-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySorts.java
More file actions
216 lines (178 loc) · 7.15 KB
/
MySorts.java
File metadata and controls
216 lines (178 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Jessica Yang
// APCS1 pd9
// CW
// 2015-12-23
/*======================================
class MySorts -- implements all sort algorithms (BubbleSort, SelectionSort, BogoSort)
======================================*/
import java.util.ArrayList;
public class MySorts {
//~~~~~~~~~~~~~~~~~~~ HELPER METHODS ~~~~~~~~~~~~~~~~~~~
//precond: lo < hi && size > 0
//postcond: returns an ArrayList of random integers
// from lo to hi, inclusive
public static ArrayList populate( int size, int lo, int hi ) {
ArrayList<Integer> retAL = new ArrayList<Integer>();
while( size > 0 ) {
// offset + rand int on interval [lo,hi]
retAL.add( lo + (int)( (hi-lo+1) * Math.random() ) );
size--;
}
return retAL;
}
//randomly rearrange elements of an ArrayList
public static void shuffle( ArrayList al ) {
int randomIndex;
for( int i = al.size()-1; i > 0; i-- ) {
//pick an index at random
randomIndex = (int)( (i+1) * Math.random() );
//swap the values at position i and randomIndex
al.set( i, al.set( randomIndex, al.get(i) ) );
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~ BUBBLESORT METHODS ~~~~~~~~~~~~~~~~~
// VOID version of bubbleSort
// Rearranges elements of input ArrayList
// postcondition: data's elements sorted in ascending order
public static void bubbleSortV( ArrayList<Comparable> data ) {
for( int passCtr = 1; passCtr < data.size(); passCtr++ ) {
System.out.println( "commencing pass #" + passCtr + "..." );
//iterate thru first to next-to-last element, comparing to next
for( int i = 0; i < data.size()-1; i++ ) {
//if element at i > element at 1+1, swap
if ( data.get(i).compareTo(data.get(i+1) ) > 0 )
data.set( i, data.set(i+1,data.get(i)) );
//System.out.println(data); //diag: show current state of list
}
}
}//end bubbleSortV -- O(n*n)
// ArrayList-returning bubbleSort
// postcondition: order of input ArrayList's elements unchanged
// Returns sorted copy of input ArrayList.
public static ArrayList<Comparable> bubbleSort( ArrayList<Comparable> input ) {
//declare and initialize empty ArrayList for copying
ArrayList<Comparable> data = new ArrayList<Comparable>();
//copy input ArrayList into working ArrayList
for( Comparable o : input )
data.add( o );
//sort working ArrayList
bubbleSortV( data );
return data;
}//end bubbleSort -- O(n*n)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~ SELECTIONSORT METHODS ~~~~~~~~~~~~~~~~
// VOID version of SelectionSort
// Rearranges elements of input ArrayList
// postcondition: data's elements sorted in ascending order
public static void selectionSortV( ArrayList<Comparable> data ) {
//note: this version places greatest value at rightmost end,
//maxPos will point to position of SELECTION (greatest value)
int maxPos;
for( int pass = data.size()-1; pass > 0; pass-- ) {
System.out.println( "\nbegin pass " + (data.size()-pass) );//diag
maxPos = 0;
for( int i = 1; i <= pass; i++ ) {
System.out.println( "maxPos: " + maxPos );//diag
System.out.println( data );//diag
if ( data.get(i).compareTo( data.get(maxPos) ) > 0 )
maxPos = i;
}
data.set( maxPos, ( data.set( pass, data.get(maxPos) ) ) );
System.out.println( "after swap: " + data );//diag
}
}//end selectionSort -- worst and best case are O(n*n)
//IMPORTANT: THERE IS NO BEST/WORST CASE
//SELECTION SORT ALWAYS HAS SAME # OF SWAPS & COMPARISONS
// ArrayList-returning selectionSort
// postcondition: order of input ArrayList's elements unchanged
// Returns sorted copy of input ArrayList.
public static ArrayList<Comparable> selectionSort( ArrayList<Comparable> input ) {
//declare and initialize empty ArrayList for copying
ArrayList<Comparable> data = new ArrayList<Comparable>();
//copy input ArrayList into working ArrayList
for( Comparable o : input )
data.add( o );
//sort working ArrayList
selectionSortV( data );
return data;
}//end selectionSort -- O(n*n)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~ BOGOSORT METHODS ~~~~~~~~~~~~~~~~~~
// VOID version of BogoSort
// Rearranges elements of input ArrayList
// postcondition: data's elements sorted in ascending order
public static void bogoSortV( ArrayList<Comparable> data ) {
while (!(isSorted(data))) {
shuffle(data);
}
}//end bogoSort -- worst and best case are O(n*n)
// helper fxn to check if data is sorted
public static boolean isSorted( ArrayList<Comparable> data ) {
for (int x = 0; x < data.size()-1; x++) {
if ((data.get(x)).compareTo(data.get(x+1)) > 0) {
return false;
}
}
return true;
}
// ArrayList-returning bogoSort
// postcondition: order of input ArrayList's elements unchanged
// Returns sorted copy of input ArrayList.
public static ArrayList<Comparable> bogoSort( ArrayList<Comparable> input ) {
//declare and initialize empty ArrayList for copying
ArrayList<Comparable> data = new ArrayList<Comparable>();
//copy input ArrayList into working ArrayList
for( Comparable o : input )
data.add( o );
//sort working ArrayList
bogoSortV( data );
return data;
}//end bogoSort -- O(n*n)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static void main( String [] args ) {
ArrayList glen = new ArrayList<Integer>();
glen.add(7);
glen.add(1);
glen.add(5);
glen.add(12);
glen.add(3);
System.out.println( "ArrayList glen before sorting:\n" + glen );
bubbleSortV(glen);
//selectionSortV(glen);
//bogoSortV(glen);
System.out.println( "ArrayList glen after sorting:\n" + glen );
/*===============for VOID methods=============
ArrayList coco = populate( 10, 1, 1000 );
System.out.println( "ArrayList coco before sorting:\n" + coco );
bubbleSortV(coco);
//selectionSortV(coco);
//bogoSortV(coco);
System.out.println( "ArrayList coco after sorting:\n" + coco );
============================================*/
/*==========for AL-returning methods==========
ArrayList glen = new ArrayList<Integer>();
glen.add(7);
glen.add(1);
glen.add(5);
glen.add(12);
glen.add(3);
System.out.println( "ArrayList glen before sorting:\n" + glen );
ArrayList glenSorted = bubbleSort( glen );
//ArrayList glenSorted = selectionSort( glen );
//ArrayList glenSorted = bogoSort( glen );
System.out.println( "sorted version of ArrayList glen:\n"
+ glenSorted );
System.out.println( "ArrayList glen after sorting:\n" + glen );
ArrayList coco = populate( 10, 1, 1000 );
System.out.println( "ArrayList coco before sorting:\n" + coco );
ArrayList cocoSorted = bubbleSort( coco );
//ArrayList cocoSorted = selectionSort( coco );
//ArrayList cocoSorted = bogoSort( coco );
System.out.println( "sorted version of ArrayList coco:\n"
+ cocoSorted );
System.out.println( "ArrayList coco after sorting:\n" + coco );
System.out.println( coco );
============================================*/
}//end main
}//end class MySorts