排序

来源:互联网 发布:老男孩linux运维2017 编辑:程序博客网 时间:2024/05/20 09:24

排序算法

交换类排序1.快速排序#include <stdio.h>void quickSort(int a[],int low,int high){    int i,j,temp;    if(low<high)    {        i=low;        j=high;        temp=a[i];        while(i!=j)        {            while(i<j&&a[j]>temp)j--;            {                if(i<j)                {                    a[i]=a[j];                    i++;                }            }            while(i<j&&a[i]<temp)i++;            {                if(i<j)                {                    a[j]=a[i];                    j--;                }            }        }        a[i]=temp;        quickSort(a,low,i-1);        quickSort(a,i+1,high);    }}void main(){    int a[8]={1,43,5,47,33,72,78,3},i;    quickSort(a,0,7);    for(i=0;i<8;i++)        printf("%3d",a[i]);}2.冒泡排序#include<stdio.h>void bubbleSort(int a[],int n){    int i,j,t,flag=1;    for(i=0;i<n&&flag;i++)    {        flag=0;        for(j=0;j<n-i-1;j++)        {            if(a[j]>a[j+1])            {                t=a[j];                a[j]=a[j+1];                a[j+1]=t;                flag=1;            }        }    }}void main(){    int a[8]={18,3,68,34,99,6,57,7},i;    bubbleSort(a,7);    for(i=0;i<=7;i++)        printf("%3d",a[i]);}选择类排序1.简单选择排序#include<stdio.h>void selectSort(int a[],int n){    int i,j,k,t;    for(i=0;i<n;i++)    {        k=i;        for(j=i+1;j<n;j++)        {            if(a[i]>a[j])            {k=j;                        t=a[i];                a[i]=a[k];                a[k]=t;            }        }    }}void main(){    int a[8]={4,6,787,43,7,78,43,9},i;    selectSort(a,7);    for(i=0;i<8;i++)        printf("%3d ",a[i]);}2.堆排序#include<stdio.h>void sift(int R[],int low,int high){    int i=low,j=low*2;    int temp=R[i];    while(j<=high)    {        if(j<high&&R[j]<R[j+1])            ++j;        if(temp<R[j])        {            R[i]=R[j];            i=j;            j=i*2;        }        else break;    }    R[i]=temp;}void HeapSort(int R[],int n){    int i;    int temp;    for(i=n/2;i>=1;i--)        sift(R,i,n);    //建立初始堆    for(i=n;i>=1;i--)    //在n-1次循环中完成排序    {        temp=R[1];        R[1]=R[i];        R[i]=temp;        sift(R,1,i-1);  //在减少了一个元素的无序序列中进行排序    }}void main(){    int i,R[9]={0,34,77,52,8,45,87,43,89};    HeapSort(R,8);    for(i=1;i<=8;i++)    printf("%3d ",R[i]);}插入类排序1.插入排序#include <stdio.h>void insertSort(int a[],int n){    int i,j,t;    for(i=1;i<n;i++)    {        j=i-1;        if(a[i]<a[j])        {            t=a[i];            for(j=i-1;j>=0&&a[j]>t;j--)            {                a[j+1]=a[j];            }            a[j+1]=t;        }       }}void main(){    int a[8]={4,6,787,43,7,78,49,9},i;    insertSort(a,8);    for(i=0;i<8;i++)        printf("%3d ",a[i]);}2.折半插入排序#include<stdio.h>void halfInsertSort(int a[],int n){    int i,j,low,high,t,mid;    for(i=1;i<n;i++)    {        t=a[i];    //将后面的无序数隔离        low=0;high=i-1;        while(low<=high)        {            mid=(low+high)/2;            if(a[mid]>t)high=mid-1;            else if(a[mid]<t)low=mid+1;        }        for(j=i-1;j>=low;--j)        {            a[j+1]=a[j];        }        a[j+1]=t;    }}void main(){    int a[8]={4,6,787,43,7,78,49,9},i;    halfInsertSort(a,8);    for(i=0;i<8;i++)        printf("%3d ",a[i]);}
0 0