【Aizu ALDS1_2_C --- Stable Sort】

【Aizu ALDS1_2_C --- Stable Sort】


#include <iostream>
using namespace std;

struct Node
{
    char ch;
    int x;
}a[40],b[40];

void bubble_sort(int n)
{
    for(int i=0;i<n;i++)
    {
        for(int j=n-1;j>i;j--)
        {
            if(a[j].x<a[j-1].x)
                swap(a[j],a[j-1]);
        }
    }
}

void selection_sort(int n)
{
    for(int i=0;i<n;i++)
    {
        int minn=i;
        for(int j=i;j<n;j++)
        {
            if(b[j].x<b[minn].x)
                minn=j;
        }
        swap(b[i],b[minn]);
    }
}

void pr(Node arr[],int n)
{
    for(int i=0;i<n;i++)
        cout << arr[i].ch << arr[i].x << (i==n-1?'\n':' ');
}

bool compare(int n)
{
    for(int i=0;i<n;i++)
    {
        if(!(a[i].x==b[i].x && a[i].ch==b[i].ch))
        {
            return false;
        }
    }
    return true;
}

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for(int i=0;i<n;i++)
    {
        cin >> a[i].ch >> a[i].x;
        b[i]=a[i];
    }
    bubble_sort(n);
    pr(a,n);
    cout << "Stable" << endl;
    selection_sort(n);
    pr(b,n);
    if(compare(n))
        cout << "Stable" << endl;
    else
        cout << "Not stable" << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章