-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoSorting.java
More file actions
199 lines (173 loc) · 5.06 KB
/
Copy pathDemoSorting.java
File metadata and controls
199 lines (173 loc) · 5.06 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
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
/**
* @author Fahmida Hamid
* @author Rowan Holop
* @version March 29 2020
* This class implements several known sorting algorithms:
* insertionSort
* selectionSort
* mergeSort
*/
class SortingAlgorithms{
/**
* Insertion Sort
* @param <T>
* @param a: an array of T type objects
*/
public static <T extends Comparable <? super T>> void insertionSort(T[] a)
{
int n = a.length;
int i = 1;
int j;
while (i < n)
{
j = i;
while ((j > 0) && (a[j-1].compareTo(a[j]) > 0))
{
T temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
j--;
}
i++;
}
}
/**
* Bubble Sort
* @param <T>
* @param a: an array of T type objects
*/
public static <T extends Comparable <? super T>> void bubbleSort(T[] a)
{
int n = a.length;
boolean swapped = true;
while (swapped)
{
swapped = false;
for (int i = 1; i < n; i++)
{
if (a[i-1].compareTo(a[i]) > 0)
{
T temp = a[i-1];
a[i-1] = a[i];
a[i] = temp;
swapped = true;
}
}
}
}
/**
* Selection Sort
* @param <T>
* @param a : an array of T type objects
*/
public static <T extends Comparable <? super T>> void selectionSort(T[] a)
{
for (int i = 0; i < a.length-1; i++)
{
int minIndex = i;
for(int j = i+1; j < a.length; j++)
{
if(a[j].compareTo(a[minIndex]) < 0)
{
minIndex = j;
}
}
if(minIndex != i)
{
T temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
}
/**
* mergeSort
* @param <T>
* @param a : an array of T type objects
* I took Weiss's implementation of mergeSort
*/
public static <T extends Comparable <? super T>> void mergeSort(T[] a)
{
/*
* Creating a temporary array that will be
* used to store the the sorted sub-lists.
*/
T[] temp = (T[]) new Comparable[a.length];
mergeSort(a, temp, 0, a.length-1);
}
/**
* mergeSort
* @param <T>
* @param a: given input
* @param temp: used to copy/store the left and right sublist
* @param p: left-most index
* @param r: right-most index
*/
public static <T extends Comparable <? super T>> void mergeSort(T[] a, T[] temp, int p , int r)
{
if(p < r)
{
int q = (p+r)/2;
mergeSort( a, temp, p, q);
mergeSort( a, temp, q+1, r);
merge(a, temp, p, q+1, r);
}
}
/**
* merge: merges the two sorted sublists into one
* @param <T>
* @param a
* @param tempArr
* @param leftPos
* @param rightPos
* @param rightEnd
*/
public static <T extends Comparable <? super T>> void merge(T[] a, T[] tempArr, int leftPos , int rightPos, int rightEnd)
{
int leftEnd = rightPos-1;
int tempPos = leftPos;
int numElements = rightEnd - leftPos + 1;
while( leftPos <= leftEnd && rightPos <= rightEnd)
{
if(a[leftPos].compareTo(a[rightPos]) <= 0)
tempArr[tempPos++] = a[leftPos++];
else
tempArr[tempPos++] = a[rightPos++];
}
/* copy the left over from the left subarray */
while(leftPos <= leftEnd)
tempArr[tempPos++] = a[leftPos++];
/* copy the left over from the right subarray */
while(rightPos <= rightEnd)
tempArr[tempPos++] = a[rightPos++];
/* copy the sorted data back to the original array */
for(int i = 0; i < numElements; i++, rightEnd--)
a[rightEnd] = tempArr[rightEnd];
}
}
public class DemoSorting {
public static <T extends Comparable <? super T>> void printData(T[] arr)
{
for(int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args){
Integer[] arr = RandomIntegerGenerator.generateRandomNumbers(20);
//this is your unsorted array
printData(arr);
//making a copy (deep copy)
Integer[] copiedArray1 = new Integer[20];
System.out.println(arr.length + " this is array length should be 20");
System.arraycopy(arr, 0, copiedArray1, 0, 20);
long startTime = System.nanoTime();
SortingAlgorithms.mergeSort(copiedArray1);
//now, copiedArray1 has the elements in sorted order
long stopTime = System.nanoTime();
double runningTime = (double)(stopTime - startTime)/1000000000;
printData(copiedArray1);
System.out.println("Running time: " + runningTime);
}
}