When the input is much too large to fit into memory, we have to do external sorting instead of internal sorting. One of the key steps in external sorting is to generate sets of sorted records (also called runs) with limited internal memory. The simplest method is to read as many records as possible into the memory, and sort them internally, then write the resulting run back to some tape. The size of each run is the same as the capacity of the internal memory.算法
Replacement Selection sorting algorithm was described in 1965 by Donald Knuth. Notice that as soon as the first record is written to an output tape, the memory it used becomes available for another record. Assume that we are sorting in ascending order, if the next record is not smaller than the record we have just output, then it can be included in the run.數組
For example, suppose that we have a set of input { 81, 94, 11, 96, 12, 99, 35 }, and our memory can sort 3 records only. By the simplest method we will obtain three runs: { 11, 81, 94 }, { 12, 96, 99 } and { 35 }. According to the replacement selection algorithm, we would read and sort the first 3 records { 81, 94, 11 } and output 11 as the smallest one. Then one space is available so 96 is read in and will join the first run since it is larger than 11. Now we have { 81, 94, 96 }. After 81 is out, 12 comes in but it must belong to the next run since it is smaller than 81. Hence we have { 94, 96, 12 } where 12 will stay since it belongs to the next run. When 94 is out and 99 is in, since 99 is larger than 94, it must belong to the first run. Eventually we will obtain two runs: the first one contains { 11, 81, 94, 96, 99 } and the second one contains { 12, 35 }.測試
Your job is to implement this replacement selection algorithm.this
Each input file contains several test cases. The first line gives two positive integers $N (≤10^5)$ and $M (<N/2)$, which are the total number of records to be sorted, and the capacity of the internal memory. Then N numbers are given in the next line, all in the range of int. All the numbers in a line are separated by a space.spa
For each test case, print in each line a run (in ascending order) generated by the replacement selection algorithm. All the numbers in a line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.code
13 3 81 94 11 96 12 99 17 35 28 58 41 75 15
11 81 94 96 99 12 17 28 35 41 58 75 15
給定一長度爲N的序列,假設如今內存大小爲M(<N/2),那麼如今須要使用外部排序的置換選擇排序算法對利用該內存空間進行排序輸出。blog
其實就是模擬外部排序的過程,題目舉的例子算是說的比較詳細了,模擬的步驟以下:排序
#include<cstdio> #include<vector> #include<queue> #include<unordered_map> using namespace std; priority_queue<int,vector<int>,greater<int> > currentRun;//當前輪次 queue<int> unsorted;//待排序部分 vector<int> nextRun;//暫存下一輪數字 vector<int> result;// 保存每一輪結果 int main(){ int N,M; scanf("%d %d",&N,&M); // 先將前M個數字加入到當前輪次中 for(int i=0;i<M;++i){ int num; scanf("%d",&num); currentRun.push(num); } // 剩餘N-M個數字加入到待排序序列中 for(int i=M;i<N;++i){ int num; scanf("%d",&num); unsorted.push(num); } while(!unsorted.empty()){ // 只要還有數字須要排序 int temp = currentRun.top(); currentRun.pop(); result.push_back(temp); // 出隊未排序序列元素 int num = unsorted.front(); unsorted.pop(); if(num>result[result.size()-1]){ // 比上一次出隊元素大,在當前輪次 currentRun.push(num); }else{ // 在下一輪次 nextRun.push_back(num); } // 判斷當前輪爲空得在當前輪次處理完畢以後才行,不然測試點1錯誤或者測試點2格式錯誤 if(currentRun.empty()) { //當前輪爲空,須要輸出result,並進行下一輪 for (int i = 0; i < result.size(); ++i) { printf("%d", result[i]); if (i < result.size() - 1) printf(" "); } printf("\n"); for (int i:nextRun) { currentRun.push(i); } nextRun.clear(); result.clear(); } } // 須要排序的都已經爲空了,判斷當前輪次還有沒有處理完畢的 if(!currentRun.empty()) { while(!currentRun.empty()){ // 當期輪還有數字沒有輸出 int temp = currentRun.top(); currentRun.pop(); result.push_back(temp); } for(int i=0;i<result.size();++i){ printf("%d",result[i]); if(i<result.size()-1) printf(" "); } printf("\n"); } if(!nextRun.empty()){ // 下一輪不空 for(int i : nextRun){ currentRun.push(i); } // 輸出 while(!currentRun.empty()){ int temp = currentRun.top(); currentRun.pop(); if(currentRun.empty()){ // 最後一個元素 printf("%d",temp); }else{ printf("%d ",temp); } } } return 0; }