统计冒泡排序与快速排序交换次数,题目SDUTOJ
#include<iostream>
#define N 100000
using namespace std;
int count=0;
int count2=0;
void swap(int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
void QuickSort(int a[],int s,int e)
{
if(s>=e) //数组头和数组尾
return;
int k=a[s];
int i=s,j=e;
while(i!=j)
{
while(j>i&&a[j]>=k)
--j;
if(a[i]!=a[j])
{
swap(a[i],a[j]);
count++;
}
while(i<j&&a[i]<=k)
++i;
if(a[i]!=a[j])
{
swap(a[i],a[j]);
count++;
}
}
QuickSort(a,s,i-1);
QuickSort(a,i+1,e);
}
void Bubble_Sort(int a[],int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1;j++)
{
if(a[j+1]<a[j])
{
swap(a[j+1],a[j]);
count2++;
}
}
}
}
int main()
{
int n;
while(cin>>n)
{
count=0;
count2=0;
int a[N];
int b[N];
for(int i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
}
QuickSort(a,0,n-1);
Bubble_Sort(b,n);
/* for(int i=0;i<n-1;i++)
{
cout<<b[i]<<" ";
}
cout<<b[n-1]<<endl;*/
cout<<count2<<" "<<count<<endl;
}
}