This repository was archived by the owner on Feb 18, 2020. It is now read-only.
forked from bliblidotcom/training-java-future-program
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
43 lines (37 loc) · 1.29 KB
/
BubbleSort.java
File metadata and controls
43 lines (37 loc) · 1.29 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
public class BubbleSort {
public BubbleSort(Integer count, Integer[] passedArray){
for(int i = 0; i < count; i++){
for(int j = 0; j < i; j++){
if(passedArray[i] <= passedArray[j]){
////Swapping using multiplication/division - buggy when any item(s) is/are 0
//passedArray[i] = passedArray[i] * passedArray[j];
//passedArray[j] = passedArray[i] / passedArray[j];
//passedArray[i] = passedArray[i] / passedArray[j];
////Swapping using XOR
//passedArray[i] = passedArray[i] ^ passedArray[j];
//passedArray[j] = passedArray[i] ^ passedArray[j];
//passedArray[i] = passedArray[i] ^ passedArray[j];
//Swapping using addition/substraction
passedArray[i] = passedArray[i] + passedArray[j];
passedArray[j] = passedArray[i] - passedArray[j];
passedArray[i] = passedArray[i] - passedArray[j];
}
}
}
for(int i = 0; i < count; i++){
System.out.println(passedArray[i]);
}
}
public static void main(String[] args){
Integer numberOfItems = args.length;
Integer[] inputsToBeSorted = new Integer[numberOfItems];
for(int i = 0; i < numberOfItems; i++){
inputsToBeSorted[i] = Integer.parseInt(args[i]);
}
try {
new BubbleSort(numberOfItems, inputsToBeSorted);
} catch (Exception e) {
System.out.println("Empty argument!");
}
}
}