forked from ranjansharma255/Java_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
40 lines (38 loc) · 809 Bytes
/
BubbleSort.java
File metadata and controls
40 lines (38 loc) · 809 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
34
35
36
37
38
39
40
import java.lang.*;
import java.util.*;
class BubbleSort{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Enter the size of the array");
int size = s.nextInt();
int arr[] = new int[size];
for(int i=0; i<size; i++)
{
arr[i] = s.nextInt();
}
System.out.println("Array Before Sorting");
for(int i=0; i<size; i++)
{
System.out.print(arr[i]+ " ");
}
int temp;
for(int i=0; i<arr.length; i++)
{
for(int j=0; j<arr.length-1-i;j++ )
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println("\nAfter Sorting Array Elements ");
for(int i=0; i<arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}