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:
- The code of merge sort is shorter
- Merge sort takes less space
- 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?
- 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(N2)
如果都相同的话: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 functionQsort()
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
andRight
D.only
Left
orRight
这道题的意思是说如果用快排递归次数太多,会导致栈溢出。因此我们可以自己维护一个栈。栈顶永远是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