-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
43 lines (42 loc) · 1.11 KB
/
Copy pathBubbleSort.java
File metadata and controls
43 lines (42 loc) · 1.11 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
39
40
41
42
43
import java.util.Scanner;
/*
* ---------------
* Time Complexity
* Best case = O(n)
* Worst, Average case = O(n^2)
* ---------------
*/
public class BubbleSort {
public static void main(String []args){
try (Scanner scan = new Scanner(System.in)) {
int size;
System.out.println("Enter the size: ");
size = scan.nextInt();
int arr[] = new int[size];
for(int i=0;i<size;i++){
arr[i] = scan.nextInt();
}
sort(arr,size);
for(int i=0;i<size;i++){
System.out.print(arr[i]+" ");
}
}
}
static void sort(int arr[],int n){
int i, j, temp;
boolean flag = false;
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(arr[j+1]>arr[j]){
temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
flag = true;
}
}
if(!flag){
break;
}
}
}
}