16주차 - 항목 1(4) 정렬 직접 선택

2127 단어
문제 설명 및 코드:
#ifndef BTREE_H_INCLUDED
#define BTREE_H_INCLUDED
/*   
*            
*      :     
*    :2015 12 14 
*    :   {57,40,38,11,13,34,48,75,6,19,9,7}      ,        
*/
#endif // BTREE_H_INCLUDED
#include <stdio.h>
#define MaxSize 20
typedef 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("
"); } } int main() { int i,n=10; 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("
"); SelectSort(R,n); printf(" :"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("
"); return 0; }
<img src="http://img.blog.csdn.net/20151214164403345" alt="" />

좋은 웹페이지 즐겨찾기