-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
33 lines (27 loc) · 878 Bytes
/
BubbleSort.java
File metadata and controls
33 lines (27 loc) · 878 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
import java.util.Scanner;
class Main
{
public static void main(String []args) {
int n, temp;
Scanner in = new Scanner(System.in);
System.out.print("Enter Total number of elements: ");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers:\n");
for(int i=0; i<n; i++)
array[i] = in.nextInt();
for(int i=0; i<n-1; i++) {
for(j=0; j<n-i-1; j++) {
/* For descending order use < */
if (array[j] > array[j+1]) {
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("Sorted list of numbers:\n");
for(int i=0; i<n; i++)
System.out.println(array[i]);
}
}