-
Notifications
You must be signed in to change notification settings - Fork 368
/
bubble_sort.java
42 lines (33 loc) · 1.13 KB
/
bubble_sort.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
import java.util.Scanner;
class bubble_sort {
public static void main(String args[]) {
int num;
int temp = 0;
try (Scanner s = new Scanner(System.in)) {
System.out.print("ENTER THE NUMBER OF ELEMENTS: ");
num = s.nextInt();
int[] array = new int[num];
System.out.println("ENTER THE ELEMENTS OF ARRAY: ");
for (int i = 0; i < num; i++) {
array[i] = s.nextInt();
}
System.out.println("UNSORTED ARRAY: ");
for (int i = 0; i < num; i++) {
System.out.println(array[i]);
}
for (int i = 0; i < num; i++) {
for (int j = 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 (int i = 0; i < num; i++) {
System.out.println(array[i]);
}
}
}
}