forked from Swatigupta-droid/Basic-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
34 lines (26 loc) · 745 Bytes
/
BubbleSort.java
File metadata and controls
34 lines (26 loc) · 745 Bytes
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
import java.util.Scanner;
class BubbleSortAlgo {
public static void main(String []args) {
int num, i, j, temp;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of integers to sort:");
num = in.nextInt();
int a[] = new int[num];
System.out.println("Enter " + num + " integers: ");
for (i = 0; i < num; i++)
a[i] = in.nextInt();
for (i = 0; i < ( num - 1 ); i++) {
for (j = 0; j < num - i - 1; j++) {
if (a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
System.out.println("Sorted list of integers:");
for (i = 0; i < num; i++)
System.out.println(a[i]);
}
}