forked from pandeyprem/DSA-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_asad.java
More file actions
32 lines (26 loc) · 839 Bytes
/
bubble_asad.java
File metadata and controls
32 lines (26 loc) · 839 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
import java.util.Scanner;
public class BubbleSort {
public static void main(String []args) {
int i,j,temp,limit;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the limit of the numbers:");
limit = sc.nextInt();
int array[] = new int[limit];
System.out.println("Enter " + limit + " numbers: ");
for (i = 0; i <limit; i++)
array[i] = sc.nextInt();
for (i = 0; i < ( limit - 1 ); i++) {
for (j = 0; j < limit - i - 1; j++) {
if (array[j] > array[j+1]) //swap the elements if first one is greater than second
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("******Sorted list******");
for (i = 0; i < limit; i++)
System.out.println(array[i]);
}
}