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


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

    Time Limit: 1000MS Memory Limit: 65536KB

    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

    #include <stdio.h>
    int cnt;
    void qsort(int *p, int n) //快排
    {
    if(n<=1) return ;
    int i = 0,j = n-1;
    int q = p[0];
    while(i<j)
    {
    while(i<j && p[j]>=q)
    j--;p[i] = p[j];
    if(i<j) cnt++;
    while(i<j && p[i]<=q)
    i++;p[j] = p[i];
    if(i<j) cnt++;
    }
    p[i] = q;
    qsort(p,i);
    qsort(p+i+1,n-i-1);
    }
    void ff(int a[],int n,int &count) //冒泡
    {
    count = 0;
    for(int i=0;i<n;i++)
    {
    for(int j=0;j<n-i-1;j++)
    {
    if(a[j]>a[j+1])
    {
    int t = a[j];
    a[j] = a[j+1];
    a[j+1] = t;
    count++;
    }
    }
    }
    }


    int main()
    {
    int n;
    int a[1234],i,b[1234];
    while(~scanf("%d",&n))
    {
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]),b[i] = a[i];
    }
    int count;
    cnt=0;
    ff(a,n,count);
    qsort(b,n);
    printf("%d %d\n",count,cnt);
    }
    return 0;
    }

  • 相关阅读:
    [SDOI2015]约数个数和
    [POI2007]ZAP-Queries
    fpu栈溢出
    shader 汇编
    sample a texture as a rendertarget
    ID3d11asynchronous
    DEVICE DRAW VERTEX BUFFER TOO SMALL
    模型的一个点显示在原点
    setrendertraget 上下颠倒
    skinned mesh 蜘蛛样
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11782110.html
Copyright © 2020-2023  润新知