-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortTester.java
116 lines (108 loc) · 3.49 KB
/
SortTester.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
import java.util.Random;
/**
* Comparing to two sorting method runtime,
* by making them to sort a said size array filled with random Integers.
* @author Zichang Liu
* @version July 20
*/
public class SortTester {
/**
* Partition for Quick Sort.
* Retrieved from http://www.algolist.net/Algorithms/Sorting/Quicksort.
* @param arr array to be sorted.
* @param left Part of the array that is smaller than pivot.
* @param right Part of the array that is larger than pivot.
* @return Index of the new pivot.
*/
public int partition(int[] arr, int left, int right) {
int i = left;
int j = right;
int tmp;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
return i;
}
/**
* Wrapper function for Quick Sort, keep tracks of the runtime.
* @param arr array to be sorted.
* @param left Part of the array that is smaller than pivot.
* @param right Part of the array that is larger than pivot.
* @return the total run time of Quick Sort.
*/
public long timeLogSort(int[] arr, int left, int right) {
long startTime = System.nanoTime();
logarithmicSort(arr, left, right - 1);
long endTime = System.nanoTime();
return endTime - startTime;
}
/**
* Quick sort.
* Retrieved from http://www.algolist.net/Algorithms/Sorting/Quicksort.
* @param arr array to be sorted.
* @param left Part of the array that is smaller than pivot.
* @param right Part of the array that is larger than pivot.
*/
public void logarithmicSort(int[] arr, int left, int right) {
int index = partition(arr, left, right);
if (left < index - 1) {
logarithmicSort(arr, left, index - 1);
}
if (index < right) {
logarithmicSort(arr, index, right);
}
}
/**
* Selection Sort.
* Retrieved from https://www.geeksforgeeks.org/selection-sort/.
* @param arr Array to be sort.
* @return Total run time of Selection Sort.
*/
public long quadraticSort(int[] arr) {
long startTime = System.nanoTime();
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
int minimumIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minimumIndex]) {
minimumIndex = j;
}
}
// Swap the found minimum element with the first
// element
int temp = arr[minimumIndex];
arr[minimumIndex] = arr[i];
arr[i] = temp;
}
long endTime = System.nanoTime();
return endTime - startTime;
}
/**
* Setting up the array for testing.
* @param setup Array to be set up.
* @param size size to be set up.
* @return Array that completed the set up.
*/
public int[] setUp(int[] setup, int size) {
Random setUpInt = new Random();
for (int i = 0; i < size; i++) {
setup[i] = setUpInt.nextInt(size);
}
return setup;
}
}