第十三周项目4-数组的排序(3 选择排序)

来源:互联网 发布:学vb还是vc 编辑:程序博客网 时间:2024/06/16 01:11
/* *Copyright(c)2014,烟台大学计算机学院 *All rights reserved. *文件名称:test.cpp *作者:满星辰 *完成日期:2014年 11月 23日 *版本号:v1.0 * *问题描述:用选择法按降序排序数组 *程序输入: *程序输出: */#include <iostream>using namespace std;void select_sort(int a[],int n);void output_array(int a[],int n);int main( ){    int a[20]= {86,76,62,58,77,85,92,80,96,88,77,67,80,68,88,87,64,59,61,76};    int b[15]= {27,61,49,88,4,20,28,31,42,62,64,14,88,27,73};    select_sort(a,20);   //用选择法按降序排序a中元素    output_array(a,20);   //输出排序后的数组    cout<<endl;    select_sort(b,15);   //用选择法按降序排序b中元素    output_array(b,15);   //输出排序后的数组    return 0;}//请在下面定义select_sort和output_array函数void select_sort(int a[],int n){    int t;    int location=0;    for(int i=0; i<n-1; i++)    {        location=i;        for(int j=i+1; j<n; j++)        {            if(a[j]>a[location])                location=j;        }        t=a[i];        a[i]=a[location];        a[location]=t;    }    return;}void output_array(int a[],int n){    for(int i=0; i<n; ++i)    {        cout<<a[i]<<' ';    }    return;}

运行结果:


学习心得:

我觉得这个要比冒泡难多了。。。不过效率好像也高了?

0 0
原创粉丝点击