zoukankan      html  css  js  c++  java
  • iOS常见算法(二分法 冒泡 选择 快排)

    二分法:
    平均时间复杂度:O(log2n)
    int halfFuntion(int a[], int length, int number)
     {
    int start = 0;
    int end = length - 1;
    int index = 0;
    while(start < end)
     {
    index = start + (end - start)/2
    if(a[index] == number){
    return index;
     } else if(a[index] < number){
    start = index + 1;
     } else{
    end = index - 1;
     }
     }
    return index;
     }


    冒泡排序:

    /**

     平均时间复杂度:O(n2)

     

     空间复杂度:O(1)  (用于交换)

     

     稳定性:稳定

     */

    void paoFuntion(int a[], int length){

        for (int i = 0; i < length - 1; i++) {

            for (int j = 0; j < length - 1 - i; j++) {

                if (a[j] > a[j+1]) {

                    int temp = a[j];

                    a[j] = a[j+1];

                    a[j+1] = temp;

                }

            }

        }

    }



    选择排序:

    /**

     平均时间复杂度:O(n2)

     

     空间复杂度:O(1)  (用于交换和记录索引)

     

     稳定性:不稳定 (比方序列【5。 5, 3】第一趟就将第一个[5]与[3]交换,导致第一个5挪动到第二个5后面)

     */

    void chooseFuntion(int a[],int length){

        for (int i = 0; i < length - 1; i++) {

            for (int j = i + 1; j < length ; j++) {

                if (a[i] > a[j]) {

                    int temp = a[j];

                    a[j] = a[i];

                    a[i] = temp;

                }

            }

        }

    }


    快排:点击打开链接

  • 相关阅读:
    Equivalent Sets HDU
    Chemical table CFR500 div2D(并查集)
    How do I create an installation log?
    Error 1937.An error occurred during the installation of assembly...
    InstallShield 版本转换
    Convert CString to TCHAR
    InstallShield : 如何查找编译后的 Merge Module存放路径
    Msi.h causes compilation error in vs2010
    区间调度(贪心)
    硬币问题(贪心)
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5173017.html
Copyright © 2011-2022 走看看