Sort

<?xml version="1.0" encoding="utf-8"?> Sort

Sort

1 Sort

Select sort is the simplest sorting alogrithms.

1.1 IDEA

1.find the smallest element in the rest of array
2.exchange the element with with the i th entry.
3.repeat step1 and step2 util all item is sorted.

1.2 Pseudo Code

for i = [0,len)min = ifor j = (i,len)if less(src[min], src[j]) != 1min = jexch(src,i, min)

1.3 Analysis

1.Selection sort uses pow(n,2)/2 compares and n exchanges.
2.Running time is insensitive to input.No matter the array is in order or not, the running time is direct proportion to pow(n,2)/2.

2 Insertion Sort

2.1 IDEA

This sorting alogrithm is similar to insert cards.Each time, we treat the front array as a in order items.And when a item is coming, we compare it with the front items, if it is smaller than the item compared,the the item compared move back. Until we find the item that is smaller than the coming item, we inset the coming item after that item.
(The algorithm that people often use to sort bridge hands is to con-
sider the cards one at a time, inserting each into its proper place among those already
considered (keeping them sorted). In a computer implementation, we need to make
space to insert the current item by moving larger items one position to the right, before
inserting the current item into the vacated position)

2.2 Pseudo Code

for i = [1,n)key = src[i]for j = [i-1,0]if key < src[j]src[j+1] = src[j];elsebreksrc[k] = key.

2.3 Analysis

Unlike the selection sort, the running time of insertion sort depends on the inital order of the item in the input.
The worst case of insertion sort is pow(n,2)/2 compares and pow(n,2)/2 exchange,but the best case is n-1 compares and no exchange.The worst case and best case can be easily proved.On the average,we can get the formula as fellow:

$$(0+1)/2 + (0+1+2)/3 + ...+(0+1+...+(n-1))/n = n^2/4$$

Insertion sort works well for certain types of nonrandom arrays that often arise in practice, even if they are huge.
Insertion sort is an excellent method for partially sorted arrays and is also a fine method for tiny arrays.

3 Shellsort

Shellsort is a sorting alogrithm based on insertion sort.

3.1 IDEA

In my opinion,shellsort divide the array into **h**th part,first step is to sort each part by using insertion sort,after that,the origin array will become a partially sorted array,then it is suitable for the origin insertion sort to sort it.

3.2 Pseudo Code

while h >= 1for i = [h,n)for j = i to h reduce h each timeif less(src[j], src[j-h])exch(src,j,j-h)h /= k;

3.3 TODO Analysis

4 MERGESORT

4.1 IDEA

The most important idea: combining two ordered arrays to make one larger ordered arrays.
Thus,we have a new question,how can we divide a random-order array into two ordered arrays? We divide each new arrays into two parts util the arrays can't be divide, which only has one elements,and obviously that two arrays which have only one item are an ordered arrays.So, we can slove the problem.

4.2 Pseudo Code

merge(src, lower, mid, upper)for i = [lower, upper]tmp[i] = src[i]i = lowerj = mid+1for k = [lower, upper]if i > mid:  src[k] = tmp[j++]else if j > upper: src[k] = tmp[i++]else if less(src[i], src[j]): src[k] = tmp[i++]else :src[k] = tmp{j++}mergesort(src, lower, upper)if lower < upper:mid = (lower+upper)/2mergesort(src, lower, mid)mergesort(src, mid+1, upper)merge(src, lower, mid, upper)

4.3 Analysis

Mergesort is one of the best-known example of divide-and-conquer paradigm for efficient alogrithm design.
Each time combine two arrays to a larger ordered array, the number of compares is larger than or equal to N/2 and smaller than or equal to N(assume that the number of items that two arrays contain is N).
And T(N) is the running time of mergesort.Then we can have
$$2T(N/2)+N/2 <= T(N) <= 2T(N/2) + N$$
In particulat, when N = pow(2,n),then we have
$$T(2^n) = 2T(2^{n-1}) + 2^n$$
===>$$T(2^n)/2^n = T(2^0)/2^0 + n$$
===>$$T(N) = T(2^n) = n2^n = Nlog_2N$$

4.4 Improvement

1.Use insertion sort for small subarrays.Insertion sort is faster than mergesort for tinny arrays.
2.Test whether the array is already in order.Each time call merge() test wheter src[mid] is smaller than or equal to src[mid+1] or not.
3.Eliminate the copy to the auxiliary array.(I haven't figured out yet)
4.To make aux[] array local to mergesort(), and pass it as an argument to merge().Because no matter how tiny it is, it will still dominate the running time of mergesort.

4.5 Summary

Mergesort is an asymptotically optimal compare-based sorting algorithm.

5 QUICKSORT

5.1 IDEA

1.Select an item to partition an array into two subarrays named subleft and subright, every item in subleft isn't greater than the partitioning item while every item in subright isn't less than the partitioning item.
2.sort subleft and subright
Obviously, for subleft and subright we can handle them just like their parent array.

5.2 Pseudo Code

function partition(src, lower, upper)item = select(src, lower, upper)partition item to a suitable index, src[lower..index-1] <= src[index](=item) <= src[index+1...upper]return indexfunction quicksort(src, lower, upper)index = partition(src, lower, upper)quicksort(src, lower, index-1)quicksort(src, index+1, upper)

5.3 Analysis

We can see the best case of quicksort would be each partitioning stage divides an array in exactly in half. But it's difficult to or hardly receive. On the average, let's assume CN be the average compares of sorting an array which have N items. The we can have,
$$C_N = N+1+(C_0+C_1+...+C_{N-1})/N + (C_{N-1}+...+C_1+C_0)/N$$
Simplification,
$$NC_N = (N+1)N + 2(C_0+C_1+...+C_{N-1})$$
Also,
$$(N-1)C_{N-1} = N(N-1) + 2(C_0+C_1+...+C_{N-2})$$
===>
$$NC_N - (N-1)C_{N-1} = 2N + 2C_{N-1}$$
===>
$$C_N \sim 2(N+1)(\frac{1}{3}+\frac{1}{4}+...+\frac{1}{4})$$
$$lnN = 1+\frac{1}{2}+\frac{1}{3}+...+\frac{1}{N}$$
===>
$$C_N \sim 2NlnN$$
What's the worst case? Obviously, when each partitioning stage select the smaller item as the partitioning item and the item's index is lower index. Then the worst case comes.
$$C_N = N + (N-1)+...+2+1 = (N+1)N/2$$
Through three case, we can learn the most influence factor is the partitioning item/index.

5.4 Improvement

5.4.1 Cutoff to Insertion Sort

As with most recursive alogrithms, it's not a good idea to use quicksort to sort tiny arrays. But obviously, there are always tiny subarrays to sort when we use recursive alogrithms. So, when the subarrays are tiny, we call insertion sort to sort them.

if(upper <= lower + M) 
{insertionsort(src, lower, upper);return;
}

The optimum value of the cutoff M is system-dependent, but any value between 5 and 15 is likely to work well in most situations.

5.4.2 Median-of-three Partitioning

Median-of-three partitioning means find the median of src[lower],src[mid],src[upper] (of course, you can choose other three item). It can eliminate the non randomness in case of avoiding the worst case.

5.4.3 TODO Entropy-optimal sorting

5.5 Summary

In the MERGESORT section, we say "Mergesort is an asymptotically optimal compare-based sorting algorithm". But quicksort is substantially faster than any other sorting method in typical applications. Because Mergesort does not guarantee optimal performance for any given distribution of duplicates.

Author: mlhy

Created: 2015-10-07 三 21:45

Emacs 24.5.1 (Org mode 8.2.10)

转载于:https://www.cnblogs.com/mlhy/p/4856289.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/397439.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

a标签实现不跳转点击

<a class"tiao" href"./index.php"></a> JS实现无跳转a标签 <script type"text/javascript"> $(".tiao").click(function (){return false; }) </script> 转载于:https://www.cnblogs.com/wenhainan/p/…

linux下的c语言控制灯闪烁,C语言实现LED灯闪烁控制

原标题&#xff1a;C语言实现LED灯闪烁控制/********* 配套 **********/#include //包含 寄存器的头文件/****************************************函数功能&#xff1a;延时一段时间*****************************************/void delay(void) //两个void意思分别为无需返回…

VBA and Access

>>.用vba连接ACESS&#xff1a; Set Conn Server.CreateObject("ADODB.Connection") Conn.ConnectionString"ProviderMicrosoft.Jet.OLEDB.4.0;Data Source" & Server.MapPath("sample.mdb") Conn.Open>>.用vba连接EXCEL,打开EX…

温州大学c语言作业布置的网站,老师APP上布置作业 三年级娃为刷排名半夜做题_央广网...

在温州读小学三年级的皮皮(化名)&#xff0c;因为学习需要&#xff0c;在妈妈黄女士的手机里安装了5个APP学习软件。有数学速算的&#xff0c;英语配音的&#xff0c;还有语文复习的。这些软件&#xff0c;都是班上的老师推荐安装的。每天放学回家&#xff0c;皮皮就拿着黄女士…

Algorithm I assignment Collinear

这本来应该是第三周的作业&#xff0c;但是由于其他作业逼近deadline&#xff0c;暂时推后了一周完成。 这周的assignment大大提高了我对这门课的看法&#xff0c;不得不说&#xff0c;Algorithms这门课的assignment部分设计得很好。为什么好&#xff1f;个人认为有以下几点&am…

vc c语言坐标图,VC++6.0下C语言画图编程问题

复制内容到剪贴板代码:#include#includevoid CSinusoidView::OnDraw(CDC* pDC){CSinusoidDoc* pDoc GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data here//建立画笔CPen cpen,pen;pen.CreatePen(PS_SOLID,4,RGB(0,0,0));cpen.CreatePen(PS_SOLID,2…

Java BigDecimal详解

1.引言 float和double类型的主要设计目标是为了科学计算和工程计算。他们执行二进制浮点运算&#xff0c;这是为了在广域数值范围上提供较为精确的快速近似计算而精心设计的。然而&#xff0c;它们没有提供完全精确的结果&#xff0c;所以不应该被用于要求精确结果的场合。但是…

Erlang库 -- 有意思的库汇总

抄自这里 首先&#xff0c;库存在的目的大致可分为&#xff1a;1、提供便利2、尽可能解决一些痛点首先&#xff0c;我们先明确一下Erlang编程语言的一些痛点&#xff08;伪痛点&#xff09;&#xff1a;1&#xff0c;单进程问题Erlang虚拟机属于抢占式调度&#xff0c;抢占式调…

windows 串口编程 c语言,windows下C语言版串口发送程序(基于VS2017)

#include "tchar.h"#include int main(){/*****************************打开串口*************************************/HANDLE hCom;//全局变量&#xff0c;串口句柄hCom CreateFile(_T("COM3"),//COM3口GENERIC_READ | GENERIC_WRITE,//允许读和写0,/…

scikit-learn决策树算法类库使用小结

之前对决策树的算法原理做了总结&#xff0c;包括决策树算法原理(上)和决策树算法原理(下)。今天就从实践的角度来介绍决策树算法&#xff0c;主要是讲解使用scikit-learn来跑决策树算法&#xff0c;结果的可视化以及一些参数调参的关键点。 1. scikit-learn决策树算法类库介绍…

3.js模式-策略模式

1. 策略模式 策略模式定义一系列的算法&#xff0c;把它们封装起来&#xff0c;并且可以互相替换。 var strategies { isNonEmpty: function(value,errMsg){ if(value ){ return errMsg; } }, minLength:function(value,length,errMsg){ if(value.length < length){ retur…

c语言编写程序求8,使用c语言编写程式,实现计算1*2*3+4*5*6+7*8*9+……+28*29*30的值...

使用c语言编写程式&#xff0c;实现计算1*2*34*5*67*8*9……28*29*30的值以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;使用c语言编写程式&#xff0c;实现计算1*2*34*5*67*8*9……28*29*3…

PHP 正则表达式分割 preg_split 与 split 函数

为什么80%的码农都做不了架构师&#xff1f;>>> preg_split() preg_ split() 函数用于正则表达式分割字符串。 语法&#xff1a; array preg_split( string pattern, string subject [, int limit [, int flags]] ) 返回一个数组&#xff0c;包含 subject 中沿着与…

简单学C——第五天

结构体 首先明确&#xff0c;结构体是一种构造的数据类型&#xff0c;是一种由多个数据类型如 int&#xff0c;char&#xff0c;double&#xff0c;数组或者结构体......组成的类型,现在告诉大家如何定义一个结构体。在定义int整型变量时&#xff0c;大家肯定都知道 int a; 即…

C语言二叉树实验报告流程图,二叉树的建立与遍历实验报告(c语言编写,附源代码).doc...

二叉树的建立与遍历实验报告(c语言编写,附源代码).doc第 1 页&#xff0c;共 9 页二叉树的建立与遍历实验报告级 班 年 月 日 姓名 学号_ 1实验题目建立一棵二叉树&#xff0c;并对其进行遍历(先序、中序、后序)&#xff0c;打印输出遍历结果。2需求分析本程序用 VC 编写&#…

三角函数泰勒展开C语言,第六章-函数作业 ---三角函数泰勒级数展开式计算正弦函数值...

E201_06_02_正弦函数题目要求&#xff1a;按照三角函数泰勒级数展开式计算正弦函数值&#xff1a;,直到最后一项的绝对值小于106解题思路&#xff1a;1. 输入弧度2. 确定初始化值3. 求阶梯函数代码&#xff1a;public class E201_06_02_正弦函数 {public static void main(Stri…

Codeforces Round #325 (Div. 2) B. Laurenty and Shop 前缀和

B. Laurenty and Shop Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/problem/BDescription A little boy Laurenty has been playing his favourite game Nota for quite a while and is now very hungry. The boy wants to make sau…

python学习感悟第3节

在继列表的学习之后&#xff0c;进行了元组的学习。元组和列表功能相似&#xff0c;只是元组不能进行修改&#xff0c;所以元组又叫只读列表。 下面列举的是一系列的字符串操作&#xff1a; name.capitalize() #首字母大写 name.count("a") #数列表中有几个a name…

MyBatis_ibatis和mybatis的区别【转】

1. ibatis3.*版本以后正式改名为mybaits&#xff0c;它也从apache转到了google code下&#xff1b;也就是说ibatis2.*&#xff0c;mybatis3.*。2. 映射文件的不同ibatis的配置文件如下<?xml version"1.0" encoding"UTF-8" ?><!DOCTYPE sqlMapCo…

android gallery自动播放,可循环显示图像的Android Gallery组件

类型&#xff1a;源码相关大小&#xff1a;23.6M语言&#xff1a;中文 评分&#xff1a;9.1标签&#xff1a;立即下载第 4 页 实现循环显示图像的Gallery组件实现循环显示图像的Gallery组件在本节将组出与循环显示图像相关的ImageAdapter类的完整代码。读者可以从中看到上一节介…