HDOJ1872(稳定排序)

稳定排序

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1066    Accepted Submission(s): 408


Problem Description
大家都知道,快速排序是不稳定的排序方法。
如果对于数组中出现的任意a[i],a[j](i<j),其中a[i]==a[j],在进行排序以后a[i]一定出现在a[j]之前,则认为该排序是稳定的。

某高校招生办得到一份成绩列表,上面记录了考生名字和考生成绩。并且对其使用了某排序算法按成绩进行递减排序。现在请你判断一下该排序算法是否正确,如果正确的话,则判断该排序算法是否为稳定的。
 

Input
本题目包含多组输入,请处理到文件结束。
对于每组数据,第一行有一个正整数N(0<N<300),代表成绩列表中的考生数目。
接下来有N行,每一行有一个字符串代表考生名字(长度不超过50,仅包含'a'~'z'),和一个整数代表考生分数(小于500)。其中名字和成绩用一个空格隔开。
再接下来又有N行,是上述列表经过某排序算法以后生成的一个序列。格式同上。
 

Output
对于每组数据,如果算法是正确并且稳定的,就在一行里面输出"Right"。如果算法是正确的但不是稳定的,就在一行里面输出"Not Stable",并且在下面输出正确稳定排序的列表,格式同输入。如果该算法是错误的,就在一行里面输出"Error",并且在下面输出正确稳定排序的列表,格式同输入。

注意,本题目不考虑该排序算法是错误的,但结果是正确的这样的意外情况。
 

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

 

 

//1352356 2009-05-09 15:17:34 Accepted 1872 31MS 288K 1634 B C++ Xredman 
#include <iostream>
#include 
<algorithm>
using namespace std;

const int N = 305;

struct node
{
    
char name[55];
    
int score;
    
bool operator <(const node tmp) const
    
{
        
return score > tmp.score;
    }

}
;

node stu[N];
int n;

void BubbleSort()
{//本程序用于实现冒泡排序
    int i, j;
    node tmp;
    
bool flag;
    
    
for(flag = true, i = n - 1; i >= 0 && flag; i--)
    
{
        flag 
= false;
        
for(j = 0; j < i; j++)
            
if(stu[j].score < stu[j + 1].score)
            
{
                tmp 
= stu[j];
                stu[j] 
= stu[j + 1];
                stu[j 
+ 1= tmp;
                flag 
= true;//若一次冒泡过程中为发生数据交换,
            }
                 //可找到此数组已经有序存放
    }

}



int main()
{
    
int i;
    
bool is_unstable, is_wrong;
    
    
char name[N];
    
int score;
    
    
while(cin>>n)
    
{
        
for(i = 0; i < n; i++)
            cin
>>stu[i].name>>stu[i].score;
        
//sort(stu, stu + n);
        BubbleSort();
        
        
//////////I will test Bubble sort/////
        
        
/*for(i = 0; i < n; i++)
            cout<<stu[i].name<<"     "<<stu[i].score<<endl;
*/

        
        is_unstable 
= false; is_wrong = false;
        
for(i = 0; i < n; i++)
        
{
            cin
>>name>>score;
            
if(is_wrong) continue;
            
            
if(score == stu[i].score)
            
{
                
if(strcmp(name, stu[i].name) != 0)
                    is_unstable 
= true;
            }

            
else  is_wrong = true;
        }

        
        
if(!is_unstable && !is_wrong)
            cout
<<"Right"<<endl;
        
else 
        
{
            
if(is_wrong)
                cout
<<"Error"<<endl;
            
else
                cout
<<"Not Stable"<<endl;
            
for(i = 0; i < n; i++)
                cout
<<stu[i].name<<" "<<stu[i].score<<endl;
        }

        
    }

    
return 0;
}

posted on 2009-05-09 15:28  Xredman  阅读(260)  评论(0编辑  收藏  举报

导航