冒泡排序
參考資料:
冒泡排序_百度百科

冒泡排序
冒泡排序可以說是最簡單的排序算法。原理就是從數組的最后讓最小的數依次排到數組的最前面,時間復雜度為$O(n^2)$。
算法代碼
// BubleSort: a most simple way to sort a series of numbers.
// but not so efficient.
// @Param numbers: the array pointer storing the numbers
// @Param beginning, tail: show the range we need to sort.(begin <= i < end)
void BubleSort(int *numbers, int beginning, int tail) {
for (int i = beginning; i < tail; i++) {
for (int j = tail - 1; j > i; j--) {
if (numbers[j] < numbers[j - 1]) {
int tmp = numbers[j - 1];
numbers[j - 1] = numbers[j];
numbers[j] = tmp;
}
}
}
}
選擇排序
參考資料:選擇排序_百度百科

選擇排序
把數列無序區中最小的一個放到無序區的最前面,從而使無序區的元素逐漸變得有序。時間復雜度也是$O(n^2)$。
算法代碼:
// SelectionSort: a unstable sorting algorithm.
// @Param numbers: the array pointer storing the numbers
// @Param beginning, tail: show the range we need to sort.(beginning <= i < tail)
void SelectionSort(int* numbers, int beginning, int tail) {
for (int i = beginning; i < tail; i++) {
// suppose the index of the number is i, and the left of i is sorted.
// then find the mininum of the rest and exchange it with numbers[i].
int min = i;
for (int j = i + 1; j < tail; j++) {
if (numbers[j] < numbers[min]) min = j;
}
// exchange. when a smaller number than nubmers[i] is found, exchange them.
if (i != min) {
int temp = numbers[min];
numbers[min] = numbers[i];
numbers[i] = temp;
}
}
}
插入排序
參考資料: 插入敗絮_百度百科
將數組中無序的元素插入到有序的元素隊列中已完成排序。
算法代碼:
// InsertionSort: a stable sorting algorithm that insert a number to the sorted
// sequence till all numbers are sorted.
// @Param numbers: the array pointer storing the numbers
// @Param beginning, tail: show the range we need to sort.(beginning <= i < tail)
void InsertionSort(int* numbers, int beginning, int tail) {
for (int i = beginning, i < tail; i++) {
// insert numbers[j] to certain position
int temp = numbers[i+1];
for (int j = i+1; j > beginning; j--) {
if (numbers[temp] < numbers[j-1]) {
// if j is not the position, move temp to the index before j
// and store the data.
numbers[j] = numbers[j-1];
} else {
numbers[j] = temp; // if j is the position, insert it
break; // and go to insert the next number.
}
}
}
}
快速排序
參考資料:快速排序_百度百科
通過一趟排序將要排序的數據分割成獨立的兩部分,其中一部分的所有數據都比另外一部分的所有數據都要小,然后再按此方法對這兩部分數據分別進行快速排序,整個排序過程可以遞歸進行,以此達到整個數據變成有序序列。
算法代碼:
// QuickSort.Just to put the numbers smaller than x on the left
// and the bigger on the right.
// @Param numbers: the array pointer storing the numbers
// @Param beginning, tail: show the range we need to sort.(beginning <= i < tail)
void QuickSort(int *numbers, int head, int tail) {
int t, i = head, j = tail, x = numbers[(i + j) / 2];
do {
while (x > numbers[i]) i++;
while (x < numbers[j]) j--;
if (i <= j) {
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
i++; j--;
}
} while (i <= j);
if (i < tail) quick_sort(numbers, i, tail); // sort the left
if (head < j) quick_sort(numbers, head, j); // sort the right
}
堆排序
堆排序是和快排、歸并排序一樣常見的復雜度為$O(nlog_2n)$的算法,速度比較快。
那么,要進行堆排序,首先要把n個數據進行最大堆化(也就是把整個數據整理成一個最大堆)這樣子首元素就是數組最大的元素了。把它和最后的元素進行交換,那么就可以得到最后的元素是最大的。如此類推,由于最后一個元素已經是有序的,對前面n-1個元素再進行堆調整。
inline void sort_branch(int nums[], int start, int end) {
// sorts a branch making the maxinum in the brach to the root
// @Param |nums|: the data array regarded as a heap
// @|start|: the beginning index of |nums|
// @|end|: the non-include end index of |nums|
int larger_child; // find the larger child and record the node
// from node(|root|)
// each time we search the larger child for the next step
// loop until we have moved all larger child nodes to the upper node
for (int root = start;
2 * root + 1 < end;
root = larger_child) {
larger_child = 2 * root + 1; // first dim larger_child as the left_child
if (larger_child < end - 1 && nums[larger_child + 1] > nums[larger_child])
larger_child++;
if (nums[root] < nums[larger_child])
swap(nums[root], nums[larger_child]);
else
break;
}
}
inline void heap_sort(int nums[], int start, int end) {
// sort with a maxinum heap.
// @Param |nums|: the data array regarded as a heap
// @|start|: the beginning index of |nums|
// @|end|: the non-include end index of |nums|
// build up a maxinum heap for the first time
for (int i = end / 2; i >= start; i--) sort_branch(nums, i, end);
// Now, the max number of |nums| between |start| and |end|-1 is |nums[start]|
// for we have built up a maxinum heap. Then swap it with the last number
// so the last number will be the largest.
// Then sort the branch from the root to find the next maxinum number and
// do the same again. Loop until there is only an element left, which means
// we have sorted all elements
for (int j = end - 1; j > start; j--) {
swap(nums[0], nums[j]);
sort_branch(nums, start, j);
}
}