Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ada393e

Browse files
First Commit.
0 parents commit ada393e

File tree

3 files changed

+302
-0
lines changed

3 files changed

+302
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.ozuduru.implementsorting;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
6+
public class Main {
7+
private static void printArray(int[] arr) {
8+
int len = arr.length;
9+
for(int i = 0; i < len; ++i)
10+
System.out.print(String.valueOf(arr[i]) + " ");
11+
System.out.println();
12+
}
13+
14+
// Create unsorted array
15+
private static int[] createArray(int len) {
16+
ArrayList<Integer> list = new ArrayList<>();
17+
for(int i = 0; i < len; ++i)
18+
list.add(i);
19+
Collections.shuffle(list);
20+
21+
int[] arr = new int[len];
22+
for(int i = 0; i < len; ++i)
23+
arr[i] = list.get(i);
24+
25+
return arr;
26+
}
27+
28+
public static void main(String[] args) {
29+
// Set the timer
30+
MyTimer timer = new MyTimer();
31+
timer.reset();
32+
33+
int len = 30;
34+
35+
//-------Insertion Sort
36+
int[] insertionSortArr = createArray(len);
37+
System.out.println("Unsorted Array:");
38+
printArray(insertionSortArr);
39+
System.out.println("Sorted by Insertion Sort:");
40+
timer.start();
41+
SortingEngine.insertionSort(insertionSortArr);
42+
timer.stop();
43+
printArray(insertionSortArr);
44+
System.out.println("Execution time of Insertion Sort: " + timer.getTotalTimeInMilis() + "ms.\n");
45+
timer.reset();
46+
47+
//-------Selection Sort
48+
int[] selectionSortArr = createArray(len);
49+
System.out.println("Unsorted Array:");
50+
printArray(selectionSortArr);
51+
System.out.println("Sorted by Selection Sort:");
52+
timer.start();
53+
SortingEngine.selectionSort(selectionSortArr);
54+
timer.stop();
55+
printArray(selectionSortArr);
56+
System.out.println("Execution time of Selection Sort: " + timer.getTotalTimeInMilis() + "ms.\n");
57+
timer.reset();
58+
59+
//-------Bubble Sort
60+
int[] bubbleSortArr = createArray(len);
61+
System.out.println("Unsorted Array:");
62+
printArray(bubbleSortArr);
63+
System.out.println("Sorted by Bubble Sort:");
64+
timer.start();
65+
SortingEngine.bubbleSort(bubbleSortArr);
66+
timer.stop();
67+
printArray(bubbleSortArr);
68+
System.out.println("Execution time of Bubble Sort: " + timer.getTotalTimeInMilis() + "ms.\n");
69+
timer.reset();
70+
71+
//-------Merge Sort
72+
int[] mergeSortArr = createArray(len);
73+
System.out.println("Unsorted Array:");
74+
printArray(mergeSortArr);
75+
System.out.println("Sorted by Merge Sort:");
76+
timer.start();
77+
SortingEngine.mergeSort(mergeSortArr, 0, len);
78+
timer.stop();
79+
printArray(mergeSortArr);
80+
System.out.println("Execution time of Merge Sort: " + timer.getTotalTimeInMilis() + "ms.\n");
81+
timer.reset();
82+
83+
//-------Quick Sort
84+
int[] quickSortArr = createArray(len);
85+
System.out.println("Unsorted Array:");
86+
printArray(quickSortArr);
87+
System.out.println("Sorted by Quick Sort:");
88+
timer.start();
89+
SortingEngine.quickSort(quickSortArr, 0, len-1);
90+
timer.stop();
91+
printArray(quickSortArr);
92+
System.out.println("Execution time of Quick Sort: " + timer.getTotalTimeInMilis() + "ms.\n");
93+
timer.reset();
94+
95+
//-------Heap Sort
96+
int[] heapSortArr = createArray(len);
97+
System.out.println("Unsorted Array:");
98+
printArray(heapSortArr);
99+
System.out.println("Sorted by Heap Sort:");
100+
timer.start();
101+
SortingEngine.heapSort(heapSortArr);
102+
timer.stop();
103+
printArray(heapSortArr);
104+
System.out.println("Execution time of Heap Sort: " + timer.getTotalTimeInMilis() + "ms.\n");
105+
timer.reset();
106+
}
107+
108+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.ozuduru.implementsorting;
2+
3+
// It is a simple class that keeps start time and total time.
4+
public class MyTimer {
5+
private long startTime, totalTime;
6+
7+
public MyTimer() {
8+
startTime = 0;
9+
totalTime = 0;
10+
}
11+
12+
public void reset() {
13+
startTime = 0;
14+
totalTime = 0;
15+
}
16+
17+
// Set start time.
18+
public void start() {
19+
startTime = System.nanoTime();
20+
}
21+
22+
// Add time difference to totaltime
23+
public void stop() {
24+
long endTime = System.nanoTime();
25+
totalTime += (endTime - startTime);
26+
}
27+
28+
// We keep time as nanoseconds but while we get the total time that function converts miliseconds.
29+
public double getTotalTimeInMilis() {
30+
return totalTime / 1000000.0;
31+
}
32+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package com.ozuduru.implementsorting;
2+
3+
public class SortingEngine {
4+
// Helper method swap
5+
private static void swap(int[] arr, int firstInd, int secondInd) {
6+
if(firstInd < 0 || secondInd < 0)
7+
return;
8+
9+
int len = arr.length;
10+
if(len == 0 || len < firstInd || len < secondInd)
11+
return;
12+
13+
int temp = arr[firstInd];
14+
arr[firstInd] = arr[secondInd];
15+
arr[secondInd] = temp;
16+
}
17+
18+
//-------Insertion Sort
19+
public static void insertionSort(int[] arr) {
20+
int len = arr.length;
21+
22+
for(int i = 1; i < len; ++i) {
23+
for(int j = i; j > 0 && arr[j - 1] > arr[j]; --j)
24+
swap(arr, j, j-1);
25+
}
26+
}
27+
28+
//-------Selection Sort
29+
public static void selectionSort(int[] arr) {
30+
int len = arr.length;
31+
32+
for(int i = 0; i < len - 1; ++i) {
33+
int minInd = i;
34+
35+
for(int j = i + 1; j < len; ++j)
36+
if(arr[j] < arr[minInd])
37+
minInd = j;
38+
39+
if(minInd != i)
40+
swap(arr, i, minInd);
41+
}
42+
}
43+
44+
//-------Bubble Sort
45+
public static void bubbleSort(int[] arr) {
46+
int len = arr.length;
47+
boolean isSwapped = true;
48+
49+
for(int j = 1; isSwapped; ++j) {
50+
isSwapped = false;
51+
52+
for(int i = 0; i < len - j; ++i) {
53+
if(arr[i] > arr[i+1]) {
54+
swap(arr, i, i+1);
55+
isSwapped = true;
56+
}
57+
}
58+
}
59+
}
60+
61+
//-------Merge Sort
62+
public static void mergeSort(int[] arr, int lowInd, int highInd) {
63+
int N = highInd - lowInd;
64+
if(N <= 1)
65+
return;
66+
int midInd = lowInd + N/2;
67+
68+
// Sort
69+
mergeSort(arr, lowInd, midInd);
70+
mergeSort(arr, midInd, highInd);
71+
72+
// Merge
73+
int[] temp = new int[N];
74+
int i = lowInd;
75+
int j = midInd;
76+
77+
for (int k = 0; k < N; ++k) {
78+
if(i == midInd)
79+
temp[k] = arr[j++];
80+
81+
else if(j == highInd)
82+
temp[k] = arr[i++];
83+
84+
else if (arr[j] < arr[i])
85+
temp[k] = arr[j++];
86+
87+
else
88+
temp[k] = arr[i++];
89+
}
90+
91+
for(int k = 0; k < N; k++)
92+
arr[lowInd + k] = temp[k];
93+
}
94+
95+
//-------Quick Sort
96+
public static void quickSort(int[] arr, int lowInd, int highInd) {
97+
int ind = quickHelper(arr, lowInd, highInd);
98+
99+
if(lowInd < ind - 1)
100+
quickSort(arr, lowInd, ind - 1);
101+
if(ind < highInd)
102+
quickSort(arr, ind, highInd);
103+
}
104+
105+
private static int quickHelper(int[] arr, int left, int right) {
106+
int i = left;
107+
int j = right;
108+
int pivot = arr[(left + right) / 2];
109+
110+
while (i <= j) {
111+
while(arr[i] < pivot)
112+
++i;
113+
while(arr[j] > pivot)
114+
--j;
115+
if(i <= j) {
116+
swap(arr, i, j);
117+
++i;
118+
--j;
119+
}
120+
}
121+
return i;
122+
}
123+
124+
//-------Heap Sort
125+
public static void heapSort(int[] arr) {
126+
int count = arr.length;
127+
128+
heapify(arr, count);
129+
int end = count - 1;
130+
131+
while(end > 0) {
132+
swap(arr, 0, end);
133+
siftDown(arr, 0, end - 1);
134+
--end;
135+
}
136+
}
137+
138+
private static void heapify(int[] arr, int count) {
139+
int start = (count - 2) / 2;
140+
while(start >= 0) {
141+
siftDown(arr, start, count - 1);
142+
--start;
143+
}
144+
}
145+
146+
private static void siftDown(int[] arr, int start, int end){
147+
int root = start;
148+
149+
while((root * 2 + 1) <= end) {
150+
int child = root * 2 + 1;
151+
152+
if(child + 1 <= end && arr[child] < arr[child + 1])
153+
child = child + 1;
154+
if(arr[root] < arr[child]){
155+
swap(arr, root, child);
156+
root = child;
157+
}
158+
else
159+
return;
160+
}
161+
}
162+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /