2.1.3 Sorting a three-valued Sequence three-value sorting

Source: Internet
Author: User

2.1.3 Sorting a three-valued Sequence three-value sorting
I. Description of the topic

Sorting is a very frequent calculation task. Now consider a maximum of only three values for the sort problem. A practical example is when we give a winner of a competition a gold and silver medal.
The possible values in this task are only three and 3. We put him in ascending order in 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.
Program NAME: Sort3
INPUT FORMAT
Line 1:n (1 <= N <= 1000)
Lines 2-n+1: One number per line, total N rows. (1.2.3)
SAMPLE INPUT (File sort3.in)
9
2
2
1
3
3
3
2
3
1
OUTPUT FORMAT
A total of one row, a number. Represents the minimum number of interchanges required to rank in ascending order.
SAMPLE OUTPUT (File sort3.out)
4

Second, the idea of solving problems

test Instructions The minimum number of moves required for sorting, you can sort the input numbers first, and then get a different place, which is where we need to swap.

Modeling, it is important to be aware of the idea of modeling. This is our highly mathematical abstract description of the topic and must be a highly refined analysis and understanding of the topic. This is of great significance for us to clear up our ideas and solve the problems, which is the premise of our programming to solve the problems. It is emphasized that when we do the problem, we should always keep in mind the description, overview, abstraction and abstract representation of the topic through Mathematical modeling .

a combination (A, B) indicates that a position should be a B after the sort, but before a. The original example we get (2,1), (2,1), ((), (3,2), (3,2), (2,3), (1,3) and then the combination can be composed of rings, the result is the length of all rings and minus the number of rings. The general principle is to find the ring to reduce the number of exchanges.

(2,1),---ring length is 2 (2,1), (1,3), (3,2)---ring length is 3 (3,2), (2,3)---ring length is 2 results 2+3+2-3=4
Ring splits of length 3 (no order, typically in storage index order):
(2,1), (1,3), (3,2)----split into (2,1), (1.3) and (3,2), (2,3). In two-step exchange, the second step is to form a 2-length ring.

Further analysis because the topic has only three numbers of sorts, the ring length that is formed for all permutation interchanges can only be 2 or 3 (special). So I began to look for the ring, I first look for the length of 2 ring, each ring exchange number of 1, and then find the length of the ring 3, I put the length of the 3 ring split into two-step exchange (clever), (can be any two combination first Exchange), the first Exchange, will form a length of 2 ring. Above


Source code (for reference only)

#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;int n;int seq1[50+10], Seq2[50+10];//,vis[50+10]int Cnt=0;int Main () {freopen ("sort3.in", "R", stdin); Freopen ("Sort3.out", "w", stdout); CIN >>n;int i,j;for (i=0;i<n;i++) {cin>>seq1[i];seq2[i]=seq1[i];} Sort (seq1,seq1+n); 5\7int temp;for (i=0;i<n;i++) {if (Seq2[i]!=seq1[i]) {//&&seq2[i]!=-1 for (j=i+1;j<n;j++) {// Calculates all rings of length 2, number of interchanges plus 1 if (Seq2[j]==seq1[i]&&seq2[j]!=seq1[j]&&seq2[i]==seq1[j]) {cnt=cnt+1;temp=seq2[ I];seq2[i]=seq2[j];seq2[j]=temp;break;}}}}               for (i=0;i<n;i++) {if (seq2[i]!=seq1[i)} {for (j=i+1;j<n;j++) {//Calculates a ring with a length of 3, splits the two-step interchange (3-length loops into two 2-length rings)   if (Seq2[j]==seq1[i]&&seq2[j]!=seq1[j]) {cnt=cnt+1;temp=seq2[i];seq2[i]=seq2[j];seq2[j]=temp;break;}   }}}cout<<cnt<<endl;return 0; }</span></span></span>

Because it is a beginner, the programming ability is limited, does not reach the level of professional programmers, may mislead everyone, please read it, the text editing is also general, the text may have inappropriate wording. Blog post the mistakes and shortcomings of the reader, please criticize.


2.1.3 Sorting a three-valued Sequence three-value sorting

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.