Bubble sort
1. Compare two values at a time.
2. If the one on the left is bigger, swap them to BUBBLE UP the bigger value to the right.
3. Move one position to the right.
public static void bubbleSort(int[] data) {
for (int out = data.length - 1; out >= 1; out--) {
for (int in = 0; in < out; in++) {
if (data[in] > data[in + 1]) {
swap(data, in, in + 1);
}
}
}
}
// a helper method that swaps two values in an int array
private static void swap(int[] data, int one, int two) {
int temp = data[one];
data[one] = data[two];
data[two] = temp;
}
Result trace: int data[] = { 4, 7, 2, 5, 3 };
First round (out: 4)
4, 7, 2, 5, 3 (in: 0, in+1: 1, swap? N)
4, 2, 7, 5, 3 (in: 1, in+1: 2, swap? Y)
4, 2, 5, 7, 3 (in: 2, in+1: 3, swap? Y)
4, 2, 5, 3, 7 (in: 3, in+1: 4, swap? Y)
Second round (out: 3)
4, 2, 5, 3, 7 (in: 0, in+1: 1, swap? Y)
2, 4, 5, 3, 7 (in: 1, in+1: 2, swap? N)
2, 4, 3, 5, 7 (in: 2, in+1: 3, swap? Y)
Third round (out: 2)
2, 4, 3, 5, 7 (in: 0, in+1: 1, swap? N)
2, 3, 4, 5, 7 (in: 1, in+1: 2, swap? Y)
Fourth round (out: 1)
2, 3, 4, 5, 7 (in: 0, in+1: 1, swap? N)
Time complexity
Comparisons: (n - 1) + (n - 2) + ... + 1 = n * (n - 1) / 2 = n ^ 2 / 2
Swaps: n ^ 2 / 4
Because constants don't count in Big O notation, we can conclude that bubble sort runs in O(n^2) time.
Selection Sort: Faster than Bubble Sort but still not enough
1. Pick or SELECT the minimum value.
2. Swap it with the element on the left end.
int min; // min variable
for (int out = 0; out < data.length - 1; out++) {
min = out; // set initial min values' s index
// select a new minimum value' s index
for (int in = out + 1; in < data.length; in++) {
if (data[in] < data[min]) {
min = in; // reset the new min index
}
}
swap(data, out, min); // time to put min value
}
First round (out: 0, min: 0)
4, 7, 2, 5, 3 (in: 1, min: 0)
4, 7, 2, 5, 3 (in: 2, min: 2)
4, 7, 2, 5, 3 (in: 3, min: 2)
4, 7, 2, 5, 3 (in: 4, min: 2)
swap (0, 2)
2, 7, 4, 5, 3
Second round (out: 1, min: 1)
2, 7, 4, 5, 3 (in: 2, min: 2)
2, 7, 4, 5, 3 (in: 3, min: 2)
2, 7, 4, 5, 3 (in: 4, min: 4)
swap (1, 4)
2, 3, 4, 5, 7
Third round (out: 2, min: 2)
2, 3, 4, 5, 7 (in: 3, min: 2)
2, 3, 4, 5, 7 (in: 4, min: 2)
no swap
2, 3, 4, 5, 7
Fourth round (out: 3, min: 3)
2, 3, 4, 5, 7 (in: 4, min: 3)
no swap
2, 3, 4, 5, 7
Insertion Sort
Most important thing in the insertion sort is that there is an imaginary dividing line.
Left hand side of the line is sorted among themselves.
The first element of the right hand side of the line should be inserted into the left hand side in a proper position
1. First, we keep the value of the first element into a temp place.
2. Shift the items of the left hand side to the right so that there can be a space for the value that is stored in the temp place.
3. When the position is found, INSERT the value into that position.
public static void insertionSort(int[] data) {
// set and increase the dividing line
for (int out = 1; out < data.length; out++) {
int temp = data[out];
int in = out;
// go backward in the left side of the imaginary line to find a place to insert temp value
while (in > 0 && data[in - 1] >= tmp) {
data[in] = data[in - 1];
in--;
}
data[in] = temp; // INSERT the temp value
}
}
Result trace:
First round (out: 1, temp: 7)
4, , 2, 5, 3 (in: 1, in-1: 0, shift?: N)
insert
4, 7, 2, 5, 3
Second round (out: 2, temp: 2)
4, 7, , 5, 3 (in: 2, in-1: 1, shift? Y)
4, , 7, 5, 3 (in: 1, in-1: 0, shift? Y)
, 4, 7, 5, 3
insert
2, 4, 7, 5, 3
Third round (out: 3, temp: 5)
2, 4, 7, , 3 (in: 3, in-1: 2, shift? Y)
2, 4, , 7, 3 (in: 2, in-1: 1, shift? N)
insert
2, 4, 5, 7, 3
Fourth round (out: 4, temp: 3)
2, 4, 5, 7, (in: 4, in-1: 3, shift? Y)
2, 4, 5, , 7 (in: 3, in-1: 2, shift? Y)
2, 4, , 5, 7 (in: 2, in-1: 1, shift? Y)
2, , 4, 5, 7 (in: 1, in-1: 0, shift? N)
insert
2, 3, 4, 5, 7
Time Complexity
These three internal sorting algorithms, bubble sort, selection sort and insertion sort, all run in O(n^2) time in the worst case.
However, often, insertion sort performs better than the other two because it may requires less number of comparisons depending on the input values and uses copying instead of swapping. Copy or shift is more efficient than swap.