-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSort.java
More file actions
66 lines (42 loc) · 1.21 KB
/
Copy pathInsertionSort.java
File metadata and controls
66 lines (42 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.*;
public class InsertionSort{
public static void main(String[] args) {
//arr REMEMBER arr IS SEPERATED BY COMMAS
//For WHILE LOOPS BETTER IMPLEMENT IN THE FUNCTION AND CHECK WHETHER OR NOT IT IS LESS THAN 0
//ELSE YOU WILL GET AN ARRAY IS OUT OF BOUNDS EXCEPTION
//
//
//if the array has just one element return that array
//if not then move to the second element
//if the previous element is bigger than the element, then switch them
//if the previous element is smaller then stop
//
//YOUTUBE LINK: https://www.youtube.com/watch?time_continue=3&v=OGzPmgsI-pQ
int arr[] = {4,3,2,1,5};
if(arr.length == 1) {
System.out.println(arr[0]);
return;
}
for(int i = 1; i < arr.length; i++) {
int x = i;
while((arr[x - 1] > arr[x]) && (x > 0)) {
int holder = arr[x - 1];
arr[x - 1] = arr[x];
arr[x] = holder;
--x;
if(x == 0) {
break;
}
}
}
private static void printArray(int[] anArray) {
for (int i = 0; i < anArray.length; i++) {
if (i > 0) {
System.out.print(", ");
}
System.out.print(anArray[i]);
}
System.out.println();
System.out.println();
}
}