Skip to content

Instantly share code, notes, and snippets.

@vsvankhede
Last active December 19, 2016 15:32
Show Gist options
  • Save vsvankhede/dedcb0021e4b6b39245d05f1d5557182 to your computer and use it in GitHub Desktop.
Save vsvankhede/dedcb0021e4b6b39245d05f1d5557182 to your computer and use it in GitHub Desktop.
/**
* Bubble sort algorithm.
* Time complexity: O(n*n)
* Space complexity: O(1)
*/
static int[] bubbleSort(int[] A) {
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length - 1 ; j++) {
if (A[j] > A[j + 1]) {
int temp = A[j + 1];
A[j + 1] = A[j];
A[j] = temp;
}
}
}
return A;
}
/**
* Insertion sort algorithm.
* Time complexity: O(n*n)
* Space complexity: O(1)
*/
static int[] insertionSort(int[] A) {
for(int i = 0; i < A.length; i++) {
int k = A[i];
int j = i - 1;
while((j > -1) && (A[j] > k)) {
A[j + 1] = A[j];
j--;
}
A[j + 1] = k;
}
return A;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment