void BubbleSort(int A[],int n)
{
for(int i = 0;i<n-1;i++)
{
for(int j = n-1;j>i;j--)
{
if(A[j-1]>A[j])
{
swap(A[j-1],A[j]);
}
}
}
}
快速排序
int Partition(int A[],int low,int high)
{
int pivot = A[low];
while(low<high)
{
while(low<high && A[high]>=pivot)
high--;
A[low] = A[high];
while(low<high && A[low]<=pivot)
low++;
A[high] = A[low];
}
A[low] = pivot;
return low;
}
int Partition2(int A[],int low,int high)
{
int rand_Index = low+rand()%(high-low+1);
swap(A[rand_Index],A[low]);
int pivot = A[low];
while(low<high)
{
while(low<high && A[high]>=pivot)
high--;
A[low] = A[high];
while(low<high && A[low]<=pivot)
low++;
A[high] = A[low];
}
A[low] = pivot;
return low;
}
void QuickSort(int A[],int low,int high)
{
if(low < high)
{
int pivotpos = Partition(A,low,high);
QuickSort(A,low,pivotpos-1);
QuickSort(A,pivotpos+1,high);
}
}