//快速排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int part(int low,int high,int* arr){
int temp=arr[low];
while(low<high){
if(arr[high]>=temp&&low<high){
high--;
}
arr[low]=arr[high];
if(arr[low]<=temp&&low<high){
low++;
}
arr[high]=arr[low];
}
arr[low]=temp;
return low;
}
void quick_sort(int* arr,int low,int high){
if(low<high){
int mid=part(low,high,arr);
quick_sort(arr,low,mid-1);
quick_sort(arr,mid+1,high);
}
}
int main(int argc, const char *argv[])
{
int arr[7]={3,7,5,1,2,9,8};
quick_sort(arr,0,6);
printf("快排成功\n");
for(int i=0;i<7;i++){
printf("%d\t",arr[i]);
}
printf("\n");
return 0;
}
在这里插入代码片
//插入排序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void insert_sort(int* arr,int n ){
int i,j;//定义循环变量
for(i=1;i<n;i++){
int temp=arr[i];
for(j=i;arr[j-1]<temp&&j>0;j--){
arr[j]=arr[j-1];
}
arr[j]=temp;
}
printf("排序完成\n");
}
int main(int argc, const char *argv[])
{
int arr[7]={3,7,5,1,2,9,8};
insert_sort(arr,7);
for(int i=0;i<7;i++){
printf("%d\t",arr[i]);
}
printf("\n");
return 0;
}