Think:
水题!!!!绝对的水题 写两个 排序函数 然后计数就好了~~~~
Problem Description
冒泡排序和快速排序都是基于”交换”进行的排序方法,你的任务是对题目给定的N个(长整型范围内的)整数从小到大排序,输出用冒泡和快排对这N个数排序分别需要进行的数据交换次数。
Input
连续多组输入数据,每组数据第一行给出正整数N(N ≤ 10^5),随后给出N个整数,数字间以空格分隔。
Output
输出数据占一行,代表冒泡排序和快速排序进行排序分别需要的交换次数,数字间以1个空格分隔,行末不得有多余空格。
Example Input
8
49 38 65 97 76 13 27 49
Example Output
15 9
Hint
注意:数据相等时不做交换
#include<bits/stdc++.h>
using namespace std;
int cnt;
int kase;
int a[100050];
int b[100050];
void qsort(int a[], int L, int R)
{
int i, j;
int x = a[L];
i = L;
j = R;
if (L >= R)
return ;
while(i < j)
{
while(i < j && a[j] >= x) j --;
if (a[i] != a[j])
cnt ++;
a[i] = a[j];
while(i < j && a[i] <= x) i ++;
if (a[i] != a[j])
cnt ++;
a[j] = a[i];
}
a[i] = x;
qsort(a, L, i - 1);
qsort(a, i + 1, R);
}
int main()
{
int n;
int i, j;
while(cin >> n)
{
cnt = 0;
kase = 0;
for (i = 0;i <= n - 1;i ++)
{
cin >> a[i];
b[i] = a[i];
}
qsort(a, 0, n - 1);
for (i = 0;i <= n - 2;i ++)
for (j = 0;j <= n - i - 2;j ++)
{
if (b[j] > b[j + 1])
{
kase ++;
int t;
t = b[j];
b[j] = b[j + 1];
b[j + 1] = t;
}
}
printf("%d %d\n", kase, cnt);
}
}