此处讲六种排序,分别是直接插入排序、改进的起泡排序、直接选择排序、快速排序、堆排序、归并排序(还有两种排序方法希尔排序、基数排序)。其中直接接插入排序(O(n2))、改进的起泡排序(O(n2))、归并排序(O(nlogn))、基数排序(LSD)四种为稳定的排序,直接选择排序(O(n2))、快速排序(O(nlogn))、堆排序(O(nlogn))、希尔排序是不稳定的排序。
1、直接插入排序(O(n2)、稳定)
//插入排序
void insertionSort(double list[],int arraySize)
{
for(int i=1;i<arraySize;i++)
{
double currentElement=list[i];
int k;
for(k=i-1;k>=0&&list[k]>currentElement;k--)
{
list[k+1]=list[k];
}
list[k+1]=currentElement;
}
}
2、改进的起泡排序(O(n2)、稳定)
void bubbleSort(double list[],int arraySize)
{
bool needNextPass=true;
for(int k=1;k<arraySize&&needNextPass;k++)
{
needNextPass=false;
for(int i=0;i<arraySize-k;i++)
{
if(list[i]>list[i+1])
{
double temp=list[i];
list[i]=list[i+1];
list[i+1]=temp;
needNextPass=true;
}
}
}
}
3、直接选择排序(O(n2)、不稳定)
//选择排序
void selectionSort(double list[],int arraySize)
{
for(int i=arraySize-1;i>=1;i--)
{
double currentMax=list[0];
int currentMaxIndex=0;
for(int j=1;j<=i;j++)
{
if(currentMax<list[j])
{
currentMax=list[j];
currentMaxIndex=j;
}
}
if(currentMaxIndex!=i)
{
list[currentMaxIndex]=list[i];
list[i]=currentMax;
}
}
}
4、快速排序(O(nlogn)、不稳定)
void quickSort(int list[],int arraySize);
void quickSort(int list[],int first,int last);
int partition(int list[],int first,int last);
void quickSort(int list[],int arraySize)
{
quickSort(list,0,arraySize-1);
}
void quickSort(int list[],int first,int last)
{
if(last>first)
{
int pivotIndex=partition(list,first,last);
quickSort(list,first,pivotIndex-1);
quickSort(list,pivotIndex+1,last);
}
}
int partition(int list[],int first,int last)
{
int pivot=list[first];
int low=first+1;
int high=last;
while(high>low)
{
while(low<=high&&list[low]<=pivot)
low++;
while(low<=high&&list[high]>pivot)
high--;
if(high>low)
{
int temp=list[high];
list[high]=list[low];
list[low]=temp;
}
}
while(high>first&&list[high]>=pivot)
high--;
if(pivot>list[high])
{
list[first]=list[high];
list[high]=pivot;
return high;
}
else
{
return first;
}
}
5、堆排序(O(nlogn)、不稳定)
#ifndef HEAP_H
#define HEAP_H
#include <vector>
#include<stdexcept>
using namespace std;
template<typename T>
class Heap
{
public:
Heap();
Heap(T elements[],int arraySize);
T remove() throw (runtime_error);
void add(T element);
int getSize();
private:
vector<T> v;
};
template<typename T>
Heap<T>::Heap()
{
}
template<typename T>
Heap<T>::Heap(T elements[], int arraySize)
{
for(int i=0;i<arraySize;i++)
{
add(elements[i]);
}
}
template<typename T>
T Heap<T>::remove() throw (runtime_error)
{
if(v.size()==0)
throw runtime_error("runtime_error");
T removedElement=v[0];
v[0]=v[v.size()-1];
v.pop_back();
int currentIndex=0;
while(currentIndex<v.size())
{
int leftChildIndex=2*currentIndex+1;
int rightChildIndex=2*currentIndex+2;
if(leftChildIndex>=v.size())break;
int maxIndex=leftChildIndex;
if(rightChildIndex<v.size())
{
if(v[maxIndex]<v[rightChildIndex])
{
maxIndex=rightChildIndex;
}
}
if(v[currentIndex]<v[maxIndex])
{
T temp=v[maxIndex];
v[maxIndex]=v[currentIndex];
v[currentIndex]=temp;
currentIndex=maxIndex;
}
else
break;
}
return removedElement;
}
template<typename T>
void Heap<T>::add(T element)
{
v.push_back(element);
int currentIndex=v.size()-1;
while(currentIndex>0)
{
int parentIndex=(currentIndex-1)/2;
if(v[currentIndex]>v[parentIndex])
{
T temp=v[currentIndex];
v[currentIndex]=v[parentIndex];
v[parentIndex]=temp;
}
else
break;
currentIndex=parentIndex;
}
}
template<typename T>
int Heap<T>::getSize()
{
return v.size();
}
#endif
6、归并排序(O(nlogn、稳定)
//递归排序
void arraycopy(int source[],int sourceStartIndex,int target[],int targetStartIndex,int length);
void merge(int list1[],int list1Size,int list2[],int list2Size,int temp[]);
void mergeSort(int list[],int arraySize)
{
if(arraySize>1)
{
int *firstHalf=new int[arraySize/2];
arraycopy(list,0,firstHalf,0,arraySize/2);
mergeSort(firstHalf,arraySize/2);
int secondHalfLength=arraySize-arraySize/2;
int *secondHalf=new int[secondHalfLength];
arraycopy(list,arraySize/2,secondHalf,0,secondHalfLength);
mergeSort(secondHalf,secondHalfLength);
int *temp=new int[arraySize];
merge(firstHalf,arraySize/2,secondHalf,secondHalfLength,temp);
arraycopy(temp,0,list,0,arraySize);
delete []temp;
delete []firstHalf;
delete []secondHalf;
}
}
void merge(int list1[],int list1Size,int list2[],int list2Size,int temp[])
{
int current1=0;
int current2=0;
int current3=0;
while(current1<list1Size&¤t2<list2Size)
{
if(list1[current1]<list2[current2])
temp[current3++]=list1[current1++];
else
temp[current3++]=list2[current2++];
}
while(current1<list1Size)
temp[current3++]=list1[current1++];
while(current2<list2Size)
temp[current3++]=list2[current2++];
}
void arraycopy(int source[],int sourceStartIndex,int target[],int targetStartIndex,int length)
{
for(int i=0;i<length;i++)
{
target[i+targetStartIndex]=source[i+sourceStartIndex];
}
}