Created
August 5, 2016 18:30
-
-
Save tareq-si-salem/fb1fe97dcf45189386f0e92aaa5e46dd to your computer and use it in GitHub Desktop.
Comparing between Merge Sort, Heap Sort, Bubble Sort, Bin Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Main { | |
public static void main(String[] args) { | |
// testing if the four algorithms are correct | |
int length = 10; | |
int max = 100; | |
int min = 0; | |
int[] a = new int[length]; | |
int[] b = new int[length]; | |
int[] c = new int[length]; | |
int[] d = new int[length]; | |
generateArrays(a, b, c, d, min, max); | |
System.out.println("Unsorted: " + Arrays.toString(a)); | |
Algorithms.bubbleSort(a); | |
System.out.println("Sorted: " + Arrays.toString(a)); | |
Algorithms.mergeSort(b); | |
System.out.println("Sorted: " + Arrays.toString(b)); | |
Algorithms.binSort(c); | |
System.out.println("Sorted: " + Arrays.toString(c)); | |
Algorithms.heapSort(d); | |
System.out.println("Sorted: " + Arrays.toString(d)); | |
// comparing between the algorithms | |
long time; | |
System.out.println("List Size\tBubbleSort\tMergeSort\tHeapSort\tBinSort"); | |
for (int i = 1; i <= 10; i++) { | |
length = 10000 * i; | |
a = new int[length]; | |
b = new int[length]; | |
c = new int[length]; | |
d = new int[length]; | |
Algorithms.heapSort(d); | |
System.out.println(Arrays.toString(d)); | |
generateArrays(a, b, c, d, 0, 100); | |
System.out.print(length + "\t\t"); | |
time = System.currentTimeMillis(); | |
Algorithms.bubbleSort(a); | |
time = System.currentTimeMillis() - time; | |
System.out.print(time + "ms\t\t"); | |
time = System.currentTimeMillis(); | |
Algorithms.mergeSort(b); | |
time = System.currentTimeMillis() - time; | |
System.out.print(time + "ms\t\t"); | |
time = System.currentTimeMillis(); | |
Algorithms.mergeSort(c); | |
time = System.currentTimeMillis() - time; | |
System.out.print(time + "ms\t\t"); | |
time = System.currentTimeMillis(); | |
Algorithms.binSort(d); | |
time = System.currentTimeMillis() - time; | |
System.out.print(time + "ms\t\t"); | |
System.out.println(""); | |
} | |
} | |
public static void generateArrays(int[] a, int[] b, int[] c, int[] d, int min, int max) { | |
for (int i = 0; i < a.length; i++) { | |
a[i] = b[i] = c[i] = d[i] = (int) (min + Math.random() * (max - min)); | |
} | |
} | |
} | |
class Algorithms { | |
public static void mergeSort(int[] elements) { | |
divide(elements); | |
} | |
private static void divide(int[] elements) { | |
if (elements.length != 1) { | |
int midLength = elements.length / 2; | |
int[] leftHalf = new int[midLength]; | |
int[] rightHalf = new int[elements.length - midLength]; | |
System.arraycopy(elements, 0, leftHalf, 0, leftHalf.length); | |
System.arraycopy(elements, leftHalf.length, rightHalf, 0, rightHalf.length); | |
divide(leftHalf); | |
divide(rightHalf); | |
merge(elements, leftHalf, rightHalf); | |
} | |
} | |
private static void merge(int[] elements, int[] leftHalf, int[] rightHalf) { | |
int leftIndex = 0; | |
int rightIndex = 0; | |
int mergeIndex = 0; | |
while (leftIndex < leftHalf.length && rightIndex < rightHalf.length) { | |
if (leftHalf[leftIndex] < rightHalf[rightIndex]) { | |
elements[mergeIndex++] = leftHalf[leftIndex++]; | |
} else { | |
elements[mergeIndex++] = rightHalf[rightIndex++]; | |
} | |
} | |
while (leftIndex < leftHalf.length) { | |
elements[mergeIndex++] = leftHalf[leftIndex++]; | |
} | |
while (rightIndex < rightHalf.length) { | |
elements[mergeIndex++] = rightHalf[rightIndex++]; | |
} | |
} | |
public static void bubbleSort(int[] elements) { | |
int finish = elements.length - 1; | |
boolean swap = true; | |
while (swap) { | |
swap = false; | |
for (int i = 0; i < finish; i++) { | |
if (elements[i] > elements[i + 1]) { | |
int temp = elements[i]; | |
elements[i] = elements[i + 1]; | |
elements[i + 1] = temp; | |
swap = true; | |
} | |
} | |
finish--; | |
} | |
} | |
public static void binSort(int[] elements) { | |
int values[] = new int[100]; | |
for (int i = 0; i < elements.length; i++) { | |
values[elements[i]]++; | |
} | |
int index = 0; | |
for (int i = 0; i < values.length; i++) { | |
for (int j = 0; j < values[i]; j++) { | |
elements[index++] = i; | |
} | |
} | |
} | |
public static void heapSort(int[] elements) { | |
int n = elements.length; | |
for (int i = n / 2 - 1; i >= 0; i--) { | |
partialOrderHeap(elements, i, n - 1); | |
} | |
// a[0] = min(a); | |
for (int k = n - 1; k > 0; k--) { | |
swap(elements, 0, k); | |
partialOrderHeap(elements, 0, k - 1); | |
} | |
// sorted in increasing order (flip it) O(n) | |
for (int i = 0; i < elements.length / 2; i++) { | |
swap(elements, i, elements.length - 1 - i); | |
} | |
} | |
private static void partialOrderHeap(int[] elements, int start, int last) { | |
int pos = start; | |
while (pos < last / 2) { | |
if (pos * 2 + 1 == last) { // one child | |
if (elements[pos] > elements[last]) { | |
swap(elements, pos, last); | |
} | |
break; | |
} else { // two or more descendents | |
int minIndex = min(elements, pos); | |
if (elements[pos] > elements[minIndex]) { | |
swap(elements, pos, minIndex); | |
pos = minIndex; | |
} else { | |
break; | |
} | |
} | |
} | |
} | |
private static void swap(int[] elements, int pos, int last) { | |
int temp = elements[pos]; | |
elements[pos] = elements[last]; | |
elements[last] = temp; | |
} | |
private static int min(int[] elements, int pos) { | |
int left = 2 * pos + 1; | |
int right = 2 * pos + 2; | |
if (elements[left] < elements[right]) { | |
return left; | |
} | |
return right; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment