(1) Most efficient sorting algorithm for each situation
-- A large array whose entries are random numbers
Quicksort. considered one of the most efficient sorting, and is in-place
-- A small array of numbers
Insert sorting. stable and in-place
-- A large array of numbers that are already sorted
Heap sort.
-- A large collection of integers that are drawn from a small range
Counting sort
-- A large collection of numbers most of which are duplicates
BST with linked list for elements which have the same key
-- Stabilily is required
Merge sort, efficient and stable
(2) Variable length sort
思路:几乎所有的sorting都依赖于swapping操作,但是,如果交换对象的大小不一样,如果才能顺利进行交换呢?能不能将对象封装成大小一样的item呢?答案是Yes。Build an array P of pointers to the records. Then sort the pointers using the compare function on dereferenced pointers。 Finally, iterate through P writing the deference pointers
(3) Least distance sort.
思路:同上(Indirect sort), sort references to the objects first and then apply the permutation that was applied the references in the end. In this way, each item is moved exactly correct destination exactly once.
(4) BST, Map, Counting sort
Map is implemented by the BST, and counting sort is entailed by the integer range.
(5) Task assignment
compute a 2-partition of A that has min max load
(6) Maximum number of events concurrently
思路:sort the endpoints of events, and count the maximum number of events taking place concurrently
思路:维护一个start和end
思路:类似于merge intervals
(9) Minimum points covering intervals
思路:find the task which ends earliest and remove those tasks which intersect with it. Repeat this process and continue.
(10) Stack sort (increasing)
思路:递归,拿出最顶段的item, insert it into the left sorted items in the stack.
if(S.empty()==false){
T e=S.top();
S.pop();
sort(S);
insert(S, e);
}
void insert(stack<int>& S, int e){
if(S.empty() || S.top()>=e)
S.push(e);
else{
int f=S.top();
S.pop();
insert(S,e);
S.push(f);
}
}