EPI:Sorting

本文探讨了针对不同情况下的高效排序算法,包括快速排序、插入排序、堆排序、计数排序、基于指针的间接排序等,并介绍了如何在变量长度排序中封装对象以顺利进行交换操作。此外,还涉及了基于二叉搜索树(BST)、映射(Map)和计数排序的实现,以及任务分配、最大并发事件数计算、区间并集、插入区间、最小覆盖点等问题的解决方法。
摘要由CSDN通过智能技术生成

(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


(7) Union of intervals

思路:维护一个start和end


(8) Insert an interval

思路:类似于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);
      }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值