数据结构中的排序查找算法(C语言实现)

直接插入排序

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

using namespace std;

typedef struct

{

         int key;

}list;

void Dir_In(list D[],int length)

{

         list dm;

         int k=0;

         for(int i=1;i<length;i++)

         {

                   dm=D[i];k=i-1;

                      while((dm.key<D[k].key)&& (k>=0))

                       {

                             D[k+1]=D[k];

                             k–;

                       }

                      D[k+1]=dm;

                   }

 

}

void main()

{

         int a[10]={10,1,9,4,3,6,11,21,36,15};

         list D[10];

         for(int i=0;i<10;i++)

                   D[i].key=a[i];

         Dir_In(D,10);

         for(int i=0;i<10;i++)

                   cout<<D[i].key<<endl;

      }

 

直接选择排序:

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

using namespace std;

typedef struct

{

         int key;

}list;

void Direct(list D[],int low,int length)

{

         int i=low;

         list dm;

         for(int i=low;i<length;i++)

         {

                   int k=i;

                   for(int j=i+1;j<length;j++)

                   {

                            if(D[k].key>D[j].key)

                                     k=j;

                   }

                   if(k!=i)

                   {

                            dm=D[k];

                            D[k]=D[i];

                            D[i]=dm;

                   }

         }

 

}

void main()

{

         int a[10]={1,11,2,3,41,54,42,40,8,4};

         list D[10];

         for(int i=0;i<10;i++)

                   D[i].key=a[i];

         Direct(D,0,10);

         for(int i=0;i<10;i++)

                   cout<<D[i].key<<endl;

      }

 

归并排序:

#include <iostream>

#include <stdio.h>

using namespace std;

typedef struct

{

         int key;

}list;

/**************************冒泡排序*********************************************/

void MA(list G[],int length)

{

         list md;

         for(int i=0;i<length;i++)

                   for(int j=0;j<length-i-1;j++)

                   {

                            if(G[j].key>G[j+1].key)

                            {

                                     md=G[j+1];

                                G[j+1]=G[j];

                                     G[j]=md;

                            }

                   }

        

}

/**************************归并排序*********************************************/

void  Gu(list G1[],int low1,int high1,int low2,int high2,list G2[],list G_K[])

{

         int i=low1,j=low2;

         int k=0;

         while((i<=high1)&&(j<=high2))

         {

                   if(G1[i].key<=G2[j].key)

                   {

                            G_K[k].key=G1[i].key;

                        i++;

                   }

                   else

                   {

                            G_K[k].key=G2[j].key;

                            j++;

                   }

                   k++;

         }

         while(i<=high1)

         {

                   G_K[k]=G1[i];

                   k++;

                   i++;

         }

         while(j<=high2)

         {

                   G_K[k]=G2[j];

                   k++;

                   j++;

         }

}

 

void main()

{

         list G[10],G1[10],G11[20];

         int a[10]={11,21,10,1,6,54,8,41,42,65};

         int b[10]={8,41,52,31,20,21,32,20,2,3};

         for(int i=0;i<10;i++)

         {

                   G[i].key=a[i];

             G1[i].key=b[i];

         }

         MA(G,10);

         MA(G1,10);

         Gu(G,0,9,0,9,G1,G11);

         for(int i=0;i<20;i++)

         cout<<G11[i].key<<endl;

       

}

 

 

快速排序法:

#include<stdio.h>

#include<stdlib.h>

#include <iostream>

using namespace std;

#define MATSIZE 10

typedef struct

{

         int key;

}list;

typedef struct

{

         list   list_data[MATSIZE];

         int  front;

         int  rear;

        

}fast_list;

int  Fa_data(list data[],int low ,int high)

{

         fast_list *aa;

   list  k=data[low];

         int rex[1];

while(low<high)

{

         while (((data[high].key>k.key) || (data[high].key==k.key)) &&(low<high))

                   high–;

         if(low<high)

         {

                   data[low]=data[high];

                   low++;

         }

         while (((data[low].key<k.key) || (data[low].key==k.key)) && (low<high))               

                   low++;

         if(low<high)

         {

         data[high]=data[low];

         high–;

         }

         //

         //

 

}

 

data[low]=k;

 

 

return low;

 

}

list * list_fast(list data[],int low,int high)

{

         if(low<high)

         {

                   int pos;

                   pos=Fa_data(data,low,high);

                   list_fast(data,low,pos-1);

                   list_fast(data,pos+1,high);

         }

         return data;

}

void main()

{

         fast_list  MM,* MM1;

         list Lm[10],*LM;

         MM.front=0;

         MM.rear=MATSIZE-1;

         int a[10]={3,5,2,1,4,6,22,77,2,10};

         for(int i=0;i<10;i++)

         Lm[i].key=a[i];

         LM=list_fast(Lm,0,9);

         //for(int i=0;i<2;i++)

         //MM1=Fa_data(MM1,0,3);

        

         //MM1=Fa_data(MM1,4,9);

         for(int i=0;i<10;i++)

                   printf(“%d\n”,LM[i].key);

        

}

 

堆排序:

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

using namespace std;

typedef struct

{

         int key;

}list;

 

void Sift(list D[],int k,int m)

{

         list dm;

         dm=D[k];

         int i=k;

         int ddd=0;

         int j=2*i;

 while((j<=m))

  {

         if((j<m) &&(D[j].key<D[j+1].key))

                   j=j+1;

         if(dm.key>=D[j].key)

                   break;

         else

         {

                   D[i]=D[j];

                   i=j;

                   j=2*i;

         }

 

  }

 D[i]=dm;

}

void InitDu(list D[],int length)

{

        

         for(int k=length/2;k>=0;–k)

                   Sift(D,k-1,length-1);

        

 

}

list * HeapSort(list D[],int length)

{

         InitDu(D,length);

         list b;

         int n=length;

         for(int i=n-1;i>=1;–i)

         {

                   b=D[0];

                   D[0]=D[i];

                   D[i]=b;

                   //Sift(D,(i-1)/2,i-1);

                   InitDu(D,i);

         }

         return D;

}

void main()

{

         int a[10]={2,33,21,1,25,63,13,15,11,9};

         list D[10],*D1;

         for(int i=0;i<10;i++)

                   D[i].key =a[i];

         D1=HeapSort(D,10);

         for(int i=0;i<10;i++)

         printf(“%d\n”,D1[i].key);

     }

 

希尔排序:

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

using namespace std;

typedef struct {

         int key;

}list;

void InitSh(list S[],int length,int d)

{

         //int d=length/2;

         list dm;

         for(int i=0;i<length-d;i++)

         {

                   if(S[i].key>S[i+d].key)

                   {

                            dm=S[i+d];

                            S[i+d]=S[i];

                            S[i]=dm;

                   }

         }

}

void Sh(list S[],int length)

{

 for(int d=length/2;d>=1;d–)

 {

          InitSh(S,length,d);

 }

}

void main()

{

         int a[10]={1,10,11,5,4,14,51,7,3,2};

         list D[10];

         for(int i=0;i<10;i++)

                   D[i].key=a[i];

         Sh(D,10);

         for(int i=0;i<10;i++)

                   cout<<D[i].key<<endl;

     }

 

    原文作者:查找算法
    原文地址: https://blog.csdn.net/tangtang19871103/article/details/11758035
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞