-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubble.java
More file actions
38 lines (32 loc) · 818 Bytes
/
Copy pathBubble.java
File metadata and controls
38 lines (32 loc) · 818 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
/*
Project 5-1
Demonstrate the Bubble sort.
*/
class Bubble {
public static void main(String args[]) {
int nums[] = { 99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49 };
int a, b, t;
int size;
size = 10; // number of elements to sort
// display original array
System.out.print("Original array is:");
for(int i=0; i< size; i++)
System.out.print(" " + nums[i]);
System.out.println();
// This is the Bubble sort.
for(a=1; a < size; a++)
for(b=size-1; b >= a; b--) {
if(nums[b-1] > nums[b]) { // if out of order
// exchange elements
t = nums[b-1];
nums[b-1] = nums[b];
nums[b] = t;
}
}
// display sorted array
System.out.print("Sorted array is:");
for(int i=0; i < size; i++)
System.out.prin(" " + nums[i]);
System.out.println();
}
}