-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
32 lines (28 loc) · 919 Bytes
/
BubbleSort.java
File metadata and controls
32 lines (28 loc) · 919 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
public class BubbleSort {
public static void main(String[] args) {
int nums[] = {34, -56, 1013, -1994, 556, 289, 43, -52, 99, 158};
int a, b, t;
// display original array
System.out.print("Original Array: ");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
System.out.println();
// Bubble Sort
for(a = 1; a < nums.length; a++) {
for(b = nums.length - 1; b >= a; b--) {
if(nums[b-1] > nums[b]) {
// exchange elements
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
}
}
}
// display sorted array
System.out.print("Sorted Array: ");
for(int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
}
}