-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathbubble_sort.java
59 lines (47 loc) · 1.85 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
55
56
57
58
59
// Implementation of Bubble sort.
// Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm
// that repeatedly steps through the input list element by element,
// comparing the current element with the one after it, swapping their values if needed.
// These passes through the list are repeated until no swaps had to be performed during a pass,
// meaning that the list has become fully sorted. (Source wiki) https://en.wikipedia.org/wiki/Bubble_sort
// Time Complexity worst-case and average complexity O(n^{2})
// Bubble sort is O(n) on a list that is already sorted i.e. Best case
// Sample Input : [2, 1, 9, 3, 5, 4, 0]
// Output : [0 1 2 3 4 5 9]
package sorting;
public class Bubblesort {
void bubblesort(int[] array) {
int size = array.length;
int temp = 0;
boolean flag = false;
for(int i = 0; i < size; i++) {
for(int j = 1; j < (size - i); j++) {
if(array[j - 1] > array[j]) {
//swap elements
temp = array[j-1];
array[j - 1] = array[j];
array[j] = temp;
flag = true;
}
}
if(!flag) {
System.out.println("Already sorted so no further redundant passes best case 0(n)");
break;
}
}
}
public static void main(String[] args) {
int[] array = {5, 1, 2, 3, 4, 5};
System.out.println("Array Before Bubble Sort");
for (int j : array) {
System.out.print(j + " ");
}
System.out.println();
Bubblesort is = new Bubblesort();
is.bubblesort(array);//sorting array elements using bubble sort
System.out.println("Array After Bubble Sort");
for (int j : array) {
System.out.print(j + " ");
}
}
}