首页 > 其他 > 详细

[DS Basics] Sorting

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!