Sorting a three-valued Sequence

Source: Internet
Author: User

Sorting is a very frequent computational task. Now consider sorting problems with up to three values. A practical example is when we give a winner of a competition the gold and silver bronze medal order. The possible values in this task are only three and 3. We put him in ascending order by means of an exchange. Write a program that calculates the minimum number of exchanges required in ascending order, given a sequence of numbers made up of a series of a.

I thought it was some high-end math. Although it is, it does not.

So it's a simple question.

The original array is called A, and the ascending sorted array is called B.

"B arrays are divided into 1 2 33 zones"

So

Then our swap is divided into two categories.

1-2 2-3 1-3 Direct one-time swap (need 1 ci per this GUI)

Rotation between and needs (2 ci per this GUI)

"The rest of the class is already in their own area, do not need to mobilize"

So answer = x + y/3+2 (where x is the logarithm to be swapped in one way (note is logarithmic), Y is the number of digits in all numbers that require a rotation of mode 2)

(also because each swap can solve 3 number of problems so a total of y/3 rotation, but each time the number of times to 2, so multiplied by 2 is spent)

Code:

#include <stdio.h>inta[1001];intCompareintS1,intE1,intNUM1,intS2,intE2,intnum2) {    inti,j,sum=0;  for(I=s1,j=s2;i<s1+e1 && j<s2+e2;i++,j++){        if(a[i]==num2 | | a[j]==NUM1) {            if(a[j]!=NUM1) {                 for(; j<s2+e2;j++)                    if(a[j]==num1) Break; }            Else if(a[i]!=num2) {                 for(; i<s1+e1;i++)                    if(a[i]==num2) Break; }            if(I==s1+e1 | | j==s2+E2) Break; A[i]^=A[j]; A[J]^=A[i]; A[i]^=A[j]; Sum++; }    }    returnsum;}voidSolveintN) {    inti,sum=0, count=0; intnum[3]={0};  for(i=0; i<n;i++) Num[a[i]-1]++; Sum+=compare (0, num[0],1, num[0],num[1],2); Sum+=compare (num[0],num[1],2, num[0]+num[1],num[2],3); Sum+=compare (0, num[0],1, num[0]+num[1],num[2],3);  for(i=0; i<num[0];i++){        if(a[i]!=1) Count++; }  for(; i<num[0]+num[1];i++){  if(a[i]!=2) Count++; }  for(; i<n;i++){  if(a[i]!=3) Count++; } Sum+=count/3*2; printf ("%d\n ", sum);}intMain () {intN;  while(scanf ("%d", &n)! =EOF) {        inti;  for(i=0; i<n;i++) scanf ("%d ",&A[i]);    Solve (n); }    return 0;}

Sorting a three-valued Sequence

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.