-
Notifications
You must be signed in to change notification settings - Fork 25
/
Bubble_Sort.java
54 lines (48 loc) · 1.27 KB
/
Bubble_Sort.java
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
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
public class Bubble_Sort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
int[] arr = new int[n];
for(int i = 0; i<n; i++){
arr[i] = sc.nextInt();
}
RBubbleSort(arr, n);
print(arr);
}
}
// Iterative
public static void bubbleSort(int[] arr, int n){
for(int i = 0; i<n-1; i++){
for(int j = 0; j<n-1-i; j++){
if( arr[j] > arr[j+1] ) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
// Recurssive
public static void RBubbleSort(int[] arr, int n) {
if( n == 1 ){
return;
}
for(int i = 0; i<n-1; i++){
if( arr[i] > arr[i+1] ){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
bubbleSort(arr, n-1);
}
public static void print(int[] arr){
for(int i : arr){
System.out.print(i + " ");
}
System.out.println();
}
}