zoukankan      html  css  js  c++  java
  • 13SCPCa201710161-排序(1)

    因为之前基础十分薄弱所以决定从头到尾重新学习一遍算法

    第一部分是排序啦

    洛谷训练场第一题、快速排序模板

    Qsort is a kind of Divide and Conquer algorithm.

    The main idea is: choose the pivot, and rearrange the array into a new one with the tag of position of pivot that all elements smaller than the pivot are in the lower position while the bigger ones are higher.

    I will have a try according to the simple explanation.

    Firstly, determine the functions that are needed

      1)qsort();

      2)rearr();

    in qsort, the array is divided into  two equal parts, and then be qsorted respectively.

    in rearr, all elements that smaller than pivot are put before pivot and the tag of pivot is returned.

    This is the first code

    #include <stdio.h>
    int shuz[10000],n;
    int zhoux(int arr[],int zuo,int you)
    {
        int temp,now=zuo;
        int zhou;
        zhou=arr[you];
        for(int i=zuo;i<you;i++)
        {
            if(arr[i]<=zhou)
            {
            temp=arr[now];
            arr[now]=arr[i];
            arr[i]=temp;//一开始把temp写成arr[now]了mdzz
            now++;
            }
        }
        arr[you]=arr[now];
        arr[now]=zhou;
        return now;
    }
    int qsort(int arr[],int zuo,int you)
    {
        if(you<=zuo)return 0;//妈个巴子这符号写反了
        int mid=zhoux(arr,zuo,you);
        qsort(arr,zuo,mid-1);//把-1写到zuo后面了。。。
        qsort(arr,mid+1,you);
        return 0;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        scanf("%d",&shuz[i]);
        qsort(shuz,0,n-1);
        for(int i=0;i<n;i++)
        printf("%d ",shuz[i]);
        return 0;
    }

    Obviously, the time taken formula is:

    T(n)=T(k)+T(n-k-1)+theta(n)//theta(n) is the time cost of partition.

    So the final formula is represented as the sum of mant theta.

     Average time complexity: O(nLogn), worse: O(n2)[every extreme number is in the pivot point, or to say it is sorted or interspersed by extreme number]

    if there are many redundant elements, 3-Way QuickSort can be used.

    main idea:

    collect recurring pivots and put into middle together.

    Example:

    #include <stdio.h>
    int shuz[10000],n;
    int zhoux(int arr[],int zuo,int you)
    {
        int temp,now=zuo;
        int zhou,rec=0;
        zhou=arr[you];
        for(int i=zuo;i<you;i++)
        {
            if(arr[i]==zhou)
            {
                rec++;
                if(arr[you-rec]==zhou)break;//加了这句话就对了,但是去掉并不是无限循环真难懂??????????????????←←难懂个屁咧、rec变得足够大不就行了嘛hhhhh
                arr[i]=arr[you-rec];
                arr[you-rec]=zhou;
                i--;
            }
            if(arr[i]<zhou)
            {
            temp=arr[now];
            arr[now]=arr[i];
            arr[i]=temp;//一开始把temp写成arr[now]了mdzz
            now++;
            }
        }
        for(int i=0;i<=rec;i++)//一开始是i<rec第一次出错竟然改成了i=-1hhhhhh
        {
        arr[you-i]=arr[now+i];
        arr[now+i]=zhou;
        }
        return now;
    }
    int qsort(int arr[],int zuo,int you)
    {
        if(you<=zuo)return 0;//妈个巴子这符号写反了
        int mid=zhoux(arr,zuo,you);
        qsort(arr,zuo,mid-1);//把-1写到zuo后面了。。。
        qsort(arr,mid+1,you);
        return 0;
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        scanf("%d",&shuz[i]);
        qsort(shuz,0,n-1);
        for(int i=0;i<n;i++)
        printf("%d ",shuz[i]);
        return 0;
    }

    There is also an iterative way of Qsorting that use stack to record the zuos and yous.

    学习资源:

    洛谷网站: https://www.luogu.org/problem/show?pid=1177

    GeekforGeeks: http://www.geeksforgeeks.org/quick-sort/

    小工具:

    在线IDE: http://ide.geeksforgeeks.org/index.php

    New words:

    implementation  redundant  reservation  attach  theta  recursive  partition  traverse  pseudo  implement  pivot  occurrence  recursively  iterative

  • 相关阅读:
    .net Thrift 之旅 (一) Windows 安装及 HelloWorld
    软件测试中的过度设计
    血型和测试
    功能点算法及在软件测试中的应用Part2
    尘归尘 土归土
    解读SAO文化中的Share
    使用C#开发winform程序的界面框架
    怎样才能说明软件测试工作做的好?
    功能点算法及在软件测试中的应用Part1
    软件测试的核心价值是什么?
  • 原文地址:https://www.cnblogs.com/endofworld/p/7676714.html
Copyright © 2011-2022 走看看