选择排序

// SelectSort.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int sort(int* p,int len )
{
    int index,temp,i,j;
    if( p == NULL  || len <= 1 )
    {
        return -1;
    }
    for( i = 0; i < len; i++ )
    {
        index = i;
        for( j = i+1;j < len;j++ )
        {
            if( p[j] < p[index] )
            {
                index = j;
            }
        }
        if( index != i )
        {
            temp = p[i];
            p[i] = p[index];
            p[index] = temp;
        }
    }
    return 0;
}

int main(int argc, char* argv[])
{
    int a[]={12,54,6,3,4,6,76,33};
    int i;
    sort(a,sizeof(a)/sizeof(a[0]));

    for( i = 0 ; i < sizeof(a)/sizeof(a[0]);i++ )
    {
        printf("%d ",a[i]);
    }
    printf("\n");
    return 0;
}

 

posted @ 2017-04-23 10:38  未命名blogs  阅读(106)  评论(0编辑  收藏  举报