排序算法

冒泡排序

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

冒泡排序
冒泡排序

冒泡排序可以说是最简单的排序算法。原理就是从数组的最后让最小的数依次排到数组的最前面,时间复杂度为$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);
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272

推荐阅读更多精彩内容