Skip to content

Commit be28268

Browse files
author
Cody Gagnon
committed
insertSort
1 parent 8a9f696 commit be28268

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

‎class_code/11-22-custom_binary_search/SortingAlgorithms.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,21 @@ public String toString() {
6060
* Sort the array with insert sort
6161
*/
6262
public void insertSort() {
63+
for (int i = 1; i < a.length; i++) {
64+
insert(i);
65+
}
6366
}
6467

6568
/**
6669
* Precondition: a is ordered from 0 to i-1 Postcondition: a is ordered from 0
6770
* to i
6871
*/
6972
private void insert(int i) {
73+
int temp = a[i], j;
74+
for (j = i - 1; j >= 0 && a[j] > temp; j--) {
75+
a[j + 1] = a[j];
76+
}
77+
a[j + 1] = temp;
7078
}
7179

7280
/**

0 commit comments

Comments
 (0)