排序算法

九种排序算法:

1.插入排序

View Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>


const int MAXSIZE = 10000; 
typedef int KeyType;
typedef int DataType;
typedef struct
{  
   KeyType key;     /*记录的关键字字段*/
   DataType info;         /*记录的其它字段*/
}RecordNode;

typedef struct
{ RecordNode  r[MAXSIZE];
  int n;               
}SeqList; 

void InsertSort(SeqList *pvector)  
{
   int i,j;
   for(i=2;i<= pvector->n;i++)     
   {
        pvector->r[0]=pvector->r[i];
        j = i-1;
        while(pvector->r[j].key > pvector->r[0].key)
        {
            pvector->r[j+1]=pvector->r[j];
            j--; 
        } 
        pvector->r[j+1]=pvector->r[0];
   }
}

void print(SeqList *pvector, int flag)
{
   if( !flag ) puts("排序前:");
   else puts("排序后:"); 
   //每10个一组
   for(int i = 1; i <= pvector->n; ++i)
   {
      printf("%d ", pvector->r[i].key);
      if( i % 10 == 0 ) puts(""); 
   } 
     
}

int main( )
{
  int N;
  while( scanf("%d",&N) != EOF )
  {
    SeqList pvector;
    pvector.n = N;
    for(int i = 1; i <= N; i++)
    {
        pvector.r[i].key = rand( ) % 1000;
    }
    print(&pvector,0);
    InsertSort(&pvector);
    print(&pvector,1);
  }
  return 0;
}

2.二分插入排序

View Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>


const int MAXSIZE = 10000; 
typedef int KeyType;
typedef int DataType;
typedef struct
{  
   KeyType key;     /*记录的关键字字段*/
   DataType info;         /*记录的其它字段*/
}RecordNode;

typedef struct
{ RecordNode  r[MAXSIZE];
  int n;                /*文件的记录个数*/
}SeqList; 

int Binary(SeqList *pvector, int num, int x)
{
  int mid, l = 1, r = num;
  while( l <= r )
  {
    mid = (l + r ) >> 1;
    //printf("mid = %d\n",mid);
    if( pvector->r[mid].key >= x )
        r = mid - 1;     
    else
        l = mid + 1;
  }
  return r;      
}

void print(SeqList *pvector, int flag)
{
   if( !flag ) puts("排序前:");
   else puts("排序后:"); 
   //每10个一组
   for(int i = 1; i <= pvector->n; ++i)
   {
      printf("%d ", pvector->r[i].key);
      if( i % 10 == 0 ) puts(""); 
   } 
     
}


void InsertSort(SeqList *pvector)  /*按记录序列递增进行插入排序*/
{
   int i,j;
   for(i=2;i<= pvector->n;i++)     /*依次插入r[2],…,r[n-1]*/ 
   {
        pvector->r[0]=pvector->r[i];
        int ans = Binary(pvector,i-1,pvector->r[0].key);
        //printf("ans = %d\n",ans);
        for(int x = i - 1; x > ans; x--)
        {
            pvector->r[x+1]=pvector->r[x]; //当前数往后移r[j] -> r[j+1]
            //j--; 
        } 
        pvector->r[ans + 1]=pvector->r[0];
        //print(pvector,1);
   }
}


int main( )
{
  int N;
  while( scanf("%d",&N) != EOF )
  {
    SeqList pvector;
    pvector.n = N;
    for(int i = 1; i <= N; i++)
    {
        pvector.r[i].key = rand( ) % 1000;
    }
    print(&pvector,0);
    InsertSort(&pvector);
    print(&pvector,1);
  }
  return 0;
}

3. 希尔排序

View Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>


const int MAXSIZE = 10000; 
typedef int KeyType;
typedef int DataType;
typedef struct
{  
   KeyType key;     /*记录的关键字字段*/
   DataType info;         /*记录的其它字段*/
}RecordNode;

typedef struct
{ RecordNode  r[MAXSIZE];
  int n;                /*文件的记录个数*/
}SeqList; 

void ShellSort(SeqList *pvector, int d)  
{
   int i,j;
   for(i = d+1;i<= pvector->n;i++)     
   {
        pvector->r[0]=pvector->r[i];
        j = i - d;
        while(j >= 1 && pvector->r[j].key > pvector->r[0].key)
        {
            pvector->r[j+d]=pvector->r[j]; 
            j = j - d; 
        } 
        pvector->r[j+d]=pvector->r[0];
   }
}

void print(SeqList *pvector, int flag)
{
   if( !flag ) puts("排序前:");
   else puts("排序后:"); 
   //每10个一组
   for(int i = 1; i <= pvector->n; ++i)
   {
      printf("%d ", pvector->r[i].key);
      if( i % 10 == 0 ) puts(""); 
   } 
     
}

void shell(SeqList *pvector)
{
  for(int d = pvector->n / 2; d >= 1; d >>= 1)
          ShellSort(pvector,d);
     
}

int main( )
{
  int N;
  while( scanf("%d",&N) != EOF )
  {
    SeqList pvector;
    pvector.n = N;
    for(int i = 1; i <= N; i++)
    {
        pvector.r[i].key = rand( ) % 1000;
    }
    print(&pvector,0);
    shell(&pvector);
    print(&pvector,1);
  }
  return 0;
}

4 快速排序

View Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>


const int MAXSIZE = 10000; 
typedef int KeyType;
typedef int DataType;
typedef struct
{  
   KeyType key;     /*记录的关键字字段*/
   DataType info;         /*记录的其它字段*/
}RecordNode;

typedef struct
{ RecordNode  r[MAXSIZE];
  int n;                /*文件的记录个数*/
}SeqList; 



void print(SeqList *pvector, int flag)
{
   if( !flag ) puts("排序前:");
   else puts("排序后:"); 
   //每10个一组
   for(int i = 1; i <= pvector->n; ++i)
   {
      printf("%d ", pvector->r[i].key);
      if( i % 10 == 0 ) puts(""); 
   } 
     
}

void swap(RecordNode &a, RecordNode &b)
{
   RecordNode temp;
   temp = a;
   a = b;
   b = temp;     
}
     

int partion(SeqList *pvector, int p, int q)
{
  RecordNode x = pvector->r[p];
  int i = p, j, temp;
  for(j = p + 1; j <= q; j++)
  {
     if( pvector->r[j].key < x.key )
     {
        ++i;
        swap(pvector->r[j],pvector->r[i]);
     }
     if( j == q )
     {
       swap(pvector->r[i], pvector->r[p]);
     }
  }
  return i;
}



void quicksort(SeqList *pvector, int p, int q)
{
   if( q - p >= 1 )
   {
       int x = partion(pvector,p,q);
       quicksort(pvector, p, x - 1 );
       quicksort(pvector, x + 1, q );
   }

}


int main( )
{
  int N;
  srand(time(NULL));
  while( scanf("%d",&N) != EOF )
  {
    SeqList pvector;
    pvector.n = N;
    for(int i = 1; i <= N; i++)
    {
        pvector.r[i].key = rand( ) % 1000;
    }
    print(&pvector,0);
    quicksort(&pvector,1,N);
    print(&pvector,1);
  }
  return 0;
}

5. 堆排

View Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>


const int MAXSIZE = 10000; 
typedef int KeyType;
typedef int DataType;
typedef struct
{  
   KeyType key;     /*记录的关键字字段*/
   DataType info;         /*记录的其它字段*/
}RecordNode;

typedef struct
{ RecordNode  r[MAXSIZE];
  int n;                /*文件的记录个数*/
}SeqList; 



void print(SeqList *pvector, int flag)
{
   if( !flag ) puts("排序前:");
   else puts("排序后:"); 
   //每10个一组
   for(int i = 1; i <= pvector->n; ++i)
   {
      printf("%d ", pvector->r[i].key);
      if( i % 10 == 0 ) puts(""); 
   } 
     
}

void swap(RecordNode &a, RecordNode &b)
{
   RecordNode temp;
   temp = a;
   a = b;
   b = temp;     
}
     
int heap_max(SeqList *pvector)
{
  return pvector->r[1].key; 
}

int heap_size(SeqList *pvector)
{
    return pvector->n;
}

void down(SeqList *pvector, int x)
{
   int len = heap_size(pvector);
   while( (x << 1) <= len )
   {
      if( ((x<<1)+1) <= len && pvector->r[x<<1].key < pvector->r[(x << 1)+1].key && (pvector->r[x].key < pvector->r[(x<<1) + 1].key))
          swap(pvector->r[x], pvector->r[(x<<1)+1]), x = (x<<1) + 1;
      else if( pvector->r[x].key < pvector->r[x << 1].key )
          swap(pvector->r[x], pvector->r[x << 1]), x <<= 1;
      else
          break;
   }
}


void set_heap_size(SeqList *pvector,int size)
{
   pvector->n += size;
}

void max_heap_sort(SeqList *pvector)
{
  int len = heap_size(pvector), num = 1;
  for(int x = len; x >= 2; x--, ++num)
  {
    printf("%d ",heap_max(pvector));
    if( num % 10 == 0 )
    puts("");
    swap(pvector->r[x],pvector->r[1]);
    set_heap_size(pvector,-1);
    down(pvector,1); 
  }
  printf("%d\n",pvector->r[1].key);
  set_heap_size(pvector,-1);
}

void build_max_heap(SeqList *pvector)
{
  int len = heap_size(pvector);
  for(int x = len / 2; x >= 1; x--)
       down(pvector, x);
   
}

int main( )
{
  int N;
  srand(time(NULL));
  while( scanf("%d",&N) != EOF )
  {
    SeqList pvector;
    pvector.n = 0;
    for(int i = 1; i <= N; i++)
    {
        pvector.r[i].key = rand( ) % 1000;
    }
    set_heap_size(&pvector,N);
    print(&pvector,0);
    build_max_heap(&pvector);
    puts("排序后:");
    max_heap_sort(&pvector);
    //print(&pvector,1);
  }
  return 0;
}

6. 冒泡排序

View Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>


const int MAXSIZE = 10000; 
typedef int KeyType;
typedef int DataType;
typedef struct
{  
   KeyType key;     /*记录的关键字字段*/
   DataType info;         /*记录的其它字段*/
}RecordNode;

typedef struct
{ RecordNode  r[MAXSIZE];
  int n;                /*文件的记录个数*/
}SeqList; 



void print(SeqList *pvector, int flag)
{
   if( !flag ) puts("排序前:");
   else puts("排序后:"); 
   //每10个一组
   for(int i = 1; i <= pvector->n; ++i)
   {
      printf("%d ", pvector->r[i].key);
      if( i % 10 == 0 ) puts(""); 
   } 
     
}

void swap(RecordNode &a, RecordNode &b)
{
   RecordNode temp;
   temp = a;
   a = b;
   b = temp;     
}
     

void Bubblesort(SeqList *pvector) 
{
   int i,j;
   for( i = 1; i < pvector->n; i++)     
   {
      for(int j = 1; j < pvector->n - i + 1; j++)
      {
         if( pvector->r[j].key > pvector->r[j + 1].key )
             swap(pvector->r[j], pvector->r[j + 1]);
              
      } 
   }
}


int main( )
{
  int N;
  srand(time(NULL));
  while( scanf("%d",&N) != EOF )
  {
    SeqList pvector;
    pvector.n = N;
    for(int i = 1; i <= N; i++)
    {
        pvector.r[i].key = rand( ) % 1000;
    }
    print(&pvector,0);
    Bubblesort(&pvector);
    print(&pvector,1);
  }
  return 0;
}

7.  选择排序

View Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>


const int MAXSIZE = 10000; 
typedef int KeyType;
typedef int DataType;
typedef struct
{  
   KeyType key;     /*记录的关键字字段*/
   DataType info;         /*记录的其它字段*/
}RecordNode;

typedef struct
{ RecordNode  r[MAXSIZE];
  int n;                /*文件的记录个数*/
}SeqList; 



void print(SeqList *pvector, int flag)
{
   if( !flag ) puts("排序前:");
   else puts("排序后:"); 
   //每10个一组
   for(int i = 1; i <= pvector->n; ++i)
   {
      printf("%d ", pvector->r[i].key);
      if( i % 10 == 0 ) puts(""); 
   } 
     
}

void swap(RecordNode &a, RecordNode &b)
{
   RecordNode temp;
   temp = a;
   a = b;
   b = temp;     
}
     

void Selectsort(SeqList *pvector) 
{
   int i,j;
   for(i=1; i < pvector->n; i++)     
   {
      for(int j = i + 1; j <= pvector->n; j++)
      {
         if( pvector->r[i].key > pvector->r[j].key )
             swap(pvector->r[i], pvector->r[j]);
              
      } 
   }
}


int main( )
{
  int N;
  while( scanf("%d",&N) != EOF )
  {
    SeqList pvector;
    pvector.n = N;
    for(int i = 1; i <= N; i++)
    {
        pvector.r[i].key = rand( ) % 1000;
    }
    print(&pvector,0);
    Selectsort(&pvector);
    print(&pvector,1);
  }
  return 0;
}

8.归并排序

View Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>


const int MAXSIZE = 10000; 
typedef int KeyType;
typedef int DataType;
typedef struct
{  
   KeyType key;     /*记录的关键字字段*/
   DataType info;         /*记录的其它字段*/
}RecordNode;

typedef struct
{ RecordNode  r[MAXSIZE];
  int n;                /*文件的记录个数*/
}SeqList; 



void print(SeqList *pvector, int flag)
{
   if( !flag ) puts("排序前:");
   else puts("排序后:"); 
   //每10个一组
   for(int i = 1; i <= pvector->n; ++i)
   {
      printf("%d ", pvector->r[i].key);
      if( i % 10 == 0 ) puts(""); 
   } 
     
}

void swap(RecordNode &a, RecordNode &b)
{
   RecordNode temp;
   temp = a;
   a = b;
   b = temp;     
}
     

void Merge(SeqList *pvector,int p1, int p2, int p3)
{
   //printf("p1 = %d, p2 = %d, p3 = %d\n", p1, p2, p3);
   RecordNode T1[MAXSIZE], T2[MAXSIZE];
   int id = 0;
   for(int x = p1;  x <= p2; ++x, ++id)
       T1[id] = pvector->r[x];
   id = 0;
   for(int x = p2 + 1;  x <= p3; ++x, ++id)
       T2[id] = pvector->r[x];
   int len1 = p2 - p1 + 1, len2 = p3 - p2, id1 = 0, id2 = 0, p = p1;
   while( id1 < len1 && id2 < len2 )
   {
     if( T1[id1].key < T2[id2].key )
       pvector->r[p++] = T1[id1++];  
     else
       pvector->r[p++] = T2[id2++];
   }
   while( id1 < len1 )
      pvector->r[p++] = T1[id1++];
   while( id2 < len2 )
      pvector->r[p++] = T2[id2++];
 
   
}

void MergeSort(SeqList *pvector, int p, int q)
{
  if( p < q )
   {
       int mid = ( p + q ) / 2;
       MergeSort(pvector, p, mid);
       MergeSort(pvector, mid + 1, q);
       Merge(pvector, p, mid, q );

   }

}

int main( )
{
  int N;
  srand(time(NULL));
  while( scanf("%d",&N) != EOF )
  {
    SeqList pvector;
    pvector.n = N;
    for(int i = 1; i <= N; i++)
    {
        pvector.r[i].key = rand( ) % 1000;
    }
    print(&pvector,0);
    MergeSort(&pvector,1,N);
    print(&pvector,1);
  }
  return 0;
}

9. 基数排序

View Code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

const int MAXSIZE = 10000; 
typedef int KeyType;
typedef int DataType;
typedef struct
{  
   KeyType key;     /*记录的关键字字段*/
   DataType info;         /*记录的其它字段*/
}RecordNode;

typedef struct
{ RecordNode  r[MAXSIZE];
  int n;                /*文件的记录个数*/
}SeqList; 

const int D=10;
const int R=10;
typedef struct RadixNode
{
  KeyType    key[D];/*关键字由d个分量组成*/
  DataType   info;  /*结点的其它字段*/
  RadixNode  *next; /*链指针*/
}RadixNode ;

typedef RadixNode *RadixList;/*待排序文件类型*/

typedef struct QueueNode
{ RadixNode *f;   /*队列的头指针*/
  RadixNode *e;   /*队列的尾指针*/
}Queue;
Queue queue[R];  

void print(RadixList head, int flag)
{
   if( !flag ) puts("排序前:");
   else puts("\n排序后:"); 
   //每10个一组
   RadixList p = head->next;
   int num = 1;
   while( p )
   {
      int d = D - 1;
      while( d >= 0 && p->key[d] == 0 ) --d;
      while( d >= 1 ) printf("%d", p->key[d--]);
      printf("%d ",p->key[0]);
      if( num % 10 == 0 ) puts("");
      ++num;
      p = p->next; 
     // puts("\n");
   } 
     
}


void radixSort(RadixList *plist,int d,int r)
{   
   int i,j,k;
   RadixNode *p,*head;
   head=(*plist)->next;
   for(j=0;j < d;j++) /*进行d次分配和收集*/
   { 
     p=head;
     for(i=0;i<r;i++)   /*队列置初值*/
     {
       queue[i].f=NULL;
       queue[i].e=NULL;
     }
     while(p!=NULL)    /*按关键字的第j个分量进行分配*/
     {
      k=p->key[j];
      if(queue[k].f == NULL) /*若第k个堆为空,则当前记录为队头*/
        queue[k].f=p;     /*否则当前记录链接到第k队的队尾*/
      else 
        (queue[k].e)->next=p; 
         queue[k].e=p;
         p=p->next;
     }
     i=0;
     while(queue[i].f==NULL) i++;
     /*从r个队列中找出第1个非空的队列*/
     p=queue[i].e;
     head=queue[i].f;    /*head为收集链表的头指针*/
     for(i++;i<r;i++)
         if( queue[i].f != NULL )
         {
            p->next=queue[i].f; /*收集非空队列*/
            p=queue[i].e;
         }
     p->next=NULL;
    }
    (*plist)->next=head;
}
        
int main( )
{
  int N,t;
  srand(time(NULL));
  while( scanf("%d",&N) != EOF )
  {
    RadixList p = (RadixNode *) malloc ( sizeof( RadixNode) );
    RadixList head = p;
    for(int i = 1; i <= N; i++)
    {
        int t = rand( ) % 1000;
        int digit = 0;
        RadixList q = (RadixNode *) malloc ( sizeof( RadixNode) );
        memset(q,0,sizeof(RadixNode));
        if( q == NULL )
        {
            puts("申请内存出错"); 
            return 0;
        }
        while( t )
        {
           q->key[digit++] = t % 10;
           t /= 10;           
        }
        q->next = NULL;
        p->next = q;
        p = q;
    }
    print(head,0);
    radixSort(&head,3,10);
    print(head,1);
  }
  return 0;
}

 

posted on 2013-04-06 15:55  luckyboy1991  阅读(151)  评论(0编辑  收藏  举报