-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort.java
192 lines (163 loc) · 4.58 KB
/
Sort.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
import java.util.Arrays;
import java.util.Random;
import java.io.InputStream;
import java.lang.*;
public class Sort {
public static int randomFill(){
Random rand = new Random();
int randomNum = rand.nextInt();
return (randomNum%1000)+1000;
}
public static void selection_sort(int[] anArray, int size){
int tmp;
int min;
for (int i=0;i<size-1;i++) {
min=i;
for(int j=i+1;j<size;j++) {
if(anArray[j]<anArray[min]) {
tmp=anArray[i];
anArray[i]=anArray[min];
anArray[min]=tmp;
}
}
}
}
public static void insertion_sort(int[] anArray, int size){
for(int i=0;i<size;i++) {
int min=anArray[i];
for(int j=i;j>0;j--) {
if(min<anArray[j-1]) {
int tmp=anArray[i];
anArray[j]=anArray[j-1];
anArray[j-1]=tmp;
}
else {
break;
}
}
}
}
public static void merge_sort(int[] anArray, int size){
if(size<2) {
return;
}
int mid=size/2;
int leftSize=mid;
int rightSize=size-mid;
int []left=new int[leftSize];
int [] right=new int[rightSize];
for(int i=0;i<mid;i++) {
left[i]=anArray[i];
}
for(int i=mid;i<size;i++) {
right[i-mid]=anArray[i];
}
merge_sort(left,leftSize);
merge_sort(right,rightSize);
merge(left, right, anArray);
}
public static void merge(int[] left, int[] right, int[] arr) {
int leftSize = left.length;
int rightSize = right.length;
int i = 0, j = 0, k = 0;
while (i < leftSize && j < rightSize) {
if (left[i] <= right[j]) {
arr[k] = left[i];
i++;
k++;
} else {
arr[k] = right[j];
k++;
j++;
}
}
while (i < leftSize) {
arr[k] = left[i];
k++;
i++;
}
while (j < leftSize) {
arr[k] = right[j];
k++;
j++;
}
}
public static void quick_sort(int[] anArray, int size){
recursiveQuickSort(anArray, 0, anArray.length - 1);
}
public static void recursiveQuickSort(int[] array, int startIdx, int endIdx) {
int pivotIndex=partition(array, startIdx, endIdx);
if(startIdx<pivotIndex-1) {
recursiveQuickSort(array, startIdx, pivotIndex - 1);
}
if (endIdx > pivotIndex) {
recursiveQuickSort(array, pivotIndex, endIdx);
}
}
public static int partition(int[] array, int left, int right) {
int pivot=array[left];
while(left<=right) {
while(array[left]<pivot) {
left++;
}
while(array[right]>pivot) {
right--;
}
if(left<=right) {
int tmp=array[left];
array[left]=array[right];
array[right]=tmp;
left++;
right--;
}
}
return left;
}
public static void main (String[] args) {
int size= 1000;
int choice=3;
System.out.println(size);
System.out.println(choice);
int[] anArray;
if (args.length > 0) {
try {
size = Integer.parseInt(args[0]);
choice = Integer.parseInt(args[1]);
System.out.println(size + choice);
} catch (NumberFormatException e) {
System.err.println("Argument" + args[0] + " must be an integer.");
System.exit(1);
}
}
System.out.println(size);
System.out.println(choice);
if(choice>=1 && choice<=4){
anArray = new int[size];
for(int i=0;i<anArray.length;i++)
{
anArray[i] = randomFill();
//System.out.println(anArray[i]);
}
double startTime = System.nanoTime();
if(choice == 1){
System.out.println("Selection sort is selected\n");
selection_sort(anArray, size);
}
if(choice == 2){
System.out.println("Insertion sort is selected\n");
insertion_sort(anArray, size);
}
if(choice == 3){
System.out.println("Merge sort is selected\n");
merge_sort(anArray, size);
}
if(choice == 4){
System.out.println("Quick sort is selected\n");
quick_sort(anArray, size);
}
double endTime = System.nanoTime();
double duration = (endTime - startTime)/1000000; //divide by 1000000 to get milliseconds.
System.out.println("it took: " + duration + " milliseconds");
}
}
}