Wang Dao-replacement selection sorting teaching video of station b
The process of permutation selection sort
Pictures are easier to understand than text, so there is no text explanation.
When the current workspace is full and no suitable elements can be found to put into the current merged segment, start to generate the next merged segment.
Thereafter the above process is repeated.
The computer postgraduate entrance examination may examine the knowledge points of permutation selection sorting
- The number of merged segments = the total number of records / the length of each merged segment [round up]
- Use the loser tree to select minimax
- external sort
PAT Class A 1171 Replacement Selection Solution
topic
Given a sequence of length n and a memory space of size m, the size of each sort cannot exceed the memory space of m, and use the permutation selection algorithm to output one or more ordered sequences
answer
- knowledge points
permutation selection sort
The memory work area WA used for internal sorting can accommodate L records, and each initial merge segment can only contain L records. If the file has n records in total, the number of initial merge segments r=n/L
- problem solving ideas
- Using priority_queue to achieve minimum heap
- Put the number smaller than minimax into the next line for processing
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, m; cin >> n >> m;
vector<int> arr(n);
for(int i = 0; i < n; i++){
cin >> arr[i];
}
priority_queue<int, vector<int>, greater<int>> wa;
vector<int> v, line; // v存放下轮的数,line存放本轮的数
//int wasize = 0; //工作区当前空闲大小
int minimax = INT_MIN;
int index = 0;
//将数先压入工作区
for(; index < m; index++){
wa.push(arr[index]);
}
int count = 0;
//用于统计是否处理完序列中的所有数字
while(count < n){
//因为当前工作区是第一组进入工作区的序列,所以直接处理就可以了
minimax = wa.top();
line.push_back(minimax);
wa.pop();
count++;
if (index < n){
if (arr[index] > minimax) {
wa.push(arr[index++]);
}
else {
v.push_back(arr[index++]);
}
}
if (wa.empty()){
//工作区为空的情况下
//说明第一行已经处理完毕了
for(int i = 0; i < line.size(); i++){
if (i == 0) cout << line[i];
else cout << " " << line[i];
}
cout << '\n';
line.clear();
for(int i = 0; i < v.size(); i++){
wa.push(v[i]);
}
v.clear();
}
}
}