数据结构排序算法代码实现

来源:互联网 发布:java高手的简历 编辑:程序博客网 时间:2024/06/06 17:24
#include <bits/stdc++.h>using namespace std;#define MAXN 100000//冒泡排序void BubbleSort( int A[], int N ){    int i,j,temp,flag;    for(i=0;i<N;++i)    {        flag=0;        for(j=i+1;j<N;++j)        {            if( A[i]>A[j])            {                 temp=A[i];                 A[i]=A[j];                 A[j]=temp;                 flag=1;            }         }         if( flag ==0 )              break;         }    for(i=0;i<N-1;i++)         printf("%d ",A[i]);    printf("%d\n",A[N-1]);}//快速排序int partition(int a[],int low,int high){    int temp = a[low];    while(low<high)    {        if(low<high && a[high]>=temp)            --high;        a[low]=a[high];        if(low<high && a[low] <= temp)            ++low;        a[high]=a[low];    }    a[low] = temp;    return low;}void QuickSort( int A[], int low, int high){int pivotloc;//枢轴if(low<high){pivotloc = Partition(A,low,high);QuickSort(A,low,pivotloc-1);QuickSort(A,pivotloc+1,high);}}//选择排序void SelectSort(int num[],int n){    for(int i=0; i<n; i++)    {        int min=i;        for(int j=i+1; j<n; j++)        {            if(num[min]>num[j])//从剩下的元素选择一个最小的元素                min=j;        }        if(min != i)//如果最小元素不是无序组起始位置元素,则与起始元素交换位置        {            int tmp = num[min];            num[min] = num[i];            num[i] = tmp;        }    }}void SelectSort(int num[],int n){    for(int i=1;i<=n-1;i++)    {        min=i;        for(int j=i+1;j<=n;j++)        {            if(num[min]>num[j])            {                min=j;            }        }        if(min!=i)        {            int temp;            for(int k=i+1;k<min;k++)            {                if(num[k]==num[i])                {                    temp=num[k];                    num[k]=num[i];                    num[i]=temp;                }            }            temp=num[i];            num[i]=num[min];            num[min]=temp;        }    }}#include<stdio.h>#define maxsize 100typedef int Keytype;typedef struct{   Keytype key;   InfoType data;}RedType;typedef struct{   RedType r[maxsize+1];   int length;  }SqList;//直接插入排序void InsertSort(SqList &L){    //对顺序表L做直接插入排序    for(i=2;i<=L.length;i++)    {       if(L.r[i].key<L.r[i-1].key)       {          L.r[0]=L.r[i];          L.r[i]=L.r[i-1];          for(j=i-2;L.r[0].key<L.r[j].key;j--)          {             L.r[j+1]=L.r[j];//记录后移          }          L.r[j+1]=L.r[0];//插入到正确位置       }    }}void InsertSort(int a[], int n){    int i,j;    for(i=2;i<=n;i++)    {        if(a[i]<a[i-1])        {            int t=a[i];            a[i]=a[i-1];            for(j=i-2;t<a[j];j--)            {                a[j+1]=a[j];            }            a[j+1]=t;        }    }}//折半插入排序void BinsertSort(SqList &L)   // 折半插入排序{    int i,low,high,mid;    for(i=2; i<= L.length; ++i)    {        L.r[0]=L.r[i];                 //将L.r [i] 暂存到L.r[0]        low=1;        high=i-1;        While(low<=high)       //比较,折半查找插入位置        {            mid=(low+high)/2;   // 折半            if (L.r[0].key< L.r[mid].key) high=mid-1;   //插入点在低半区            else low=mid+1;        }   // 插入点在高半区        for( j=i-1; j>=low; --j )  L.r[j+1]=L.r[j];    // 记录后移        L.r[low]=L.r[0];    }    // 插入} // BInsertSortvoid BinsertSort(int a[],int n)//传递数组和数组元素个数{    int i,j,mid,low,high,temp;    for(i = 2; i <= n;i++) //我看了一些其他人写得折半插入算法是从下标1开始的,i = 2;i <=n,并将array[i]存储到array[0]    {        temp = a[i];//把第i+1个元素赋值给temp(数组从下标0开始)        low = 1;//初始化low,array[low]代表数组中第1个元素        high = i-1;//初始化high,array[high]代表已插入的最后一个元素        while(low <= high) //不断的折半1/2 1/4 ....        {            mid = (low + high) / 2;//计算中间位置            if (temp > a[mid])            {                //插入值大于中间值                low = mid + 1;            }            else            {                //插入值小于中间值                high = mid - 1;            }        }        for(j=i-1; j >= low;j--)        {            //将需要移动的数组向后移            a[j+1] = a[j];        }        //将值插入到指定位置        a[low] = temp;    }}//希尔排序void ShellSort(int a[], int length){    int increment;    int i,j;    int temp;    for(increment = length/2; increment > 0; increment /= 2) //用来控制步长,最后递减到1    {        // i从第step开始排列,应为插入排序的第一个元素        // 可以先不动,从第二个开始排序        for(i = increment; i < length; i++)        {            temp = a[i];            for(j = i - increment; j >= 0 && temp < a[j]; j -= increment)            {                a[j + increment] = a[j];            }            a[j + increment] = temp; //将第一个位置填上        }    }}//归并排序void merge(int a[], int first, int mid, int last){    int i = first, j = mid + 1;    int m = mid,   n = last;    int k = 0;    int l=mid-first;    int temp[maxn];    while (i <= m && j <= n)    {        if (a[i] <= a[j])            temp[k++] = a[i++];        else        {            temp[k++] = a[j++];        }    }    while (i <= m)        temp[k++] = a[i++];    while (j <= n)        temp[k++] = a[j++];    for (i = 0; i < k; i++)        a[first + i] = temp[i];}void mergesort(int a[], int first, int last){    if (first < last)    {        int mid = (first + last) / 2;        mergesort(a, first, mid);    //左边有序        mergesort(a, mid+1, last); //右边有序        merge(a, first, mid, last); //再将二个有序数列合并    }}