Skip to content

Instantly share code, notes, and snippets.

@andersonmo
Last active November 9, 2021 16:22
Show Gist options
  • Save andersonmo/a024ed226af8ac4a2c52df4e51a83fce to your computer and use it in GitHub Desktop.
Save andersonmo/a024ed226af8ac4a2c52df4e51a83fce to your computer and use it in GitHub Desktop.
A simple way to show three common ways of sorting data using Java. Bubble Sort, Selection Sort and Insertion Sort.
public class CompareSortingAlgorithm {
public static void main(String[] args) {
Long startTime;
Long finishTime;
Long elapsedTime;
Integer[] arrayUnordered = new Integer[] {30, 21, 25, -8, 4, 67, 91, 48};
System.out.println("Original order: "+printArray(arrayUnordered));
printLine();
System.out.println("Starting Booble Sort");
startTime = System.nanoTime();
Integer[] arrayOrdered = bubbleSort(arrayUnordered);
System.out.println(printArray(arrayOrdered));
finishTime = System.nanoTime();
elapsedTime = finishTime - startTime;
System.out.println("Booble Sort: "+elapsedTime);
printLine();
arrayUnordered = new Integer[] {30, 21, 25, -8, 4, 67, 91, 48};
System.out.println("Starting Selection Sort");
startTime = System.nanoTime();
selectionSort(arrayUnordered);
finishTime = System.nanoTime();
elapsedTime = finishTime - startTime;
System.out.println("Selection Sort: "+elapsedTime);
printLine();
arrayUnordered = new Integer[] {30, 21, 25, -8, 4, 67, 91, 48};
System.out.println("Starting Insertion Sort");
startTime = System.nanoTime();
insertionSort(arrayUnordered);
finishTime = System.nanoTime();
elapsedTime = finishTime - startTime;
System.out.println("Insertion Sort: "+elapsedTime);
}
static void printLine() {
System.out.println("-----------------------------------------");
}
static String printArray (Integer[] array) {
String temp;
temp = "{";
for (int i = 0; i < array.length - 1; i++) {
temp += array[i] + ", ";
}
temp += array[(array.length - 1)] + "}";
return temp;
}
static Integer[] bubbleSort(Integer[] array) {
System.out.println(printArray(array));
boolean cont;
int aux;
int size = array.length;
do {
cont = false;
for(int i = 0; i < size-1; i++) {
if(array[i] > array[i+1]) {
aux = array[i];
array[i] = array[i+1];
array[i+1] = aux;
cont = true;
}
}
System.out.println(printArray(array));
size--;
} while (cont);
return array;
}
static void selectionSort(Integer[] array) {
System.out.println(printArray(array));
int small;
int aux;
int size = array.length;
for (int i = 0; i < size-1; i++) {
small = i;
for(int j = i+1; j < size; j++) {
if (array[j] < array[small]) {
small = j;
}
}
if (i != small) {
aux = array[i];
array[i] = array[small];
array[small] = aux;
}
System.out.println(printArray(array));
}
}
static void insertionSort(Integer[] array) {
System.out.println(printArray(array));
int aux, j, i;
int size = array.length;
for (i = 1; i < size; i++) {
aux = array[i];
for (j = i; (j>0) && (aux<array[j-1]); j--) {
array[j] = array[j-1];
}
array[j] = aux;
System.out.println(printArray(array));
}
System.out.println(printArray(array));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment