-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
56 lines (40 loc) · 1.4 KB
/
BubbleSort.java
File metadata and controls
56 lines (40 loc) · 1.4 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
54
55
56
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args){
//SORTS THE ARRAY USING BUBBLE SORT
//SELECTS THE MAXIMUM ELEMENT FROM THE ARRAY & PUSHES IT TO THE LAST USING ADJACENT SWAPING.
//REPEAT THIS PROCESS TILL THE ARRAY IS SORTED.
Scanner scanner = new Scanner(System.in);
int[] arr;
int[] sortedArr;
int size;
System.out.print("Enter the size of array : ");
size=scanner.nextInt();
arr=new int[size];
sortedArr=new int[size];
System.out.print("\nEnter the array elements : \n");
for(int i=0;i<size;i++){
System.out.printf("Enter element %d : ",i);
arr[i]=scanner.nextInt();
}
System.out.print("\nUnsorted Array is : ");
for(int i=0;i<size;i++){
System.out.print(arr[i] + " ");
}
for(int i=size-1;i>=1;i--){
for(int j=0;j<=i-1;j++){
if(arr[j]>arr[j+1]){
int max=j;
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.print("\nSorted Array is : ");
for(int j=0;j<size;j++){
System.out.print(arr[j] + " ");
}
scanner.close();
}
}