排序算法二:简单选择排序

来源:互联网 发布:手机照片怎么导入mac 编辑:程序博客网 时间:2024/06/01 20:52
#include "stdafx.h"#include<iostream>using namespace std;inline void sway(int &a, int &b) {int temp;temp = a;a = b;b = temp;}int main(){//length of array and initint len;cin >> len;int *array = new int[len];for (int i = 0; i < len; i++) {cin >> array[i];}//Select the smallest element and put it in the right position.//This loop from 0 to length minus 1 because no need to //select the last one.for (int l = 0; l < len - 1; l++) {int min = array[l];int m = l + 1;//从第二个开始选择未排序最小while ((m < len) && (array[m] < min)) {sway(min, array[m]);//sway the position.array[l] = min;m++;}}//output.new line per ten element.for (int n = 0; n < len; n++) {if ((n % 10 == 0) && (n != 0)) {cout << array[n] << endl;}else {cout << array[n] << " ";}}    return 0;}

0 0
原创粉丝点击