-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubbleSort.java
More file actions
38 lines (38 loc) · 1.26 KB
/
BubbleSort.java
File metadata and controls
38 lines (38 loc) · 1.26 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
//The sorting method for bubble sort
import java.util.*;
class BubbleSort {
void bubbleSort(int[]arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){
temp = arr[j-1]; //Swaping
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
}
void display(int arr[]){
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
}
public static void main(String args[]){
int arr[],l;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the length of the required array: ");
l=sc.nextInt();
arr=new int[l];
for(int i=0;i<arr.length;i++){
System.out.println("Enter the array element: ");
arr[i]=sc.nextInt();
}
BubbleSort ob=new BubbleSort();
System.out.println("Unsorted array:");
ob.display(arr);
ob.bubbleSort(arr);
System.out.println("Sorted Array:");
ob.display(arr);
}
}