-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBubbleSort.java
More file actions
36 lines (34 loc) · 1.19 KB
/
BubbleSort.java
File metadata and controls
36 lines (34 loc) · 1.19 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
/**
* This program bubble sorts an int array
* Created by Devang on 08-Jan-17.
*/
public class BubbleSort {
public static void main(String[] args){
int[] collection = {5, 8, 24, 15, 200, 192, 86, 35, 78, 4 ,9, 20};
printArray(collection);
bubbleSort(collection, 'a');
printArray(collection);
}
private static void bubbleSort(int[] collection, char order){
if (order != 'a' && order != 'd') {
System.out.println("Incorrect order parameter! Use [a or d]");
return;
}
int temp;
for (int i = 0; i < collection.length; i++){
for(int j = 0; j < collection.length - i - 1; j++){
if((order == 'd' && collection[j] < collection[j+1]) || (order == 'a' && collection[j] > collection[j+1])){
temp = collection[j];
collection[j] = collection[j+1];
collection[j+1] = temp;
}
}
}
}
private static void printArray(int[] collection){
for(int i = 0; i < collection.length; i++){
System.out.print("\t" + collection[i]);
}
System.out.println();
}
}