Sorting Algorithm
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的更多相关文章
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
- 中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm
1352: New Sorting Algorithm Time Limit: 1 Sec Memory Limit: 128 MB Description We are trying to use ...
- Bubble sort of sorting algorithm
Bubble sort,It's a relatively basic algorithm.The core implementation ideas are as follows: 1.Define ...
- 排序算法 (sorting algorithm)之 冒泡排序(bubble sort)
http://www.algolist.net/Algorithms/ https://docs.oracle.com/javase/tutorial/collections/algorithms/ ...
- csuoj 1352: New Sorting Algorithm
因为每个元素都是移动到比它小1位的元素的后面: 这样的话以后的一定就可以把他们两个打包: 所以用这种方法最多扫一遍就可以了: 但是最小的那个数要不要移动呢? 如果最小的数后面的数都是升序的,那么一直扫 ...
- 排序算法(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 ...
- 排序算法(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 -> ...
- 中南大学oj:1352: New Sorting Algorithm
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1352 题意:就是要将7 1 5 2这样的序列变成1 2 5 7最少需要多少步?给出变的规律, ...
- 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 ...
随机推荐
- jsp - forward指令
forward指令 既可以指向静态的html页面,也可以转发到动态的jsp页面,并可以保留先前请求的参数. 例如,在web中新建一个Jsp_src.jsp的jsp页面: <%@ page lan ...
- Membership角色与权限管理
安全性:成员资格与角色:验证与授权. 一.建数据库:在VS工具中用DOS环境执行ASPNET_REGSQL 二.配置程序访问数据库: > 在web.config之中加入 <connecti ...
- jQuery分析(1) - 介绍
前言 web技术高速发展到现在已经出了现非常多的库或框架,库或框架实现方法也是五花八门.现在要实现一个web站点只需要根据自己的业务需求选择js框架即可快速完成.有些框架可以快速满足业务需求,但是有些 ...
- Cocos2dx开发(4)——Windows环境创建Cocod2dx 3.2第一个项目HelloWorld
本文内容:cocos2dx+VS2013环境下创建项目,部分代码简析.会的朋友可以略过. 前面简单安装了几个环境,程序完全可以顺利跑起来(其他的cocos-stadio这些需要用到再装) 1.命令行形 ...
- ubuntu 14.04 修改PS1提示符
默认情况下,Ubuntu终端会输出完整路径,在路径名很长的时候,提示方式很不友好,通过以下步骤修改PS1变量的设置,可以让终端输出相对路径.类似于红帽系统的风格.修改思路:将w修改为W显示绝对路径,并 ...
- Spring面试笔记
1. Spring工作机制及为什么要用?Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.Spring既是一个AOP框架,也是一IOC容器.SpringFramework的组成: ...
- phpcms v9栏目列表调用每一篇文章内容方法1
我们先来看下默认栏目调用的代码: 复制代码代码如下:{pc:content action="lists" catid="$catid" num="25 ...
- sublime支持显示中文
Sublime Text 2是一个非常不错的源代码及文本编辑器,但是不支持GB2312和GBK编码在很多情况下会非常麻烦.不过Sublime Package Control所以供的插件可以让Subli ...
- nRF52系列——Get started
Install Jlink Install MDK 这里的使用的是MDK 5.14 Install SDK https://developer.nordicsemi.com/nRF52_SDK/ 这里 ...
- 安装linux系统后要做的事情
基本安装0 http://www.kali.org.cn/thread-20517-1-1.html 基本安装1 http://defcon.cn/1618.html 基本安装2 http://www ...