3399 数据结构实验之排序二:交换排序

来源:互联网 发布:网络接入管理软件 编辑:程序博客网 时间:2024/06/02 03:37

3399 数据结构实验之排序二:交换排序

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

冒泡排序和快速排序都是基于"交换"进行的排序方法,你的任务是对题目给定的N个(长整型范围内的)整数从小到大排序,输出用冒泡和快排对这N个数排序分别需要进行的数据交换次数。

Input

连续多组输入数据,每组数据第一行给出正整数N(N ≤ 10^5),随后给出N个整数,数字间以空格分隔。

Output

输出数据占一行,代表冒泡排序和快速排序进行排序分别需要的交换次数,数字间以1个空格分隔,行末不得有多余空格。

Example Input

849 38 65 97 76 13 27 49

Example Output

15 9

Hint

注意:数据相等时不做交换

#include <bits/stdc++.h>using namespace std;int qcount;void mp(int *a,int n){    int t, mcount = 0;    for(int i =1;i<n;i++)    {        for(int j =0;j<n-i;j++)        {            if(a[j]>a[j+1])            {                t = a[j];                a[j] = a[j+1];                a[j+1] = t;                mcount++;            }        }    }    cout << mcount << ' ';}void qsort(int *a, int l,int r){    int t = a[l];    if(l>=r) return;    int i = l,j = r;    while(i<j)    {        while(t<=a[j]&&i<j)j--;        a[i] = a[j];        if(i<j) qcount++;        while(t>=a[i]&&i<j) i++;        a[j] = a[i];        if(i<j) qcount++;    }    a[i] = t;    qsort(a,l,i);    qsort(a,i+1,r);}int main(){    int n;    int a[100000],b[100000];    while(cin>>n)    {        for(int i =0;i<n;i++)        {            cin >> a[i];            b[i] = a[i];        }        mp(a,n);        qcount = 0;        qsort(b,0,n-1);        cout << qcount << endl;    }    //cout << "Hello world!" << endl;    return 0;}