堆、快排和TopK

网上有写得更好的,仅个人记录

#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;
	}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值