-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheBubbleSort.java
More file actions
45 lines (40 loc) · 1.44 KB
/
Copy pathTheBubbleSort.java
File metadata and controls
45 lines (40 loc) · 1.44 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
44
45
// Bubble Sort : is the simplest sorting algorithm that works by repeatedly
// swapping the adjuscent elements if they are in the wrong order,
// This algorithm is not suitable for large data sets and worst-time complexity is high.
// time complexity = O(n^2) ==> quadratic
import java.util.Arrays;
public class TheBubbleSort {
public static void main(String[] args) {
int[] array = { 64, 34, 25, 12, 22, 11, 90 };
int n = array.length;
bubblesort(array, n);
// Create a copy of the sorted array
int[] SortedArray = Arrays.copyOf(array, n);
for (int i : SortedArray) {
System.out.println(i);
}
System.out.println(array);
}
// An optimized version of bubble sort
static void bubblesort(int[] array, int n) {
int i, j, temp;
boolean swapping;
for (i = 0; i < n - 1; i++) {
swapping = false;
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
// Swap arr[j] and arr[j+1]
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapping = true;
}
}
// If no two elements were
// swapped by inner loop, then break
if (swapping == false) {
break;
}
}
}
}