-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble_Sort2.java
More file actions
29 lines (28 loc) · 782 Bytes
/
Bubble_Sort2.java
File metadata and controls
29 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
29
import java.util.*;
public class Bubble_Sort2{
public static void bubblesort(int arr[]){
int n=arr.length;
//loop for number of turns
for(int turn=0;turn<n-1;turn++){
//inner loop for camparisons
for(int j=0;j<n-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] + " ");
}
}
public static void main(String args[]){
int arr[]={3,6,2,1,8,7,4,5,3,1};
bubblesort(arr);
printarr(arr);
}
}