forked from CodersForLife/Data-Structures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionsort.java
More file actions
28 lines (20 loc) · 741 Bytes
/
Copy pathinsertionsort.java
File metadata and controls
28 lines (20 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* Created by Jacob Rodgers
10/11/17
*/
public class insertionSort {
public void iSort(int userArray[]){
for (int i = 1; i < userArray.length; i++){
//create an index variable for tracking current number
int index = userArray[i];
//and a second counting variable, j
int j = i - 1;
//Iterate through entire Array comparing each variable to the index assigned above
while ((j >= 0) && (userArray[j] > index)) {
//If there is a number out of order then we will switch them
userArray[j+1] = userArray[j];
j = j - 1;
}
userArray[j+1] = index;
}
}
}