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. iOS Core Animation学习总结(1)--CALayer常用属性

    图层是core animation的基础, UIView之所以能显示在屏幕上,靠的是其内部的这个图层,即每个UIView 都有 CALayer,可通过UIView.layer或者[UIView lay ...

  2. 函数还能这样玩儿~实现类似add(1)(2)(3)的函数

    人生的第一份前端工作找到了,感谢大神主子们给半路出家自学的我这么多的机会,很高兴正式踏上客观又乐趣满满的程序员之路,哇咔咔咔. ​ 分享一个准备面试时遇到的一个有趣的问题: 要求实现类似add(1)( ...

  3. new、delete用法(一)

    第一部分:new的使用: #define DEBUG_NEW new(THIS_FILE, __LINE__)解释 THIS_FILE:表示当前类所处的文件名: __LINE__:表示分配内存操作所在 ...

  4. Linux 特殊权限位

    特殊权限位 LINUX 基本权限有9位但是还有三位特殊权限. suid s(有x权限) S(没有x权限) 4 在用户权限的第三位 sgid s(有x权限) S(没有x权限) 2 在用户组权限的第三位 ...

  5. InfoPath本地发布及部署

    前言 最近在接触SharePoint项目,第一次接触,总感觉有些不适应.以前只是听过,现在要遇见了,有些小紧张.今天改了一下表单的东西,也是对sharepoint的慢慢熟悉过程,分享给初学,或者未学者 ...

  6. A transition animation compatible Library.

    Android5.0之后为我们提供了许多炫酷的界面过渡效果,其中共享元素过渡也是很有亮点的一个效果,但这个效果只能在Android5.0之后使用,那今天我们就来将共享元素过渡效果兼容到Android4 ...

  7. apache2.4配置虚拟主机

    step1 启用 httpd-vhosts.conf 找到E:/apache/Apache24/conf 中httpd.conf 文件,取消注释下面这句话 step2 在 httpd-vhosts.c ...

  8. [CSS]cursor鼠标样式

     用css控制鼠标样式的语法如下: <span style="cursor:*">文本或其它页面元素</span>  把 * 换成如下15个效果的一种:   ...

  9. 【pyhton】import math与import cmath

    import math与import cmath分别代表导入math模块和复数math模块 还有一种导入方式是 from math import sqrt 从math中单独导入sqrt 直接可以用sq ...

  10. WPF后台访问XAML元素

    当我们需要从后台访问xaml文件时,我们可以通过这样的方式来操作: private void button1_Click(object sender, RoutedEventArgs e) { Sys ...