[关闭]
@DingCao-HJJ 2015-09-15T11:15:15.000000Z 字数 3885 阅读 1072

排序算法

算法 排序

冒泡排序

参考资料:
冒泡排序_百度百科

冒泡排序
冒泡排序可以说是最简单的排序算法。原理就是从数组的最后让最小的数依次排到数组的最前面,时间复杂度为O(n2)

算法代码

  1. // BubleSort: a most simple way to sort a series of numbers.
  2. // but not so efficient.
  3. // @Param numbers: the array pointer storing the numbers
  4. // @Param beginning, tail: show the range we need to sort.(begin <= i < end)
  5. void BubleSort(int *numbers, int beginning, int tail) {
  6. for (int i = beginning; i < tail; i++) {
  7. for (int j = tail - 1; j > i; j--) {
  8. if (numbers[j] < numbers[j - 1]) {
  9. int tmp = numbers[j - 1];
  10. numbers[j - 1] = numbers[j];
  11. numbers[j] = tmp;
  12. }
  13. }
  14. }
  15. }

选择排序

参考资料:选择排序_百度百科

选择排序

把数列无序区中最小的一个放到无序区的最前面,从而使无序区的元素逐渐变得有序。时间复杂度也是O(n2)

算法代码:

  1. // SelectionSort: a unstable sorting algorithm.
  2. // @Param numbers: the array pointer storing the numbers
  3. // @Param beginning, tail: show the range we need to sort.(beginning <= i < tail)
  4. void SelectionSort(int* numbers, int beginning, int tail) {
  5. for (int i = beginning; i < tail; i++) {
  6. // suppose the index of the number is i, and the left of i is sorted.
  7. // then find the mininum of the rest and exchange it with numbers[i].
  8. int min = i;
  9. for (int j = i + 1; j < tail; j++) {
  10. if (numbers[j] < numbers[min]) min = j;
  11. }
  12. // exchange. when a smaller number than nubmers[i] is found, exchange them.
  13. if (i != min) {
  14. int temp = numbers[min];
  15. numbers[min] = numbers[i];
  16. numbers[i] = temp;
  17. }
  18. }
  19. }

插入排序

参考资料: 插入败絮_百度百科

将数组中无序的元素插入到有序的元素队列中已完成排序。

算法代码:

  1. // InsertionSort: a stable sorting algorithm that insert a number to the sorted
  2. // sequence till all numbers are sorted.
  3. // @Param numbers: the array pointer storing the numbers
  4. // @Param beginning, tail: show the range we need to sort.(beginning <= i < tail)
  5. void InsertionSort(int* numbers, int beginning, int tail) {
  6. for (int i = beginning, i < tail; i++) {
  7. // insert numbers[j] to certain position
  8. int temp = numbers[i+1];
  9. for (int j = i+1; j > beginning; j--) {
  10. if (numbers[temp] < numbers[j-1]) {
  11. // if j is not the position, move temp to the index before j
  12. // and store the data.
  13. numbers[j] = numbers[j-1];
  14. } else {
  15. numbers[j] = temp; // if j is the position, insert it
  16. break; // and go to insert the next number.
  17. }
  18. }
  19. }
  20. }

快速排序

参考资料:快速排序_百度百科

通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。

算法代码:

  1. // QuickSort.Just to put the numbers smaller than x on the left
  2. // and the bigger on the right.
  3. // @Param numbers: the array pointer storing the numbers
  4. // @Param beginning, tail: show the range we need to sort.(beginning <= i < tail)
  5. void QuickSort(int *numbers, int head, int tail) {
  6. int t, i = head, j = tail, x = numbers[(i + j) / 2];
  7. do {
  8. while (x > numbers[i]) i++;
  9. while (x < numbers[j]) j--;
  10. if (i <= j) {
  11. temp = numbers[i];
  12. numbers[i] = numbers[j];
  13. numbers[j] = temp;
  14. i++; j--;
  15. }
  16. } while (i <= j);
  17. if (i < tail) quick_sort(numbers, i, tail); // sort the left
  18. if (head < j) quick_sort(numbers, head, j); // sort the right
  19. }

堆排序

参考资料:
堆排序_百度百科
堆排序_维基百科

堆排序是和快排、归并排序一样常见的复杂度为O(nlog2n)的算法,速度比较快。
那么,要进行堆排序,首先要把n个数据进行最大堆化(也就是把整个数据整理成一个最大堆)这样子首元素就是数组最大的元素了。把它和最后的元素进行交换,那么就可以得到最后的元素是最大的。如此类推,由于最后一个元素已经是有序的,对前面n-1个元素再进行堆调整。

  1. inline void sort_branch(int nums[], int start, int end) {
  2. // sorts a branch making the maxinum in the brach to the root
  3. // @Param |nums|: the data array regarded as a heap
  4. // @|start|: the beginning index of |nums|
  5. // @|end|: the non-include end index of |nums|
  6. int larger_child; // find the larger child and record the node
  7. // from node(|root|)
  8. // each time we search the larger child for the next step
  9. // loop until we have moved all larger child nodes to the upper node
  10. for (int root = start;
  11. 2 * root + 1 < end;
  12. root = larger_child) {
  13. larger_child = 2 * root + 1; // first dim larger_child as the left_child
  14. if (larger_child < end - 1 && nums[larger_child + 1] > nums[larger_child])
  15. larger_child++;
  16. if (nums[root] < nums[larger_child])
  17. swap(nums[root], nums[larger_child]);
  18. else
  19. break;
  20. }
  21. }
  22. inline void heap_sort(int nums[], int start, int end) {
  23. // sort with a maxinum heap.
  24. // @Param |nums|: the data array regarded as a heap
  25. // @|start|: the beginning index of |nums|
  26. // @|end|: the non-include end index of |nums|
  27. // build up a maxinum heap for the first time
  28. for (int i = end / 2; i >= start; i--) sort_branch(nums, i, end);
  29. // Now, the max number of |nums| between |start| and |end|-1 is |nums[start]|
  30. // for we have built up a maxinum heap. Then swap it with the last number
  31. // so the last number will be the largest.
  32. // Then sort the branch from the root to find the next maxinum number and
  33. // do the same again. Loop until there is only an element left, which means
  34. // we have sorted all elements
  35. for (int j = end - 1; j > start; j--) {
  36. swap(nums[0], nums[j]);
  37. sort_branch(nums, start, j);
  38. }
  39. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注