美文网首页
排序算法

排序算法

作者: 我什么都不知道呀 | 来源:发表于2015-08-29 13:37 被阅读118次

冒泡排序

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

冒泡排序冒泡排序

冒泡排序可以说是最简单的排序算法。原理就是从数组的最后让最小的数依次排到数组的最前面,时间复杂度为$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);
  }
}

相关文章

  • java实现快速排序、归并排序、希尔排序、基数排序算法...

    快速排序算法 归并排序算法 希尔排序算法 基数排序算法

  • web开发需要知道的几个算法

    算法分类 快速排序算法 深度优先算法 广度优先算法 堆排序算法 归并排序算法

  • 算法学习(1)-排序算法

    八大排序算法九大排序算法再总结[经典排序算法][集锦][直观学习排序算法] 视觉直观感受若干常用排序算法 快速排序...

  • 经典排序算法总结

    经典排序算法集锦 冒泡法 排序算法入门之冒泡排序 排序算法入门之冒泡排序优化

  • 前端算法学习-第一篇

    冒泡排序算法 冒泡排序算法是最慢的排序算法之一,也是最容易实现的排序算法。之所以叫冒泡排序是因为使用这种算法排序时...

  • 七大排序算法之冒泡排序

    七大排序算法之冒泡排序 @(算法笔记)[排序算法, 冒泡排序, C++实现] 冒泡排序介绍 冒泡排序是七大排序算法...

  • 算法-选择排序

    算 法:选择排序算法时间复杂度: 选择排序算法概述 选择排序伪代码 选择排序实现 选择排序算法概述 排序算法有许...

  • 浅谈排序算法

    排序算法有很多种,今天先谈谈一些简单的排序算法。包括桶排序、冒泡排序和快速排序算法。后期总结各种排序算法。 桶排序...

  • 线性排序

    桶排序、计数排序、基数排序 一、线性排序算法介绍 1.线性排序算法包括桶排序、计数排序、基数排序。2.线性排序算法...

  • 算法4:插入排序和选择排序算法的比较

    排序算法列表电梯: 选择排序算法:详见 《算法4》2.1 - 选择排序算法(Selection Sort), Py...

网友评论

      本文标题:排序算法

      本文链接:https://www.haomeiwen.com/subject/kivkcttx.html