数据结构13 Quick Sort&Bucket Sort

Quick Sort

3-mid算法:

Qsort

输入left,right

1.对三个元素:a[left],a[right],a[(left+right)/2]进行排序,如果中间值为a[k],则pivot = a[k]

2.Swap(&a[k],&a[right-1])

3.i从left开始寻找大于pivot的元素,j从right-1寻找小于pivot的元素

4.若i<j,S(&a[i],&a[j]),否则退出

5.Swap(&a[i],&a[right-1])

6.Qsort(left,i)

7.Qsort(i+1,right)

1.a[left] a[left+1] ... a[(left+right)/2] ...a[right-1] a[right] 比较红色的三个元素,并排序,选择中值做为pivot

2.a[left] a[left+1] ... a[(left+right)/2] ...a[right-1] a[right] 交换pivot和a[right-2],这里假设pivot是a[left]:

3.在left到right-1交换,最后交换a[i]和a[right-2](pivot)

最好情况,平均情况:O(NlogN),最坏情况,O(N^2)

比较法排序的最坏情况是O(NlogN)

元素个数较少时考虑插入排序

Bucket Sort 

把对应的元素放置桶中。

复杂度为O(M+N)N个元素,M个桶

Radix Sort

LSD算法

1.建立0-9的桶

2.放置:将元素按照最后一个字符放入桶中

3.收集并更新桶:按照从0到9的桶序把桶中的元素按照第二字符放在对应的桶中(注意桶中是有序的)

4.重复3直到字符排完

MSD算法 

如果两元素关键字相同,则看第二重要关键字,以此类推。

1.先按照最重要的关键词放入桶中

2.在每个桶中按照第二重要的关键词排序

3.重复2直到所有的关键词排完。

 1.To sort N records by quick sort, the worst-case time complexity is O(NlogN).(2分)

最差情况是N^2 F

2.During the sorting, processing every element which is not yet at its final position is called a "run". To sort a list of integers using quick sort, it may reduce the total number of recursions by processing the small partion first in each run.

递归的总数是不变的 F

3.If there are less than 20 inversions in an integer array, the Quick Sort will be the best method among Quick Sort, Heap Sort and Insertion Sort.

小数据用插入最好,F

4.During the sorting, processing every element which is not yet at its final position is called a "run". Which of the following cannot be the result after the second run of quicksort?

A.5, 2, 16, 12, 28, 60, 32, 72

B.2, 16, 5, 28, 12, 60, 32, 72

C.2, 12, 16, 5, 28, 32, 72, 60

D.5, 2, 12, 28, 16, 32, 72, 60

每一个run可以确定一个pivot的位置,两次递归可以确定1+2=3个。

但是要注意,如果在末尾的pivot使得递归只在之前进行,所以也可以。选D

5.Given input { 321, 156, 57, 46, 28, 7, 331, 33, 34, 63 }. Which one of the following is the result after the 2nd run of the Least Signification Digit (LSD) radix sort?

A.→7→321→28→34→33→331→156→46→57→63

B.→7→321→28→331→34→33→46→156→57→63

C.→156→28→321→331→33→34→46→57→63→7

D.→7→321→28→331→33→34→46→156→57→63

第一次按照个位排:321->331->33->63->34->156->46->57->7->28

第二次按照十位排:7->321->28->331->33->34->46->156->57->63

D

6.When running internal sorting, if merge sort is chosen instead of insertion sort, the possible reason should be:

  1. The code of merge sort is shorter
  2. Merge sort takes less space
  3. Merge sort runs faster

A.2 only

B.3 only

C.1 and 2

D.1 and 3

B.merge比insert多了N的空间,1显然不算

7.Among the following sorting methods, which ones will be slowed down if we store the elements in a linked structure instead of a sequential structure?

  1. Insertion sort; 2. Selection Sort; 3. Bubble sort; 4. Shell sort; 5. Heap sort

A.1 and 2 only

B.2 and 3 only

C.3 and 4 only

D.4 and 5 only

如果在链表中存储数据,变慢的操作是:访问第n个元素,变快的是插入

shell需要访问第k个元素,heap需要访问i/2的元素,D

8.When selecting a sorting algorithm, which of the following factors must be taken into consideration besides the time and space complexities?

  • I、the size of input data
  • II、the structure used to store the data
  • III、the stability of the algorithm
  • IV、the initial condition of the data

A.III only

B.I and II only

C.II, III and IV only

D.I, II, III and IV

都需要考虑

9.If quick sort is implemented recursively to sort an array of records, then which one of the following statements is TRUE?

A.After each partition, handling the longer sublist first will reduce the number of recursions.

B.After each partition, handling the shorter sublist first will reduce the number of recursions.

C.The number of recursions has nothing to do with the order of handling the sublists after each partition.

D.The number of recursions has nothing to do with the initial condition of the input.

递归和首先处理哪个子列的顺序无关

10.For the quicksort implementation with the left pointer stops at an element with the same key as the pivot during the partitioning, but the right pointer does not stop in a similar case, what is the running time when all keys are equal?

A.O(logN)

B.O(N)

C.O(NlogN)

D.O(N​2​​)

如果都相同的话:T(N)=T(N-1)+N,解出来是N^2

11.Quick Sort can be implemented by a recursive function void Qsort( ElementType A[ ], int Left, int Right ). If we are to implement the function Qsort() in a non-recursive way with a stack, which of the following should be packed as the elements of the stack?

A.value of pivot

B.index of pivot

C.Left and Right

D.only Left or Right

这道题的意思是说如果用快排递归次数太多,会导致栈溢出。因此我们可以自己维护一个栈。栈顶永远是left,下一个是right,然后按照left和right进行弹出。C

12.Given input { 18, 92, 47, 51, 64, 9, 32 }. After the first partition (with the median-of-three as the pivot) of quick sort, the resulting sequence is ____D

A.{ 18, 9, 32, 51, 64, 92, 47 }

B.{ 18, 9, 47, 32, 51, 92, 64 }

C.{ 9, 18, 47, 51, 64, 92, 32 }

D.{ 18, 9, 32, 92, 64, 47, 51 }

首先对18 51 32排序:

18 92 47 32 64 9 51

将pivot选为32,换到倒数第二个上

18 92 47 9 64 32 51

在18到64进行交换

18 9 47 92 64 32 51

最后i在47,j在9停止,i和32交换

18 9 32 92 64 47 51

D 

内排序是指在排序过程中所有数据都能被放在内存中完成排序。常见的内排序算法包括以下几种: 1. 冒泡排序(Bubble Sort):重复走访要排序的数列,每次比较相邻两个元素,如果它们的顺序错误就把它们交换过来。 2. 选择排序(Selection Sort):每次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。 3. 插入排序(Insertion Sort):将一个记录插入到已经排好序的有序表中,从而得到一个新的、记录数增1的有序表。 4. 希尔排序(Shell Sort):将待排序的数组元素按下标的一定增量分组,对每组使用插入排序算法进行排序,随着增量逐渐减少,每组包含的元素越来越多,当增量减至1时,整个数组恰被分成一组,最后进行插入排序。 5. 快速排序(Quick Sort):通过一趟排序将待排记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,然后分别对这两部分继续进行排序。 6. 归并排序(Merge Sort):将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。 7. 堆排序(Heap Sort):利用堆这种数据结构所设计的一种排序算法。堆排序的过程就是将待排序的序列构造成一个堆,根据堆的性质,最大(或最小)的元素就是堆顶的根节点,将其与堆末尾元素交换,然后将剩余的元素重新构造成一个堆,重复此步骤直至整个序列有序。 8. 计数排序(Counting Sort):根据待排序序列中每个元素在序列中出现的次数,统计出每个元素在最终有序序列中的位置,进而将待排序序列排序。 9. 桶排序(Bucket Sort):将待排序序列中的元素按一定规则分配到不同的桶中,对每个桶中的元素进行排序,最后将所有桶中的元素按顺序合并成有序序列。 10. 基数排序(Radix Sort):将待排序元素按照位数划分为不同的数字,然后按照每个数字的大小依次进行排序,直至所有位数排序完成,最终得到一个有序序列。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值