Last active
December 19, 2016 15:32
-
-
Save vsvankhede/dedcb0021e4b6b39245d05f1d5557182 to your computer and use it in GitHub Desktop.
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
/** | |
* 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; | |
} |
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
/** | |
* 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