-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.java
33 lines (28 loc) · 1.11 KB
/
BubbleSort.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
package Algorithms.Sorting;
import java.util.Arrays;
/**
* @author Srinvas Vadige, [email protected]
* @since 21 Sept 2024
*/
public class BubbleSort { // compares "adjacent pairs"; j=0 and compare j & j+1 and sort <- from right to left
public static void main(String[] args) {
System.out.println(Arrays.toString(sort(new int[]{3, 2, 4, -1, 1000, 100, 3, 1})));
}
// length-i-1; -1 cause we use both items[j+1] & items[j]
// and -i cause last items already been sorted. So, -i is optional & we add this to make less iterations
public static int[] sort(int[] items){
for(int i=0; i<items.length; i++){
for(int j=0; j<items.length-i-1; j++){
int jItem = items[j];
int jNextItem = items[j+1];
System.out.println(Arrays.toString(items));
if(jNextItem < jItem){ // or keep the current j+1 index in temp and swap at last loop
int temp = items[j];
items[j] = items[j+1];
items[j+1] = temp;
}
}
}
return items;
}
}