Various sorting algorithms 排序算法总结分析

版权声明:转载请注明出处. https://blog.csdn.net/laodaliubei/article/details/84257449

Overview

There exists so many kinds of sorting algorithms,e.g. bubble sort, quick sort, selection sort etc. However, many advanced sort algorithms are used in specific scenarios.

Different sort algorithm may use different datastructures, so it’s necessary to comprehend them, not only for learning algorithm, but also to master queue, stack,map , set etc.

Here, I’d like to show you the algorithm description , coding and testing , as a small but fully equipped project.

Algorithms below included:

  1. bubble sort

0-file I/O

  • If the input.txt file contains whitespace at the end of the file , the last element may be repeated.
  • How to handle the open-file-check code?

??

  • The concept of “stream” is important, stdio/file/string may be stream.

1-bubble sort

animation for bubble

algorithm description

每扫描一趟,将区间中最大的一个移至右侧.

  • 改进:设置flag(sorted=true/false),如果本趟没有进行交换操作,则说明左侧已经是有序序列,无需再扫描.
  • 再改进:记录最后一次交换的位置.
  • 总结:起泡排序无论怎么改进,最坏情况依然是O(n2).
    最好情况(sorted): O(n), 平均复杂度O(n2), 低于O(nlogn)这一基于比较的排序算法下限.
    空间复杂度: O(1)

也可以从大到小排序,也可以从后向前冒泡。其特征操作是相邻元素的比较和交换

猜你喜欢

转载自blog.csdn.net/laodaliubei/article/details/84257449