-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble_Sort.java
More file actions
28 lines (28 loc) · 782 Bytes
/
Bubble_Sort.java
File metadata and controls
28 lines (28 loc) · 782 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
import java.util.*;
public class Bubble_Sort{
public static void bubblesort(int arr[]){
//outer loop
for(int turn = 0 ; turn<arr.length-1;turn++){
//inner loop
for(int j=0;j<arr.length-1-turn;j++){
if(arr[j]>arr[j+1]){
//swap
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
public static void printArr(int arr[]){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+ " ");
}
System.out.println();
}
public static void main(String args[]){
int arr[] = {5,4,1,3,2};
bubblesort(arr);
printArr(arr);
}
}