-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshorting_selection_insertion_bubble.java
More file actions
117 lines (85 loc) · 3.26 KB
/
shorting_selection_insertion_bubble.java
File metadata and controls
117 lines (85 loc) · 3.26 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.util.*;
public class shorting_selection_insertion_bubble {
// ******************* bubble sort start ************************************
//compare the adjacent element if larger then swap
public static void bubble(int a[]) {
int i, j, temp, l = a.length;
System.out.println("process of bubble sort : ");
for (i = 0; i <l-1 ; i++) {
for (j = 0; j < l -1 - i; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
for (int k = 0; k < a.length; k++) {
System.out.print(a[k] + ",");
}
System.out.println();
}
}
// ******************* bubble sort end ************************************
// ******************* selection sort start ************************************
//select the smallest in unsorted and add in sorted array using swap the next element of sorted array
public static void selection(int a[]) {
System.out.println("process of selection sort : ");
int i, j, l = a.length;
for (i = 0; i < l; i++) {
int min = Integer.MAX_VALUE, t = 0;
for (j = i; j < l; j++) {
if (min > a[j]) {
min = a[j];
t = j;
}
}
int temp = a[i];
a[i] = min;
a[t] = temp;
for (int k = 0; k < a.length; k++) {
System.out.print(a[k] + ",");
}
System.out.println();
}
}
// ******************* selection sort end ************************************
// ******************* insertion sort start ************************************
//start from index 1 and insert it on the correct position using shift
//*******no use of swap in insertion
public static void insertion(int a[]){
System.out.println("process of insertion sort : ");
int i,l=a.length,temp,j;
for(i=1;i<l;i++){
temp=a[i];
j=i;
while(j>0 && temp<a[j-1]){
a[j]=a[j-1];
j--;
}
a[j]=temp;
for (int k = 0; k < a.length; k++) {
System.out.print(a[k] + ",");
}
System.out.println();
}
}
// ******************* insertion sort end ************************************
// ******************* count sort start ************************************
// ******************* main function start ************************************
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a[] = { 4, 8, 9, 7, 6, 2, 0, 1 };
System.out.print("this is my question : ");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + ",");
}
System.out.println();
/* ************function call here *********************/
insertion(a);
// Arrays.sort(a,0,5,);
System.out.print("this is sorted list : ");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + ",");
}
}
}