-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathbubble_sort.java
More file actions
30 lines (28 loc) · 841 Bytes
/
Copy pathbubble_sort.java
File metadata and controls
30 lines (28 loc) · 841 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
30
import java.util.*;
class BubbleSort {
public static void main(String[] args) {
int arr[] = {4, 12, 23, 1, 90, 1243, 98, 24, 1,56, 124};
bubbleSort(arr);
System.out.println("Array after sorting is: ");
for(int i : arr) {
System.out.print(i + " ");
}
}
public static void bubbleSort(int arr[]) {
int n = arr.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (swapped == false)
break;
}
}
}