几大常用的排序算法 C++

过两天有某公司的sp免笔试,赶紧复习一些基础内容,今天看了一下常用的排序

冒泡,选择,插入,希尔,归并,计数,快速,桶,基数,堆排序

由于堆排序是在数据结构里面的内容,需要建立堆,而桶排序和基数排序很好理解,就没写,其它的都用C++写了一下

#include <iostream>
#include "vector"
#include "algorithm"
#include "random"

using namespace std;

void quickSort(vector<int>& vec,int left,int right)
{
	if(left>=right)
		return;
	int i = left;
	int j = right;
	int x = vec[left];
	while(i<j)
	{
		while(i < j )
		{
			if(vec[j]>=x)
				j--;
			else
			{
				vec[i]=vec[j];
				i++;
				break;
			}
		}
		while(i<j)
		{
			if(vec[i]<x)
				i++;
			else
			{
				vec[j]=vec[i];
				j--;
				break;
			}
		}			
	}	
	vec[i] = x;
	quickSort(vec,left,i - 1);
	quickSort(vec, i + 1, right);
}


void bubbleSort(vector<int>& vec)
{
	for(int i=0;i<vec.size() - 1;i++)
	{
		for(int j=0;j<vec.size()-i-1;j++)
		{
			if(vec[j]>vec[j+1])
			{
				int temp = vec[j];
				vec[j] = vec[i];
				vec[i] = temp;
			} 
		} 
	} 
}

void selectionSort(vector<int>& vec)
{
	for(int i=0;i<vec.size()-1;i++)
	{
		for(int j=i+1;j<vec.size();j++)
		{
			if(vec[i]>vec[j])
			{
				int temp = vec[i];
				vec[i] = vec[j];
				vec[j] = temp;
			}
		}
	}
}

void insertionSort(vector<int>& vec)
{
	for(int i=1;i<vec.size();i++)
	{
		int temp = vec[i];
		for(int j=i-1;j>-1;j--)
		{
			if(vec[i]>vec[j])
			{
				vec[j+1] = vec[j];
				vec[j] = temp;
			}
			else
			{
				vec[j+1] = temp;
				break;
			}
		}
	}
}

void shellSort(vector<int>& vec)
{
	int gap = vec.size();
	while(gap)
	{
		for(int i=0;i<gap;i++)
		{
			for(int j=i+gap;j<vec.size();j+=gap)
			{
				if(vec[j]<vec[j-gap])
				{
					int temp = vec[j];
					int k = j - gap;
					while( k>=0 && vec[k]>temp)
					{
						vec[k+gap] = vec[k];
						k = k - gap;
					}
					vec[k+gap] = temp;
				}
			}
		}
		gap/=2;
	}
}

void mergePart(vector<int>& vec,int start,int mid,int end)
{
	//cout<<"start:"<<start<<" mid:"<<mid<<" end:"<<end<<endl;
	vector<int> tempVec(vec.begin(),vec.end());
	int tempMid = mid+1;
	int tempIndex = start; 
	while(mid>=start &&end>=tempMid)
	{
		if(vec[tempMid]>=vec[start])
		{
			tempVec[tempIndex] = vec[tempMid];
			tempIndex ++;
			tempMid ++;
		}
		else
		{
			tempVec[tempIndex] = vec[start];
			tempIndex ++;
			start++;
		}
		//cout<<"tempMid:"<<tempMid<<" start:"<<start<<" tempIndex:"<<tempIndex<<endl;
	}
	while(mid>=start)
	{
		tempVec[tempIndex] = vec[start];
		tempIndex ++;
		start ++;
	}
	while(end>=tempMid)
	{
		//cout<<"vec[tempMid]:"<<vec[tempMid]<<endl;
		tempVec[tempIndex] = vec[tempMid];
		tempIndex ++;
		tempMid ++;
	}
	vec = tempVec;
}

void mergeSort(vector<int>& vec,int start,int end)
{
	if(vec.empty() || start>=end)
		return;
	
	int mid = (end+start)/2;
	mergeSort(vec,start,mid);
	mergeSort(vec,mid+1,end);
	mergePart(vec,start,mid,end);
	
}

void countingSort(vector<int>& vec)
{
	int maxElement = *max_element(vec.begin(), vec.end());
	int minElement = *min_element(vec.begin(), vec.end());
	vector<int> countVec(maxElement - minElement + 2);
	for(auto it:vec)
	{
		countVec[it-minElement+1]++; 
	}
	for(int i=0;i<countVec.size()-1;i++)
	{
		countVec[i+1]+=countVec[i];
	}
	vector<int> tempVec(vec.size());
	for(int i=0;i<vec.size();i++)
	{
		int tempIndex = vec[i]-minElement;
		int tempVecIndex = countVec[tempIndex];
		tempVec[tempVecIndex] = vec[i];
		countVec[tempIndex] ++;
	}
	for(int i=0;i<vec.size();i++)
	{
		vec[i] = tempVec[i];
	}
	
}

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
	
	int num[]={25,18,71,32,64,55,48};
	vector<int> vec(num,num+sizeof(num)/sizeof(int));
	for(auto it : vec)
	{
		cout<<it<<" ";
	}
	quickSort(vec,0,vec.size()-1);
	cout<<endl<<endl;
	for(auto it: vec)
	{
		cout<<it<<" ";
	}
	cout<<endl<<endl;
	
	shuffle(vec.begin(),vec.end(),default_random_engine(1));
	for(auto it : vec)
	{
		cout<<it<<" "; 
	}
	cout<<endl<<endl;
	bubbleSort(vec);
	for(auto it : vec)
	{
		cout<<it<<" "; 
	}
	cout<<endl<<endl;
	
	shuffle(vec.begin(),vec.end(),default_random_engine(1));
	for(auto it : vec)
	{
		cout<<it<<" "; 
	}
	cout<<endl<<endl;
	selectionSort(vec);
	for(auto it : vec)
	{
		cout<<it<<" "; 
	}
	cout<<endl<<endl;
	
	cout<<"insertionSort:"<<endl;
	shuffle(vec.begin(),vec.end(),default_random_engine(1));
	for(auto it : vec)
	{
		cout<<it<<" "; 
	}
	cout<<endl<<endl;
	insertionSort(vec);
	for(auto it : vec)
	{
		cout<<it<<" "; 
	}
	cout<<endl<<endl;
	
	cout<<"shellSort:"<<endl;
	shuffle(vec.begin(),vec.end(),default_random_engine(1));
	for(auto it : vec)
	{
		cout<<it<<" "; 
	}
	cout<<endl<<endl;
	shellSort(vec);
	for(auto it : vec)
	{
		cout<<it<<" "; 
	}
	cout<<endl<<endl;
	
	cout<<"mergeSort:"<<endl;
	shuffle(vec.begin(),vec.end(),default_random_engine(1));
	for(auto it : vec)
	{
		cout<<it<<" "; 
	}
	cout<<endl<<endl;
	mergeSort(vec,0,vec.size()-1);
	for(auto it : vec)
	{
		cout<<it<<" "; 
	}
	cout<<endl<<endl;
	
	cout<<"countingSort:"<<endl;
	shuffle(vec.begin(),vec.end(),default_random_engine(1));
	for(auto it : vec)
	{
		cout<<it<<" "; 
	}
	cout<<endl<<endl;
	countingSort(vec);
	for(auto it : vec)
	{
		cout<<it<<" "; 
	}
	cout<<endl<<endl;
	
	return 0;
}

 

点赞