-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbleSort.java
More file actions
33 lines (32 loc) · 848 Bytes
/
bubbleSort.java
File metadata and controls
33 lines (32 loc) · 848 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
package sorting;
import java.util.*;
public class bubbleSort {
public static void main(String[] args) {
System.out.println("Enter the no of elements:");
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
System.out.println("Enter the Elements : ");
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
swap(arr);
for(int i=0;i<n;i++){
System.out.print(arr[i]+" ");
}
sc.close();
}
static void swap(int arr[]){
int temp;
int n =arr.length;
for(int i =0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(arr[j]>arr[j+1]){
temp= arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
}