STL-C - stable sort

C - stable sort

As we all know, quick sort is unstable sorting method.
If prior to the occurrence of any of the array a [i], a [j ] (i <j), where after a [i] == a [j ], performing sorting a [i] must appear in a [j] , it is considered that sort is stable.

A college admissions office to get a list of accomplishments, above the record the names of candidates and test scores. And it uses a sorting algorithm sorting in descending order by score. Now you determine what the correct sorting algorithm, if correct, it is determined whether the sorting algorithm is stable.

Input this title has multiple sets of input, processing to the end of the file.
For each test, the first line has a positive integer N (0 <N <300) , representing the number of examinees list.
Then there are N rows, each row has a string representing the name of the candidate (length of not more than 50, comprising only 'a' ~ 'z') , and a fractional integer representing candidates (less than 500). In which the names and scores separated by a space.
Then again there are N rows, the above-described sequence is a sorted list through a later generation algorithm. Format above. Output For each test, if the algorithm is correct and stable outputs "Right" in the inside row. If the algorithm is correct but it is not stable, it outputs "Not Stable" in a row inside, and stable output correct ordering of the list, with input format below. If the algorithm is wrong, then output "Error" in a row inside, and stable output correct ordering of the list, with input format below.

Note that this title does not consider that the sorting algorithm is wrong, but the result is correct such an accident situation. Sample Input

3
aa 10
bb 10
cc 20
cc 20
bb 10
aa 10
3
aa 10
bb 10
cc 20
cc 20
aa 10
bb 10
3
aa 10
bb 10
cc 20
aa 10
bb 10
cc 20

Sample Output

Not Stable
cc 20
aa 10
bb 10
Right
Error
cc 20
aa 10
bb 10
Problem-solving ideas: the original data after proper sorting, and data subject given the sorted for comparison. (1) When the i-name, found different results, then it must be wrong sort; (2) the same results, but the names are different, it should be unstable, but should also be determined; (3) the names and scores are the same, count, count data is described in the correct order when the end is equal to n
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 int n;
 5 struct student{
 6     char name[60];
 7     int gra, d;
 8 }stu1[330], stu2[330];
 9 
10 bool cmp(student &a, student &b){
11     if(a.gra != b.gra)
12         return a.gra > b.gra;
13     return a.d < b.d;
14 }
15 
16 int main(){
17     while(~scanf("%d",&n)){
18         for(int i=0; i<n; i++){
19             scanf("%s %d",stu1[i].name, &stu1[i].gra);
20             stu1[i].d = i;
21         }
22         for(int i=0; i<n; i++){
23             scanf("%s %d",stu2[i].name, &stu2[i].gra);
24             stu2[i].d = i;
25         }
26         
27         sort(stu1, stu1+n, cmp);
28         int is = 0;
29         for(int i=0; i<n; i++){
30             int nam = strcmp(stu1[i].name, stu2[i].name);
31             if(nam == 0 && stu1[i].gra == stu2[i].gra)//名字和成绩均相同 
32                 is ++;
33             else if(nam && stu1[i].gra == stu2[i].gra)//Different names, the same results, may be unstable, determined 
34 is                  IS = - . 1 ;
 35              the else { // results are different, certainly error 
36                  IS = - 2 ;
 37 [                  BREAK ;
 38 is              }
 39          }
 40          IF ( IS == n-)
 41 is              the printf ( " Right \ n- " );
 42 is          the else {
 43 is              IF ( IS == - 2 )
 44 is                  the printf ( " Error \ n- ");
45             else
46                 printf("Not Stable\n");
47             for(int i=0; i<n; i++)
48                 printf("%s %d\n",stu1[i].name, stu1[i].gra);
49         }
50     }
51     return 0;
52 }

 

 

 

 

Guess you like

Origin www.cnblogs.com/0424lrn/p/12219914.html
Recommended