-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1dArrayBubbleSort.java
More file actions
40 lines (37 loc) · 1.07 KB
/
1dArrayBubbleSort.java
File metadata and controls
40 lines (37 loc) · 1.07 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
package com.CodeWithSarnav;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
int i,j,n,a[]=new int[10],temp;
Scanner s=new Scanner(System.in);
System.out.print("Enter size of array: ");
n=s.nextInt();
for(i=0;i<n;i++)
{
System.out.print("Enter element "+(i+1)+": ");
a[i]=s.nextInt();
}
System.out.print("\nGiven list:");
for(i=0;i<n;i++)
System.out.print(" "+a[i]);
for(j=0;j<n-1;j++)
{
for (i=0;i<n-1;i++)
{
if (a[i+1]<a[i])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
System.out.print("\nAscending order:");
for(i=0;i<n;i++)
System.out.print(" "+a[i]);
System.out.print("\nDescending order:");
for(i=n-1;i>=0;i--)
System.out.print(" "+a[i]);
}
}