选择排序

来源:互联网 发布:淘宝男童运动套装 编辑:程序博客网 时间:2024/06/14 11:43
#include <iostream>using namespace std;void swap(int * p1, int * p2){int t;t = *p1;*p1 = *p2;*p2 = t;}void sort(int sorce[], int n){int min;   //最小元素的下标for (int j=0; j<n; j++){min = j;for (int i=j+1; i<n; i++){if (sorce[i] < sorce[min])min = i;}swap(&sorce[min], &sorce[j]);}}int main(){int a[6] = {1, 2, 3, 4, 5, 6};sort(a, 6);for (int i=0; i<6; i++){cout << a[i] << endl;}return 0;}