-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble.java
47 lines (35 loc) · 1.07 KB
/
bubble.java
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
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int i,j,temp,size;
int arr[] = new int[50];
System.out.print("Array Size : ");
size = scan.nextInt();
System.out.print("Enter Array Elements : ");
for(i=0; i<size; i++)
{
arr[i] = scan.nextInt();
}
int n = arr.length;
for(i=0; i < n; i++)
{
for(j=1; j < (n-i); j++)
{
if(arr[j-1] > arr[j])
{
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
System.out.println("Array After Bubble Sort");
for(i=0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}