Skip to content

Commit cd88ceb

Browse files
authored
Merge pull request #40 from Sneha2319/main
Added bubble sort .java
2 parents a6815b1 + 76b7047 commit cd88ceb

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

bubblesort.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.util.Scanner;
2+
public class bubblesort{
3+
public static void main(String[] args){
4+
Scanner sc = new Scanner(System.in);
5+
System.out.println("Enter the number of elements");
6+
int n = sc.nextInt();
7+
8+
int[] arr = new int[n];
9+
System.out.println("Enter the elements");
10+
for(int i = 0 ; i<n;i++){
11+
arr[i]=sc.nextInt();
12+
}
13+
for(int i = 0 ; i<n;i++){
14+
for(int j=0;j<n-i-1;j++){
15+
if(arr[j]> arr[j+1]){
16+
int temp = arr[j];
17+
arr[j]= arr[j+1];
18+
arr[j+1]= temp;
19+
}
20+
}
21+
}
22+
System.out.println("sorted array is ");
23+
for(int i = 0;i<n;i++){
24+
System.out.println(arr[i]+" ");
25+
}
26+
}}
27+

0 commit comments

Comments
 (0)