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

[DS Basics] Sorting

时间:2014-08-21 22:39:14      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:os   io   strong   ar   cti   log   sp   on   

Time complexity:

Binary search O(log2 n):

i=0.   n elements:         -------------------

i=1.   n/2 elements:                   ----------

i=2.   n/4 elements:                          -----

...

i=i.    n/2^i elements:                        -

进行n/2^i=1, i=log2 n 次,来达到只剩一个element.

1,Selection Sort

Steps:

from 0~n-1, select the minimum element[comparing n-1 times], swap it with the A[0].

from 1~n-1, select the minimum element[comparing n-2 times], swap it with the A[1].

...

So total comparison times: n-1+n-2+...+2+1 = n(n-1)/2=O(n^2).

2, Insertion Sort

Steps:

i, compare with j=0~i-1=0, if bigger than A[j], nothing changes; if smaller than A[j], A[j+1] = A[j], move A[i] to position j.

So total comparison times:  1+2+...+n-1=n(n-1)/2=O(n^2).

 

[DS Basics] Sorting,布布扣,bubuko.com

[DS Basics] Sorting

标签:os   io   strong   ar   cti   log   sp   on   

原文地址:http://www.cnblogs.com/chayu3/p/3928167.html

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