select_sort
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
void select_sort1(int* a, int n) {
if (n < 2)
return;
int p;
for (int i = 0; i < n - 1; i++) {
p = i;
for (int j = i + 1; j < n; j++) {
if (a[j] < a[p])
p = j;
}
if(p!=i){
a[i]=a[i]+a[p];
a[p] = a[i] - a[p];
a[i] = a[i] - a[p];
}
}
return;
}
void select_sort2(int* a,int n) {
if (n < 2)
return;
int p=0;
for (int j = 1; j < n; j++) {
if (a[j] < a[p])
p = j;
}
if (p !=0 ) {
a[0] = a[0] + a[p];
a[p] = a[0] - a[p];
a[0] = a[0] - a[p];
}
select_sort2(a+1, --n);
}
void select_sort3(int* a, int n) {
if (n < 2)
return;
int minp,maxp;
int l = 0, r = n-1;
while(l<r) {
minp =maxp= l;
for (int j = l+1; j <= r; j++) {
if (a[j] < a[minp])
minp = j;
if (a[j] > a[maxp])
maxp = j;
}
if (minp != l) {
a[l] = a[l] + a[minp];
a[minp] = a[l] - a[minp];
a[l] = a[l] - a[minp];
}
if (maxp == l) maxp = minp;
if (maxp != r) {
a[r] = a[r] + a[maxp];
a[maxp] = a[r] - a[maxp];
a[r] = a[r] - a[maxp];
}
++l;
--r;
}
return;
}
void select_sort4(int* a, int n) {
if (n < 2)
return;
int minp, maxp;
int l = 0, r = n - 1;
minp = maxp = l;
for (int j = l + 1; j <= r; j++) {
if (a[j] < a[minp])
minp = j;
if (a[j] > a[maxp])
maxp = j;
}
if (minp != l) {
a[l] = a[l] + a[minp];
a[minp] = a[l] - a[minp];
a[l] = a[l] - a[minp];
}
if (maxp == l) maxp = minp;
if (maxp != r) {
a[r] = a[r] + a[maxp];
a[maxp] = a[r] - a[maxp];
a[r] = a[r] - a[maxp];
}
select_sort4(++a, n - 2);
return;
}
int main() {
int a[] = { 44,3,38,5,47,15,36,26,27,2,46,4,19,50,48 };
select_sort1(a, sizeof(a) / sizeof(a[0]));
for (const auto& e : a)
cout << e << " ";
cout << endl;
return 0;
}