————————————————————————————————————————————
这个题应用了快排。。。虽然题目只要求一遍,但是回了一遍就会了全部的。。。。
先上一遍的。。。。
————————————————————————————————————————————
#include<stdio.h>
void swap(long long *x,long long *y);
int main()
{
int n;
long long a[100005];
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%lld",&a[i]);
int temp=a[0];
int x=0,y=n-1;
while(x<y)
{
while(x<y && a[y]>=temp) y--;
a[x]=a[y];
while(x<y && a[x]<=temp) x++;
a[y]=a[x];
}
a[x]=temp;
for(int i=0;i<n;i++)
printf("%lld%c",a[i],i==n-1?'\n':' ');
}
return 0;
}
——————————————————————————
然后再来讲一下全部的快速排序。。。。一开始学的时候没用过快排排过一直用stl库中的sort
然后做做了数据节后题发现都不会。。。。。。。。。
能写的都超时。。。所以又学了一遍几种排序的方法
——————————————————————————————————————————
快速排序的归并有相同之处就是应用了递归
void Qsort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=Partition(a,low,high);
Qsort(a,low,mid);
Qsort(a,mid+1,high);
}
}
这个部分就是把把数组分成两部分。。。
一部分比比较值低,一部分比比较值高
int Partition(int a[],int low,int high)
{
int temp;
temp=a[low]; //用表的第一个数作为比较值
while(low<high)
{
while(low<high && a[high]>=temp) high--;
a[low]=a[high];
while(low<high && a[low]<=temp) low++;
a[high]=a[low];
}
a[low]=temp;
return low;
}
这用来返回中轴值的
讲每个部分进行排序最后就排好了
#include<stdio.h>
#define N 10000
void Qsort(int a[],int low,int high);
int Partition(int a[],int low,int high);
int main()
{
int n;
int a[N];
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
Qsort(a,0,n-1);
for(int i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
void Qsort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=Partition(a,low,high);
Qsort(a,low,mid);
Qsort(a,mid+1,high);
}
}
int Partition(int a[],int low,int high)
{
int temp;
temp=a[low]; //用表的最小值数作为中轴值
while(low<high)
{
while(low<high && a[high]>=temp) high--;
a[low]=a[high];
while(low<high && a[low]<=temp) low++;
a[high]=a[low];
}
a[low]=temp;
return low;
}