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.pswp.cn/news/397439.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/397439.shtml
英文地址,請注明出處:http://en.pswp.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類的完整代碼。讀者可以從中看到上一節介…