黑马程序员技术交流社区

标题: 关于面试常见问题之一的 数据排序问题. [打印本页]

作者: 阿么    时间: 2016-2-28 22:36
标题: 关于面试常见问题之一的 数据排序问题.
数据排序是面试的常见问题  也是很繁琐的问题,今天我直接分享代码给大家


int main()
{
    int arr[]={1,2,4,3,8,5};
    int tempMax = 0;
    //定义变量tempMax来存储遍历中的最大值
    int temp = 0;
    for (int i = 0; i<5; i++)
    {
        tempMax = i;
        for (int j = i+1;j<6;j++)
        {
        if(arr[tempMax]<arr[j])
        {
            tempMax = j;
        }
    }
   
    printf("第%d轮是%d\n",i,temp);
    temp = arr[i];
    arr[i] = arr[tempMax];
    arr[tempMax] = temp;
    }
    for(int k = 0; k<6;k++)
    {
        printf("%d\t",arr[k]);
    }
    return 0;
}
                      头文件自己补




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2