Skip to content

Instantly share code, notes, and snippets.

@savioserra
Created March 1, 2018 03:22
Show Gist options
  • Save savioserra/2299f4b1c10c115bb1cd9f16d9173823 to your computer and use it in GitHub Desktop.
Save savioserra/2299f4b1c10c115bb1cd9f16d9173823 to your computer and use it in GitHub Desktop.
Sort Benchmark
package niddho.sorting;
import niddho.sorting.core.Sort;
import java.util.concurrent.ThreadLocalRandom;
public class Main {
private static Integer[] getRandomizedIntegerArray(int arraySize, int randomRange) {
Integer[] array = new Integer[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = ThreadLocalRandom.current().nextInt(randomRange);
}
return array;
}
public static void main(String[] args) {
String[][] results = new String[20][5];
int arraySize = 0;
for (String[] line : results) {
arraySize += 10;
Integer[] array = getRandomizedIntegerArray(arraySize, 1000);
line[0] = String.format("[N = %s]", arraySize);
line[1] = Sort.quickSort(array.clone()).toString();
line[2] = Sort.insertionSort(array.clone()).toString();
line[3] = Sort.bubbleSort(array.clone()).toString();
line[4] = Sort.selectionSort(array.clone()).toString();
}
for (int i = 0; i < results.length; i++) {
System.out.println(String.join(", ", results[i]));
}
}
}
package niddho.sorting.core;
public class Sort {
public static Integer bubbleSort(Comparable[] array) {
int count = 0;
for (int i = 0; i < array.length; i++)
for (int j = 0; j < array.length - 1; j++)
if (array[j].compareTo(array[j + 1]) > 0) {
Comparable aux = array[j];
array[j] = array[j + 1];
array[j + 1] = aux;
count++;
}
return count;
}
public static Integer insertionSort(Comparable[] array) {
int count = 0;
for (int i = 0; i < array.length; i++) {
Comparable aux = array[i];
int index = i;
while (index > 0 && array[index - 1].compareTo(aux) > 0) {
array[index] = array[index - 1];
index--;
count++;
}
array[index] = aux;
}
return count;
}
public static Integer selectionSort(Comparable[] array) {
int count = 0;
for (int i = 0; i < array.length; i++) {
int minorPos = i;
for (int j = (i + 1); j < array.length; j++) {
if (array[j].compareTo(array[minorPos]) < 0) minorPos = j;
}
if (array[i].compareTo(array[minorPos]) != 0) {
Comparable aux = array[i];
array[i] = array[minorPos];
array[minorPos] = aux;
count++;
}
}
return count;
}
public static Integer quickSort(Comparable[] array) {
return quickSort(array, 0, array.length - 1);
}
public static Integer quickSort(Comparable[] array, int left, int right) {
int i = left, j = right, count = 0;
Comparable pivot = array[(left + right) / 2];
Comparable aux;
/* partition */
while (i <= j) {
while (array[i].compareTo(pivot) < 0)
i++;
while (array[j].compareTo(pivot) > 0)
j--;
if (i <= j) {
aux = array[i];
array[i] = array[j];
array[j] = aux;
i++;
j--;
count++;
}
}
/* recursion */
if (left < j)
quickSort(array, left, j);
if (i < right)
quickSort(array, i, right);
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment