第16周 项目1-验证算法(直接选择排序)

来源:互联网 发布:最新彩票预测软件 编辑:程序博客网 时间:2024/06/08 19:22

问题及描述:

#include <stdio.h>#define MaxSize 20typedef int KeyType;    //定义关键字类型typedef char InfoType[10];typedef struct          //记录类型{    KeyType key;        //关键字项    InfoType data;      //其他数据项,类型为InfoType} RecType;              //排序的记录类型定义void SelectSort(RecType R[],int n){    int i,j,k,l;    RecType temp;    for (i=0; i<n-1; i++)           //做第i趟排序    {        k=i;        for (j=i+1; j<n; j++)   //在当前无序区R[i..n-1]中选key最小的R[k]            if (R[j].key<R[k].key)                k=j;            //k记下目前找到的最小关键字所在的位置        if (k!=i)               //交换R[i]和R[k]        {            temp=R[i];            R[i]=R[k];            R[k]=temp;        }        printf("i=%d: ",i);        for (l=0; l<n; l++)            printf("%d ",R[l].key);        printf("\n");    }}int main(){    int i,n=12;    RecType R[MaxSize];    KeyType a[]= {57,40,38,11,13,34,48,75,6,19,9,7};    for (i=0; i<n; i++)        R[i].key=a[i];    printf("排序前:");    for (i=0; i<n; i++)        printf("%d ",R[i].key);    printf("\n");    SelectSort(R,n);    printf("排序后:");    for (i=0; i<n; i++)        printf("%d ",R[i].key);    printf("\n");    return 0;}


运行结果:

0 0