排序

来源:互联网 发布:淘宝刷好评句子 编辑:程序博客网 时间:2024/05/21 23:02
#include <stdio.h>#include <stdlib.h>#include <string.h>void select_sort(int a[],int n){    int i,j;    for(i=0;i<n-1;i++)    {        int min_index = i;        for(j=i+1;j<n;j++)        {            if(a[j] < a[min_index])            {                min_index = j;            }        }        if(min_index!=i)        {            int temp  = a[i];            a[i] = a[min_index];            a[min_index] = temp;        }    }}void modify_bublle_sort(int a[],int n){    int i,j;    for(i=0;i<n-1;i++)    {        int sorted = 1;        for(j=i;j<n-1-i;j++)        {            if(a[i]>a[j])            {                sorted = 0;                int temp = a[i];                a[i] = a[j];                a[j] = temp;            }        }        if(sorted) break;    }}void print(int b[],int n){    int i;    for(i=0;i<n;i++)    {        printf("%d  ",b[i]);    }    printf("\n\n");}int partition(int a[],int low,int high){    int key;    key = a[low];    while(low<high)    {        while(low<high&&a[high]>=key)            high--;        if(low < high)            a[low++] = a[high];        while(low<high&&a[low]<=key)            low++;        if(low<high)            a[high--] = a[low];    }    a[low] = key;    return low;}void quick_sort(int b[],int start ,int end){    int pos;    if(start < end)    {         pos = partition(b,start,end);         quick_sort(b,start,pos-1);         quick_sort(b,pos+1,end);    }}int main(int argc, char** argv){    int a[] = {1,3,4,1,3,12,33,12,124};    printf("The select sort is:\n");    select_sort(a,9);    print(a,9);    printf("The modify bubble sort is:\n");    modify_bublle_sort(a,9);    print(a,9);    printf("The quick_sort is:\n");    quick_sort(a,0,8);    print(a,9);    return 0;}
0 0
原创粉丝点击