码迷,mamicode.com
首页 > 其他好文 > 详细

Sort

时间:2015-10-05 22:06:30      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

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 = i
   for j = (i,len)
      if less(src[min], src[j]) != 1
	 min = j
   exch(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];
      else
	  brek
  src[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 >= 1
  for i = [h,n)
    for j = i to h reduce h each time
      if 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.

Author: mlhy

Created: 2015-10-05 一 21:51

Emacs 24.5.1 (Org mode 8.2.10)

Sort

标签:

原文地址:http://www.cnblogs.com/mlhy/p/4856289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!