Skip to content

Commit de71b09

Browse files
committed
Created Java Implementation of Insertion Sort
Basic working Java Insertion Sort
1 parent d8e127c commit de71b09

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Sorting/insertionsort.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Created by Jacob Rodgers
2+
10/11/17
3+
*/
4+
5+
public class insertionSort {
6+
7+
public void iSort(int userArray[]){
8+
for (int i = 1; i < userArray.length; i++){
9+
//create an index variable for tracking current number
10+
int index = userArray[i];
11+
//and a second counting variable, j
12+
int j = i - 1;
13+
14+
//Iterate through entire Array comparing each variable to the index assigned above
15+
while ((j >= 0) && (userArray[j] > index)) {
16+
//If there is a number out of order then we will switch them
17+
userArray[j+1] = userArray[j];
18+
j = j - 1;
19+
}
20+
userArray[j+1] = index;
21+
}
22+
}
23+
}
24+
25+
26+
27+
28+

0 commit comments

Comments
 (0)