码迷,mamicode.com
首页 > 编程语言 > 详细

C语言数据结构各种排序算法(选择,直接,希尔,起泡等排序)

时间:2016-06-15 23:43:38      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:

#include <stdio.h>

#include<stdlib.h>

#include <time.h>

#define MAX 20         /*线性表中最多元素个数*/

typedef int KeyType;

typedef char InfoType[10];

typedef struct

{

  KeyType key;

  InfoType data;

}RecType;

 

void InsertSort(RecType R[],int n)

{

  int i,j,k;

  RecType temp;

  for(i=1;i<n;i++)

  {

    temp=R[i];

j=i-1;

while(j>=0 && temp.key<R[j].key)

{

  R[j+1]=R[j];

  j--;

}

R[j+1]=temp;

printf("   i=%d   ",i);

for(k=0;k<n;k++)

  printf("%3d",R[k].key);

printf("\n");

  }

}

 

void ShellSort(RecType R[],int n)

{

  int i,j,d,k;

  RecType temp;

  d=n/2;

  while(d>0)

  {

for(i=d;i<n;i++)

{

  j=i-d;

  while(j>0 && R[j].key>R[j+d].key)

  {

    temp=R[j];

    R[j]=R[j+d];

    R[j+d]=temp;

    j=j-d;

  }

}

printf("   d=%d:   ",d);

for(k=0;k<n;k++)

  printf("%3d",R[k].key);

printf("\n");

d=d/2;

  }

}

 

void BubbleSort(RecType R[],int n)

{

  int i,j,k;

  RecType temp;

  for(i=0;i<n-1;i++)

  {

       for(j=n-1;j>i;j--)

                if(R[j].key<R[j-1].key)

                {

                     temp=R[j];

                     R[j]=R[j-1];

                     R[j-1]=temp;

                }

              printf("   i=%d   ",i);

              for(k=0;k<n;k++)

                printf("%3d",R[k].key);

              printf("\n");

  }

}

 

void QuickSort(RecType R[],int s,int t,int n)

{

  int i=s,j=t,k;

  RecType temp;

  if(s<t)

  {

temp=R[s];

while(i!=j)

{

  while(j>i && R[j].key>temp.key)

    j--;

  if(i<j)

  {

    R[i]=R[j];

    i++;

  }

  while(i<j && R[i].key<temp.key)

    i++;

  if(i<j)

  {

    R[j]=R[i];

    j--;

  }

}

R[i]=temp;

printf("      ");

for(k=0;k<n;k++)

  if(k==i)

    printf(" [%d]",R[k].key);

  else

    printf("%4d",R[k].key);

printf("\n");

QuickSort(R,s,i-1,n);

QuickSort(R,i+1,t,n);

  }

}

 

void SelectSort(RecType R[],int n)

{

  int i,j,k,l;

  RecType temp;

  for(i=0;i<n-1;i++)

  {

k=i;

for(j=i+1;j<n;j++)

  if(R[j].key<R[k].key)

    k=j;

if(k!=i)

{

  temp=R[i];

  R[i]=R[k];

  R[k]=temp;

}

printf("    i=%d   ",i);

for(l=0;l<n;l++)

  printf("%3d",R[l].key);

printf("\n");

  }

}

 

void DispHeap(RecType R[],int i,int n)    /*以括号表示法输出建立的堆*/

{

                if(i<=n)

              printf("%d",R[i].key);

                if(2*i<=n)

                            {

                                          printf("(");

                                          DispHeap(R,2*i,n);

                                          printf(",");

                                          if(2*i+1<=n)

                                            DispHeap(R,2*i+1,n);

                                          printf(")");

                            }

}

void Sift(RecType R[],int low,int high)         /*调整堆*/

{

  int i=low,j=2*i;

  RecType temp=R[i];

  while(j<=high)

  {

if(j<high && R[j].key<R[j+1].key)

  j++;

if(temp.key<R[j].key)

{

  R[i]=R[j];

  i=j;

  j=2*i;

}

else

  break;

  }

  R[i]=temp;

}

void HeapSort(RecType R[],int n)        /*对R[1]到R[n]元素实现堆排序*/

{

  int i;

  RecType temp;

  for(i=n/2;i>=1;i--)

Sift(R,i,n);

  printf("初始堆:");

  DispHeap(R,1,n);

  printf("\n");

  for(i=n;i>=2;i--)

  {

printf("交换%d与%d,输出%d\n",R[i].key,R[1].key,R[1].key);

temp=R[1];

R[1]=R[i];

R[i]=temp;

Sift(R,1,i-1);

printf("筛选调整得到堆:");

DispHeap(R,1,i-1);

printf("\n");

  }

}

 

void PrintArray(RecType R[],int n)

{

  int i;

  for(i=0;i<n;i++)

printf("%3d",R[i].key);

  printf("\n");

}

 

void main()

{

      

  int i,n=10,cord;

  RecType R[MAX],S[MAX];

  srand(time(0));              /*用系统时间作为随机数种子*/

for (i=0; i<n; i++)

S[i].key=100*rand()/RAND_MAX;     /*利用随机函数产生10个100以内的随机整数*/

do

  {

printf("*************主菜单****************\n");

printf("1-直接插入排序\n");

printf("2-希尔排序\n");

printf("3-冒泡排序\n");

printf("4-快速排序\n");

printf("5-简单选择排序\n");

printf("6-堆排序\n");

printf("7-退出\n");

printf("请输入你的选择:");

scanf("%d",&cord);

switch(cord)

{

  case 1:

    printf("初始关键字:");

    PrintArray(S,n);

        for(i=0;i<n;i++)

          R[i].key=S[i].key;

    InsertSort(R,n);

    printf("最后结果 :");

    PrintArray(R,n);

    break;

  case 2:

    printf("初始关键字:");

    PrintArray(S,n);

        for(i=0;i<n;i++)

          R[i].key=S[i].key;

    ShellSort(R,n);

    printf("最后结果 :");

    PrintArray(R,n);

    break;

  case 3:

    printf("初始关键字:");

    PrintArray(S,n);

        for(i=0;i<n;i++)

          R[i].key=S[i].key;

    BubbleSort(R,n);

    printf("最后结果 :");

    PrintArray(R,n);

    break;

  case 4:

    printf("初始关键字:");

    PrintArray(S,n);

        for(i=0;i<n;i++)

          R[i].key=S[i].key;

    QuickSort(R,0,n-1,n);

    printf("最后结果 :");

    PrintArray(R,n);

    break;

  case 5:

    printf("初始关键字:");

    PrintArray(S,n);

        for(i=0;i<n;i++)

          R[i].key=S[i].key;

    SelectSort(R,n);

    printf("最后结果 :");

    PrintArray(R,n);

    break;

  case 6:

    printf("初始关键字:");

    PrintArray(S,n);

        for(i=1;i<=n;i++)

          R[i].key=S[i-1].key;

    HeapSort(R,n);

    printf("最后结果 :");

    PrintArray(R,n);

    break;

  case 7:

    exit(0);

  default:

    printf("选择错误,请重新输入!\n");

}

  }while(1);

}

C语言数据结构各种排序算法(选择,直接,希尔,起泡等排序)

标签:

原文地址:http://www.cnblogs.com/lhq-it/p/5589280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!