- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathbubble_sort.java
More file actions
Latest commit
42 lines (33 loc) · 1.13 KB
/
Copy pathbubble_sort.java
File metadata and controls
42 lines (33 loc) · 1.13 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
importjava.util.Scanner;
classbubble_sort {
publicstaticvoidmain(Stringargs[]) {
intnum;
inttemp = 0;
try (Scanners = newScanner(System.in)) {
System.out.print("ENTER THE NUMBER OF ELEMENTS: ");
num = s.nextInt();
int[] array = newint[num];
System.out.println("ENTER THE ELEMENTS OF ARRAY: ");
for (inti = 0; i < num; i++) {
array[i] = s.nextInt();
}
System.out.println("UNSORTED ARRAY: ");
for (inti = 0; i < num; i++) {
System.out.println(array[i]);
}
for (inti = 0; i < num; i++) {
for (intj = 1; j < (num - i); j++) {
if (array[j - 1] > array[j]) {
temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
System.out.println("SORTED ARRAY: ");
for (inti = 0; i < num; i++) {
System.out.println(array[i]);
}
}
}
}