-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorts.java
207 lines (183 loc) · 4.56 KB
/
Sorts.java
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
import java.util.Random;
public class Sorts {
private int[] nos;
private int steps;
// Constructs a default array of size 10
public Sorts() {
nos = new int[10];
nos[0] = -10001;
nos[1] = 3;
nos[2] = 7;
nos[3] = 19;
nos[4] = 15;
nos[5] = 19;
nos[6] = 7;
nos[7] = 3;
nos[8] = 19;
nos[9] = -100;
}
public Sorts(int[] temp) {
nos = temp;
}
// Constructs an array with size random Sorts from [0,range)
public Sorts(int size, int range) {
nos = new int[size];
for (int i = 0; i < size; i++)
{
nos[i] = (int)(Math.random()*range)+1;
}
}
// Constructs an array of random Sorts [0-range) array of size count with a
// seed
// The seed allows you to use the same set of random numbers
public Sorts(int count, int range, long seed) {
nos = new int[count];
Random rand = new Random(seed);
for (int i = 0; i < count; i++)
nos[i] = rand.nextInt(range);
}
// This constructor will create an ordered array of consecutive integers
// true will yield ascending order
// false will yield descending order
public Sorts(int count, boolean ordered) {
nos = new int[count];
for(int i = 0; i < nos.length; i++) {
if (ordered)
{
nos[i] = i;
}
else
{
nos[i] = nos.length-i-1;
}
}
}
public int getSteps() {
return steps;
}
public void display() {
for (int x : nos)
System.out.print(x + " ");
System.out.println();
}
public int[] getNos() {
return nos;
}
public void swap(int x, int y) {
int temp = nos[x];
nos[x] = nos[y];
nos[y] = temp;
steps += 3;
}
public void insertionSort() {
int temp;
for (int i =1; i <nos.length; i++)
{
steps++;
for (int j = i-1; j >= 0 && nos[i] < nos[j]; j--, i--)
{
steps++;
temp = nos[i];
nos[i] = nos[j];
nos[j] = temp;
}
}
}
public void selectionSort() {
steps = 0;
for (int x = 0; x < nos.length; x++) {
steps++;
int minIndex = x;
int z = x + 1;
while (z < nos.length) {
steps++;
if (nos[z] < nos[minIndex]) {
minIndex = z;
}
z++;
}
int temp = nos[minIndex];
nos[minIndex] = nos[x];
nos[x] = temp;
}
}
public void bubbleSort() {
//Consecutive values are compared and swapped if necessary
steps = 0;
boolean swapped = true;
steps++;
int lastSwap = nos.length - 1;
steps++;
int temp = 0;
steps++;
steps++; // initialize for loop
for (int i = 0; i < nos.length; i++) {
steps += 3; // boundary check, increment,if
if (swapped) {
swapped = false;
steps++;
steps++; // initialize for loop
for (int j = 0; j < lastSwap; j++) {
steps += 3; // boundary check, increment,if
if (nos[j] > nos[j + 1]) {
swap(j, j + 1);
swapped = true;
steps++;
temp = j;
steps++;
}
}
lastSwap = temp;
steps++;
}
}
}
/*For our quadratic sort, we decided to sort according to the maximum value. Similarly, to selection sort,
* we find the maximum value of the entire array and sort the max value to the last unsorted array.
* Then, we continue iterating the loop comparing values in the unsorted array to find the max and
* append the max founded to the beginning of the sorted array. We continue until the entire array has
* been traversed.
*/
public void ourSort() {
int maxIndex = 0;
int temp;
for (int i =0; i<nos.length; i++)
{
maxIndex=0;
steps++;
for (int j=0; j < nos.length-i; j++)
{
steps++;
if (nos[j] > nos[maxIndex])
maxIndex = j;
}
temp = nos[nos.length-1-i];
nos[nos.length-1-i] = nos[maxIndex];
nos[maxIndex] = temp;
}
}
public static void main(String[] args) {
Sorts one = new Sorts(60, 1000);
StopWatch timer = new StopWatch();
timer.start();
one.display();
// one.bubbleSort();
one.ourSort();
timer.stop();
one.display();
System.out.println("Default Array Steps: " + one.getSteps());
System.out.println("Default Array time: " + timer.getElapsedTime()+ " milliseconds.");
//
//This is a sample code for testing bubble sort for data in reverse order
// Sorts two = new Sorts (80, false);
// two.display();
// timer.reset();
// timer.start();
// two.ourSort();
// timer.stop();
// two.display();
// System.out.println("Reverse order Steps: " + two.getSteps());
// System.out.println("Reverse order time: " + timer.getElapsedTime()+ " milliseconds");
// System.out.println();
}
}