forked from phsestep/ADSB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubble_Insertion_Selection.java
59 lines (54 loc) · 1.28 KB
/
Bubble_Insertion_Selection.java
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
package searchesAndSorts;
public class Bubble_Insertion_Selection {
public static void viewArray(int[] list){
for(int x: list){
System.out.print(x + "\t");
}
System.out.println();
}
public static void selectionSort(int[] list){
for(int pass = 0; pass < list.length; pass++){
int smallest = pass;
for(int i = pass; i < list.length; i++){
if(list[i] < list[smallest]){
smallest = i;
}
}
int temp = list[pass];
list[pass] = list[smallest];
list[smallest] = temp;
}
}
public static void insertionSort(int[] list){
for(int pass = 1; pass < list.length; pass++){
int temp = list[pass];
int i = pass - 1;
while(i >= 0 && list[i] > temp){
list[i+1] = list[i];
i--;
}
list[i+1] = temp;
}
}
public static void bubbleSort(int[] list){
for(int i = 0; i < list.length; i++){
boolean inorder = true;
for(int j = 0; j < list.length - 1 - i; j++){
if(list[j] > list[j+1]){
int temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
inorder = false;
}
}
if(inorder)
return;
}
}
public static void main(String[] args) {
int[] lost = new int[]{2,5,7,1,32,8,0};
viewArray(lost);
insertionSort(lost);
viewArray(lost);
}
}