四种排序:面试基本上有考
#include <iostream>
using namespace std;
void bubbleSort(int *data[],int iSize)
{
if (!data ||1 == iSize) return;
bool bSwap = false;
for (int i = 0;i < iSize;i++)
{
for (int x = 0;x < iSize - 1 - i;i++)
{
if (data[x] > data[x+1])
{
swap(data[x],data[x+1]);
bSwap = true;
}
}
if(!bSwap) break;
}
}
void insertSort(int *data,int iSize)
{
if (!data ||1 == iSize) return;
int iTemp;
int x;
for(int i = 1;i < iSize;i++)
{
iTemp = data[i];
for(x = i;x>0&&iTemp<data[x-1];x--)data[x] = data[x-1];
data[x] = iTemp;
}
}
void selectSort(int *data,int iSize)
{
if (!data ||1 == iSize) return;
int iMin;
for (int i = 0;i < iSize -1;i++)
{
for (int x = 1;x < iSize;x++)
{
if (data[x] < data[iMin])iMin = x;
}
if (i != iMin)swap(data[i],data[iMin]);
}
}
void quickSort(int *data,int iLeft,iRight)
{
if (!data) return;
int i = iLeft,j = iRight;
int iMidData = data[(iLeft+iRight)/2];
while(i <= j)
{
while(iMidData > data[i])i++;
while(iMidData < data[j])j--;
if(i <= j){swap(data[i],swap[j]);i++;j--;}
}
if (i < iRight)quickData(data,i,iRight);
if (j > iLeft)quickData(data,iLeft,j);
}