Topic Portal
1 /*2 Verify the correctness of the algorithm with the zstu3539 problem3 */4#include <cstdio>5#include <iostream>6#include <algorithm>7#include <ctime>8#include <cstdlib>9 using namespacestd;Ten One Const intMAXN =1000000+Ten; A Const intINF =0x3f3f3f3f; - intA[MAXN]; - the voidSelectsort (int*a,intN) - { - for(intI=1; i<=n; ++i) - { + intMN = A[i];intK =i; - for(intj=i+1; j<=n; ++j) + { A if(Mn >A[j]) at { -MN =A[j]; -K =J; - } - } - if(MN! =A[i]) in swap (A[i], a[k]); - } to } + - voidSelectsort_2 (int*a,intN//assign maximum and minimum values each time, plus bugs the { * for(intI=1; i<=n/2+1; ++i) $ {Panax Notoginseng intmn_id = i, mx_id = N-i +1; - for(intj=i+1; j<=n-i; ++j) the { + if(A[j] >a[mx_id]) A { themx_id = j;Continue; + } - if(A[j] < a[mn_id]) mn_id =J; $ } $ swap (A[i], a[mn_id]); -Swap (a[n-i+1], a[mx_id]); - } the } - Wuyi intMainvoid) the { - //freopen ("rand_small.in", "R", stdin); Wu intN; - About while(SCANF ("%d", &n)! =EOF) $ { - if(n = =0) - Continue; - for(intI=1; i<=n; ++i) A { +scanf ("%d", &a[i]); the } - $ Selectsort (A, n); the //Selectsort_2 (A, n); the the BOOLFlag =true; the for(intI=1; i<=n; ++i) - { in if(flag) the { theprintf ("%d", A[i]); AboutFlag =false; the } the Else theprintf ("%d", A[i]); + } -Puts (""); the }Bayi the return 0; the}
Sorting algorithm Selection