-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
39 lines (36 loc) · 998 Bytes
/
BubbleSort.java
File metadata and controls
39 lines (36 loc) · 998 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
39
/**
* Java bubble sort of an array of integers.
*
* Goes through the array and swaps the integers if the left number is greater than the right.
* Continues making passes until a pass is made without any swapping.
*
* @author Anthony Reinecker
*/
public class BubbleSort
{
public static void main(String[] args)
{
int[] numbers = {5, 0, -1, 111, -200, 5, 8};
boolean swapped;
int temp;
do
{
swapped = false;
for (int i = 0; i < numbers.length - 1; i++)
{
if (numbers[i] > numbers[i+1])
{
swapped = true;
temp = numbers[i];
numbers[i] = numbers[i+1];
numbers[i+1] = temp;
}
}
} while(swapped);
// Print out numbers in sorted array
for (int i = 0; i < numbers.length; i++)
{
System.out.print(numbers[i]);
}
}
}