-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
129 lines (106 loc) · 4.14 KB
/
SelectionSort.java
File metadata and controls
129 lines (106 loc) · 4.14 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
// Jessica Yang
// APCS1 pd9
// CW
// 2015-12-23
/*======================================
class SelectionSort -- implements SelectionSort algorithm
======================================*/
import java.util.ArrayList;
public class SelectionSort {
//~~~~~~~~~~~~~~~~~~~ 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) ) );
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 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)
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 );
selectionSortV(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 );
selectionSortV(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 = selectionSort( 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 = selectionSort( 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 SelectionSort