排序算法程序示例

  • 程序包括了常见的排序算法,分两部分编写,主程序和头文件部分

主程序

#include<stdio.h>
#include"code.h"
int main(void)
{
        int A[]={7,6,3,4,5,1,2};
        int n=7;
        //bubblesort(A,n);
        //selectionsort(A,n);
        //insertionsort(A,n);
        //MergeSort(A,n);
        //quicksort(A,0,n-1);
        //headsort(A,n);
        shellsort(A,n);
        for(int i=0;i<n;i++)
                printf("%d ",A[i]);
        printf("\n");
        return 0;

}

头文件

#include<algorithm>
#include <iostream>
using namespace std;
int *bubblesort(int *A,int n);
int *selectionsort(int *A,int n);
int *insertionsort(int *A,int n);

void mergearray(int *A,int first,int mid,int last,int *temp);
void mergesort(int *A,int first,int last,int *temp);
bool MergeSort(int *A,int n);

void quicksort(int *A,int left,int right);

void headadjust(int *A,int i,int n);
void BuildHeap(int *A,int n);
void headsort(int *A,int n);

void shellsort(int *A,int n);


int *bubblesort(int *A,int n)
{
        for(int i=0;i<n;i++){
            for(int j=0;j<n-1-i;j++){
                if(A[j]>A[j+1]){
                    int temp=A[j];A[j]=A[j+1];A[j+1]=temp;
                }
            }
        }
        return A;
}

int *selectionsort(int *A,int n)
{
        for(int i=0;i<n;i++){
            int k=i;
            for(int j=i+1;j<n;j++){
                if(A[j]<A[k]){
                    int temp=A[j];A[j]=A[k];A[k]=temp;
                }
            }
        }
        return A;
}

int *insertionsort(int *A,int n)
{
        for(int i=0;i<n;i++){
            for(int j=i;j>0;j--){
                if(A[j]<A[j-1]){
                    int temp=A[j];A[j]=A[j-1];A[j-1]=temp;
                }
            }
        }
        return A;
}

void mergearray(int *A,int first,int mid,int last,int *tmp)
{
        int i=first,j=mid+1;
        int m=mid,n=last;
        int k=0;
        while(i<=m&&j<=n)
        {
                if(A[i]<=A[j])
                    tmp[k++]=A[i++];
                else
                    tmp[k++]=A[j++];
        }
        while(i<=m)
                tmp[k++]=A[i++];
        while(j<=n)
                tmp[k++]=A[j++];
        for(i=0;i<k;i++)
                A[first+i]=tmp[i];
}

void mergesort(int *A,int first,int last,int *tmp)
{
        if(first<last)
        {
            int mid=(first+last)/2;
            mergesort(A,first,mid,tmp);
            mergesort(A,mid+1,last,tmp);
            mergearray(A,first,mid,last,tmp);
        }
}

bool MergeSort(int *A,int n)
{
        int *p=new int[n];
        if(p==NULL)     return 0;
        mergesort(A,0,n-1,p);
        delete []p;
        p=NULL;
        return 1;
}

void quicksort(int *A,int left,int right)
{
        if(left<right){
            int i=left,j=right;
            int key=A[left];
            while(i<j){
                while(i<j&&A[j]>=key)
                    j--;
                A[i]=A[j];
                while(i<j&&A[i]<=key)
                    i++;
                A[j]=A[i];
            }
            A[i]=key;
            quicksort(A,left,i-1);
            quicksort(A,i+1,right);
        }
}

void headadjust(int *A,int i,int n)
{
        int lchild=2*i+1;
        int rchild=2*i+2;
        int tmp=i;
        if(i<=n/2)
        {
                if(lchild<=n&&A[lchild]>A[tmp])
                    tmp=lchild;
                if(rchild<=n&&A[rchild]>A[tmp])
                    tmp=rchild;
                if(tmp!=i)
                {
                        swap(A[i],A[tmp]);
                        headadjust(A,tmp,n);
                }
        }
}

void BuildHeap(int *A,int n)
{
        int i;
        for(i=n/2;i>=0;i--)
        {
                headadjust(A,i,n);
        }
}

void headsort(int *A,int n)
{
        int i;
        BuildHeap(A,n);
        for(i=n;i>=0;i--)
        {
             swap(A[0],A[i]);
             headadjust(A,0,i-1);
        }
}

void shellsort(int *A,int n)
{
        for(int gap=n/2;gap>0;gap/=2){
            for(int i=gap;i<n;i++){
                for(int j=i-gap;j>=0&&A[j+gap]<A[j];j-=gap)
                    swap(A[j],A[j+gap]);
            }
        }
}

你可能感兴趣的:(排序算法程序示例)