-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJPQbubbleSort.java
More file actions
53 lines (53 loc) · 1 KB
/
Copy pathJPQbubbleSort.java
File metadata and controls
53 lines (53 loc) · 1 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
43
44
45
46
47
48
49
50
51
52
53
import java.util.Scanner;
public class JPQbubbleSort
{
public static void main(String[] args)
{
Scanner ab = new Scanner(System.in);
System.out.println("Enter the array elements");
int a[] = new int[10];
int b;
for(int i=0;i<a.length;i++)
{
a[i] = ab.nextInt();
}
System.out.println("Enter option for sorting,\n1. Ascending Order\n2. Descending Order");
int sort = new Scanner(System.in).nextInt();
System.out.println("Array before sorting ");
for(int X: a)
{
System.out.print(X+"\t");
}
System.out.println("\nSorted Array is ");
for (int i=0;i<a.length;i++)
{
for(int j=1;j<a.length-i;j++)
{
if(sort==1)
{
if(a[j-1]>a[j])
{
b = a[j-1];
a[j-1] = a[j];
a[j] = b;
}
}
else if (sort==2)
{
if(a[j-1]<a[j])
{
b = a[j-1];
a[j-1] = a[j];
a[j] = b;
}
}
else
{
System.out.println("Entered option is not valid");
}
}
}
for(int Q: a)
System.out.print(Q+"\t");
}
}