-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
40 lines (33 loc) · 1022 Bytes
/
BubbleSort.java
File metadata and controls
40 lines (33 loc) · 1022 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
31
32
33
34
35
36
37
38
39
40
package org.simplemedium;
import java.util.Arrays;
public class BubbleSort {
public static int[] bubbleSort(int[] inputArr) {
int temp;
int ln = inputArr.length;
boolean swapped = false;
for(int i =0; i<ln-1; i++) {
swapped = false;
for(int j=0; j < ln - 1 - i; j++) {
if(inputArr[j] > inputArr[j+1]) {
// Swap Elements
temp = inputArr[j];
inputArr[j] = inputArr[j+1];
inputArr[j+1] = temp;
swapped = true;
System.out.println(Arrays.toString(inputArr));
}
}
if(!swapped)
break;
}
return inputArr;
}
public static void main(String[] args) {
int[] inputArr = {5,32,8,2,4,1};
int[] outputArr;
outputArr = bubbleSort(inputArr);
for (int i : outputArr) {
System.out.print(i + " ");
}
}
}