-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProbOne.java
More file actions
171 lines (122 loc) · 4.02 KB
/
ProbOne.java
File metadata and controls
171 lines (122 loc) · 4.02 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
import java.util.Random ;
import java.util.Arrays ;
public class ProbOne {
/*
We learned a couple of sorting algorithms over the last two weeks. Your task in this problem is to
implement one of the most efficient algorithms, namely quick sort.
Hint: When selecting a pivot, use the hint given in the slides to avoid the worst case scenario
(1) A single executable program implementing quick sort.
(2) At the beginning, the program should create an array of 100 values randomly selected from the
range of [1..1000]. That will be the array to be sorted.
(3) The output of the algorithm is a file containing several lines. Each line will list the current state
of the 100 values (comma separated). After each iteration of the algorithm, one line should be added to
the output file. Certainly, the last line should have all values completely sorted.
*/
// main function
public static void main (String[] args) {
Random rand = new Random () ;
int[] array = new int[100] ;
for (int i = 0 ; i < 100 ; i++) {
array[i] = rand.nextInt(1000) + 1 ;
}
System.out.println("Orignial randomly generated array is: " + '\n' + Arrays.toString(array) + '\n') ;
int start = 0 ;
int end = 99 ;
int [] finalArray = new int [100] ;
finalArray = quickSort(array, start, end) ;
System.out.println('\n' + "The final, sorted array is: " + '\n' + Arrays.toString(finalArray)) ;
}
// quick sort function
static int [] quickSort(int[] array, int start, int end) {
if (start < end) {
int q = partition(array, start, end) ;
quickSort(array, start, q - 1) ;
quickSort(array, q + 1, end) ;
}
System.out.println(Arrays.toString(array)) ;
return array ;
}
static int partition(int [] array, int start, int end) {
int midIndex = (end - start)/2 ;
// Pick an element to be the pivot in array
int rightmostElt = array[start] ;
int middleElt = array[midIndex] ;
int leftmostElt = array[end] ;
// find good pivot
int pivot = findPivot(leftmostElt, middleElt, rightmostElt) ;
// switch pivot and element in rightmost position
// if pivot is the last elt
if (array[end] == pivot) {
array[end] = pivot;
}
// if pivot is the first element
else if (array[start] == pivot) {
// switch elts
array[start] = array[end] ;
array[end] = pivot ;
}
// if pivot is the middle element
else if (array[midIndex] == pivot) {
array[midIndex] = array[end] ;
array[end] = pivot ;
}
// if p is beginning of array is it 0?
// index its gonna reset and shift
int i = start - 1 ;
for (int j = start ; j < end ; j++) {
if (array[j] <= pivot) {
i = i + 1 ;
// exchange A[i] with A[j]
int temp = 0 ;
temp = array[i] ;
array[i] = array[j] ;
array[j] = temp ;
}
}
// exchange A[i + 1] with A[r]
int temp = 0 ;
temp = array[i + 1] ;
array[i + 1] = array[end] ;
array[end] = temp ;
return i + 1;
}
static int findPivot (int leftmostElt, int middleElt, int rightmostElt) {
// order rightmost, leftmost, and middle element
int [] medianList = new int [3] ;
// R is least
if ((rightmostElt <= leftmostElt) && (rightmostElt <= middleElt)) {
medianList[0] = rightmostElt ;
if (leftmostElt <= middleElt) {
medianList[1] = leftmostElt ;
medianList[2] = middleElt ;
} else {
medianList[1] = middleElt ;
medianList[2] = leftmostElt ;
}
}
// L is least
else if ((leftmostElt <= rightmostElt) && (leftmostElt <= middleElt)) {
medianList[0] = leftmostElt ;
if (rightmostElt <= middleElt) {
medianList[1] = rightmostElt ;
medianList[2] = middleElt ;
} else {
medianList[1] = middleElt ;
medianList[2] = rightmostElt ;
}
}
// middle is least
else {
medianList[0] = middleElt ;
if (leftmostElt <= middleElt) {
medianList[1] = leftmostElt ;
medianList[2] = rightmostElt ;
} else {
medianList[1] = rightmostElt ;
medianList[2] = leftmostElt ;
}
}
int median = medianList[1] ;
return median ;
}
}