网上有写得更好的,仅个人记录
#include <iostream>
#include <vector>
#include <cstdlib>
#include <stack>
using namespace std;
//小顶堆,vector实现,更像优先队列
class Heap
{
private:
vector<int> heap;
void shiftUp(int child)
{
while(child/2>0)
{
int parent = child / 2;
int child1 = parent * 2;
int child2 = parent * 2 + 1;
int min = parent;
if(child2<heap.size()&&heap[child2]<heap[min])
{
min = child2;
}
if(heap[child1] < heap[min])
{
min = child1;
}
if(min==parent)
{
break;
}else
{
swap(heap[parent], heap[min]);
child = parent;
}
}
}
void shiftDown(int parent)
{
while (parent *2<heap.size())
{
int child1 = parent * 2;
int child2 = parent * 2 + 1;
int min = parent;
if (child2 < heap.size() && heap[child2] < heap[min])
{
min = child2;
}
if (heap[child1] < heap[min])
{
min = child1;
}
if (min == parent)
{
break;
}
else
{
swap(heap[parent], heap[min]);
parent = min;
}
}
}
public:
Heap()
{
heap = vector<int>(1 );
}
void print()
{
for (int i = 1; i < heap.size();i++)
{
cout << heap[i] << " ";
}
cout << endl;
}
//入堆
void push(int num)
{
heap.push_back(num);
shiftUp(heap.size() - 1);
}
//出堆
int pop()
{
int top = heap[1];
heap[1] = heap[heap.size() - 1];
heap.pop_back();
shiftDown(1);
return top;
}
};
//快排
class QuickSort
{
private:
public:
//递归版本
void quickSort(vector<int>& list,int start,int end)
{
if (start >= end)return;
int r = rand() % (end - start + 1) + start;
swap(list[start], list[r]);
int i = start;
int j = end;
int key = list[start];
while(i<j)
{
//右边搜索
while(i<j&&list[j]>=key)
{
j--;
}
list[i] = list[j];
while(i < j&&list[i]<=key)
{
i++;
}
//左边搜索
list[j] = list[i];
}
//中间位置
list[i] = key;
quickSort(list, start, i - 1);
quickSort(list, i+1, end);
}
//迭代版本
void quickSortIteration(vector<int>& list)
{
stack<pair<int, int>> s;
s.push(pair<int, int>(0,list.size()-1));
while(!s.empty())
{
auto thisPair = s.top();
s.pop();
int start = thisPair.first;
int end = thisPair.second;
if (start >= end)continue;;
int r = rand() % (end - start + 1) + start;
swap(list[start], list[r]);
int i = start;
int j = end;
int key = list[start];
while (i < j)
{
cout << i << " " << j << endl;
cout << list[i] << " " << list[j] << endl;
while (i<j && list[j]>=key)
{
j--;
}
list[i] = list[j];
while (i < j && list[i] <= key)
{
i++;
}
list[j] = list[i];
}
list[i] = key;
s.push(pair<int,int>(start,i-1));
s.push(pair<int, int>(i + 1, end));
}
}
};
//topk问题
class TopK
{
private:
public:
vector<int> divideTopK(vector<int>& list, int k)
{
if (list.size() <= k)return list;
int start = 0;
int end = list.size() - 1;
while(true)
{
int r = rand()%(end-start+1)+start;
swap(list[start], list[r]);
//topk
int i = start;
int j = end;
int key = list[start];
while (i < j)
{
while (i<j && list[j]<=key)
{
j--;
}
list[i] = list[j];
while (i < j && list[i] >= key)
{
i++;
}
list[j] = list[i];
}
list[i] = key;
//至此start~i-1是较大的,i是刚好第i+1大的,i+1~end较小的,0~start必然比start~i-1大
if (i+1 == k||i==k)break;
//如果i还不够大,从i+1~end继续割
if(i<k-1)
{
start = i + 1;
}else//i还不够小
{
end = i - 1;
}
}
//复制数组
vector<int> ans(k);
copy_n(list.begin(), k, ans.begin());
return ans;
}
};