OJ the "Binary choice sort" of brush question

Source: Internet
Author: User

DescriptionNote: You only need to commit the code that is marked as modified in the section, please follow the C + + language.

Binary selection sequencing: the traditional selection sorting algorithm improved, in a trip comparison process, simultaneously record the maximum and minimum position, the minimum value and the first element exchange, the maximum value and the last element exchange, that is, a trip to determine two elements, the remaining sequence repeats the above process, until the sequence is empty.

#include <iostream>
using namespace Std;
int main ()
{
int a[100],i,n;
cin>>n;
for (i=0; i<n; i++)
Cin>> A[i];
Int J, Minpos, Maxpos, TMP;
for (i=0; I <n/2; i++)
{
Minpos = i;
Maxpos = i;
for (j= i+1; j<= n-i-1; j + +)
{
if (A[j] > A[maxpos])
{
Maxpos = j;
Continue;
}
if (a[j]< A[minpos])
Minpos = j;
}
/***** Modified starting line ******/
TMP = A[i];
A[i] = A[minpos];
A[minpos] = tmp;
TMP = A[n-i];
A[n-i] = A[maxpos];
A[maxpos] = tmp;
/***** modified terminating line *****/
}
for (i = 0; i<n; i++)
cout<<a[i]<< "";
cout<<endl;
return 0;
}

Input

N and N integers

Output

From small to large sequence

Sample Input
8 12 35 69 84 52 54 75 65
Sample Output
12 35 52 54 65 69 75 84
HINT

The code is as follows:

#include <iostream>using namespace Std;int main () {    int a[100],i,n;    cin>>n;    for (i=0; i<n; i++)        cin>> a[i];    Int J, Minpos, Maxpos, tmp;    for (i=0; I <n/2; i++)    {        minpos = i;        Maxpos = i;        for (j= i+1; j<= n-i-1; j + +)        {            if (A[j] > A[maxpos])            {                maxpos = j;                Continue;            }            if (a[j]< A[minpos])                minpos = j;        }        TMP = A[i];        A[i] = A[minpos];        A[minpos] = tmp;        if (maxpos==i)        {        tmp = A[minpos];        A[minpos] = a[n-i-1];        A[N-I-1] = tmp;        }        else        {        tmp = A[maxpos];        A[maxpos] = a[n-i-1];        A[N-I-1] = tmp;        }    }    for (i = 0; i<n; i++)        cout<<a[i]<< "";    cout<<endl;    return 0;}

Operation Result:



OJ the "Binary choice sort" of brush question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.