sorting 应该是最容易被考到的东西,自己老是学了背,背了忘。为了方便复习,这里进行总结

1. Bubble Sort

定义:每两个两个比较,每扫完一次,当前扫过的最大值放在了末尾。

for i = (n-1) to 1
for j = 0 to i-1
if(A[j] > A[j+1])
swap

  Time Complexity:

Best case : O(n) It can occur if the array is already sorted and no swap occurred.

Worse case: O(n^2)

2. Insertion Sort

定义:当前element 的之前所有elements 都已排好序。把当前element 放进之前排好序的数列中的正确位置。(当前的element从后向前比较)

Insertion sort takes advantage of the presorting. It requires fewer comparision than bubble sort

 for i = 1 to n -1
j = i
while j >0 and A[j] <A[j-1]
swap(A[j], A[j-1])
j --;

  Time Complexity:

Best case : O(n)

Worse case:  O(n ^2)

3. Merge Sort

定义:把一个数组打散看成一个一个的单独的,然后每两个两个组一组,merge,新的组合再两个两个组一组,merge

# C = output [length = N]
# A 1st sorted half [N/2]
# B 2nd sorted half [N/2]
i = j = 1
for k = 1 to n
if A[i] < B[j]
C[k] = A[i]
i++
else
C[k] = B[j]
j++

  

Time Complexity:  O(nlgN)

4. Quick sort

定义: 随机选一个pivot( 当然ideally 是 medium), pivot 的左边全是比自己小的数,右边全是比自己大的数

所以有两个指针,一个指头,一个指尾,第一个指针指向第一个elemnt > pivot 的位置, 第二个指针从后往前,指向第一个element 小于pivot的位置

然后swap。如此扫一遍。然后以pivot为界限,array 分为两部分,再分别选一个pivot,继续上面的过程

Quicksort(A as array, low as int, high as int){
if (low < high){
pivot_location = Partition(A,low,high)
Quicksort(A,low, pivot_location)
Quicksort(A, pivot_location + 1, high)
}
}
Partition(A as array, low as int, high as int){
pivot = A[low]
leftwall = low for i = low + 1 to high{
if (A[i] < pivot) then{
swap(A[i], A[leftwall])
leftwall = leftwall + 1
}
}
swap(A[low],A[leftwall]) return (leftwall)}

  

Time complexity:  O(nlogn)

5. Selection Sort

定义: 选到第一小的,跟第一个element swap, 然后选第二小的,跟第二个element swap

SELECTION-SORT(A)
1. for j ← 1 to n-1
2. smallest ← j
3. for i ← j + 1 to n
4. if A[ i ] < A[ smallest ]
5. smallest ← i
6. Exchange A[ j ] ↔ A[ smallest ]

  

Complexity: O(n^2)

Sorting Algorithm的更多相关文章

  1. 1306. Sorting Algorithm 2016 12 30

    1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...

  2. 中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm

    1352: New Sorting Algorithm Time Limit: 1 Sec  Memory Limit: 128 MB Description We are trying to use ...

  3. Bubble sort of sorting algorithm

    Bubble sort,It's a relatively basic algorithm.The core implementation ideas are as follows: 1.Define ...

  4. 排序算法 (sorting algorithm)之 冒泡排序(bubble sort)

    http://www.algolist.net/Algorithms/ https://docs.oracle.com/javase/tutorial/collections/algorithms/ ...

  5. csuoj 1352: New Sorting Algorithm

    因为每个元素都是移动到比它小1位的元素的后面: 这样的话以后的一定就可以把他们两个打包: 所以用这种方法最多扫一遍就可以了: 但是最小的那个数要不要移动呢? 如果最小的数后面的数都是升序的,那么一直扫 ...

  6. 排序算法(sorting algorithm) 之 选择排序(selection sort)

    https://en.wikipedia.org/wiki/Selection_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 4,6,1,3,7 -> ,3,7 1 ...

  7. 排序算法(sorting algorithm)之 插入排序(insertion sort)

    https://en.wikipedia.org/wiki/Insertion_sort loop1: 4,6,1,3,7 -> 4,6,1,3,7 loop2: 4,6,1,3,7 -> ...

  8. 中南大学oj:1352: New Sorting Algorithm

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1352 题意:就是要将7 1 5 2这样的序列变成1  2  5  7最少需要多少步?给出变的规律, ...

  9. Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

随机推荐

  1. 状态CSS

    <span style="background-color: #999999 !important;color:#FFFFFF;font-size: 11px;height: 18px ...

  2. FTPClient文件下载

    一.FTPClient下载文件所需要的jar包: org.apache.commons.net  [commons-net-3.4.jar] 二.FTPClient的连接和关闭 //FTPClient ...

  3. C++的显示转换

    利用显示转换使得我们可以很容易发现它们,因为通过名字就能找到:  static_cast 用于“良性”和“适度良性”转换,包括不用强制转换  const_cast  对“const”和“volatil ...

  4. (转)应用内存优化之OnLowMemory&OnTrimMemory

    1.应用内存onLowMemory& onTrimMemory优化 onLowMemory& onTrimMemory简介:OnLowMemory是Android提供的API,在系统内 ...

  5. JavaScript 学习笔记之线程异步模型

    核心的javascript程序语言并没有包含任何的线程机制,客户端javascript程序也没有任何关于线程的定义,事件驱动模式下的javascript语言并不能实现同时执行,即不能同时执行两个及以上 ...

  6. js获取时间加多山天和时间戳转换成日期

    function huoqu(){    var data = $("#data").val();//获取的时间    var day = $('#day').val();//往后 ...

  7. PHP实现简易的模板引擎

    PHP实现简易的模板引擎 1.MVC简介 MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式(详情自己百度): 1. Mode ...

  8. window.frameElement属性

    比如有一个iframe的src是xxx.htm frameElement的作用就是在xxx.htm中获得这个引用它的iframe objet 这样你就可以在xxx.htm改变iframe的大小,或是边 ...

  9. swift官方文档中的switch中case let x where x.hasSuffix("pepper")是什么意思?

    在官方文档中,看到这句.但不明白什么意思. let vegetable = "red pepper" switch vegetable { case "celery&qu ...

  10. sourceInsight的技巧

    在用sourceInsight看代码...在这里积累技巧,慢慢积累吧 1.如何高亮显示所有要搜的东西,例如 1.aaaaaa 2. bbbbbbbbaaaaaaa 3. ccccccc 4. aaaa ...