您的位置:首页 > 编程语言

冒泡排序和快速排序(完整代码)

2020-09-04 18:42 701 查看

#include<stdio.h>
#include<stdlib.h>

void BubbleSort(int a[],int n) //直接插入排序
{
int i,j,flag;
int temp;
for(i=1;i<n;i++)
{
flag=0;
for(j=1;j<n-i+1;j++)
if(a[j-1]>a[j])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
flag=1;
}
if(flag==0)
break;
}

}

void QuickSort(int a[],int low,int high) //快速排序
{
int temp;
int i=low,j=high;
if(i<j)
{
temp=a[i];
while(i<j)
{
while(i<j&&a[j]>=temp)
–j;
while(i<j&&a[i]<temp)
++i;
if(i<j)
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
a[low]=a[i];
a[i]=temp;
QuickSort(a,low,i-1);
QuickSort(a,i+1,high);
}

}

void print(int array[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d ",array[i]);
}
}
int main()
{
int array[]={4,3,6,9,7,1,2,4,5,0};
int n=10;
int low=0,high=9;
// BubbleSort(array,n);
QuickSort(array,low,high);
print(array,n);
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: