forked from recongamer/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble_sort.java
40 lines (33 loc) · 1.2 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
import java.util.Arrays;
import java.util.Scanner;
class bubble {
Scanner input = new Scanner(System.in);
void bubbleSort(int array[]) {
int size = array.length;
System.out.println("Choose Sorting Order:");
System.out.println("1 for Ascending \n2 for Descending");
int sortOrder = input.nextInt();
for (int i = 0; i < size - 1; i++)
for (int j = 0; j < size - i - 1; j++)
if (sortOrder == 1) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
} else {
if (array[j] < array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
public static void main(String args[]) {
int[] data = {-8, 23, 4, 12, -19};
bubble bs = new bubble();
bs.bubbleSort(data);
System.out.println("Sorted Array in Ascending Order:");
System.out.println(Arrays.toString(data));
}
}