sorting algorithms

Implementations

Selection Sort

img

generally performs worse than the similar insertion sort.

The algorithm divides the input list into two parts: the sublist of items already sorted, which is built up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order) element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element (putting it in sorted order), and moving the sublist boundaries one element to the right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public int[] selectionSort(int[] array) {
if (array == null || array.length == 0)
return new int[0];
for (int i = 0; i < array.length; ++i) {
int minIdx = i;
for (int j = i + 1; j < array.length; ++j) {
if (array[j] < array[minIdx])
minIdx = j;
}
if (minIdx != i) {
int temp = array[i];
array[i] = array[minIdx];
array[minIdx] = temp;
}
}
return array;
}

Insertion Sort

img

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

  • Efficient for (quite) small data sets, much like other quadratic sorting algorithms
  • More efficient in practice than most other simple quadratic algorithms such as selection sort or bubble sort
  • efficient for data sets that are already substantially sorted, the time complexity is O(nk) when each element in the input is no more than k places away from its sorted position
  • stable

Array based

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public int[] insertionSort(int[] array) {
if (array == null || array.length == 0)
return new int[0];
int curr = 0;
while (curr < array.length) {
int idx = curr;
while (idx > 0 && array[idx] < array[idx - 1]) {
int temp = array[idx];
array[idx] = array[idx - 1];
array[idx - 1] = temp;
idx--;
}
curr++;
}
return array;
}

LinkedList based

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public ListNode (ListNode head) {
ListNode dummy = new ListNode(0);
ListNode prev = dummy, curr = head, next = null;
while(curr != null){

next = curr.next;
while(prev.next != null && prev.next.val < curr.val)
prev = prev.next;
// insert curr after prev
curr.next = prev.next;
prev.next = curr;
curr = next;
// reset prev
prev = dummy;
}
return dummy.next;
}

Bubble Sort

img

insertion sort, generally run faster than bubble sort.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static int[] bubbleSort(int[] array) {
if (array == null || array.length == 0)
return new int[0];
// total n-1 loop, each loop find a curr largest
for (int i = 0; i < array.length - 1; ++i) {
for (int j = 0; j < array.length - 1 - i; ++j) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
return array;
}

Merge Sort

img

Quick Sort