Code Monkey home page Code Monkey logo

sdalgorithm's Introduction

SDAlgorithm

Put some algorithms that use in Objective-C on git.eg:QuickSort Algorithm,HeapSort Algorithm,MergeSort Algorithm.

// Quicksort Algorithm(快排算法)
/**
 * 思路:快速排序是在冒牌排序的基础上改进的方法
 1.将序列的第一个元素作为比较的基准值key
 2.从最右端开始和key值比较,找到第一个小于key的值,将该值赋值给[i],此时j记录的是右侧第一个比key小的值的index
 3.再从最左端开始查找第一个大于key的值,并将该值赋给上面的[j],这样就完成了一次比较,直到i == j,比较结束。
 4.此时将基准数key放到赋给[i],此时序列被分为两组,比key值小的在左边,比key值大的放在右边,在此基础上,再分别递归两组进行排序。
 优点:速度很快 时间复杂度最好情况下是O(nlogn) 最坏情况是O(n^2)
 缺点:不稳定
 */
- (void)quicksortAlgorithm:(NSMutableArray *)numbers leftIndex:(NSInteger)leftIndex rightIndex:(NSInteger)rightIndex {
    if (leftIndex >= rightIndex) {//如果数组长度为0或1时返回
        return ;
    }

    NSInteger i = leftIndex;
    NSInteger j = rightIndex;
    
    // Use the first value as compare key
    NSInteger key = [numbers[i] integerValue];
    
    while (i < j) {
         /**** 首先从右边j开始查找比基准数小的值 ***/
        while (i < j && [numbers[j] integerValue] >= key) {
            j--;
        }
        NSLog(@"i = %ld, j = %ld , numers[%ld] = %ld",i, j, j,[numbers[j] integerValue]);
        // 如果比基准数小,则将查找到的小值调换到i的位置
        numbers[i] = numbers[j];
        
        /**** 当在右边查找到一个比基准数小的值时,就从i开始往后找比基准数大的值 ***/
        while (i < j && [numbers[i] integerValue] <= key) {
            i++;
        }
        //如果比基准数大,则将查找到的大值调换到j的位置
        numbers[j] = numbers[i];
    }
    //将基准数放到正确位置
    numbers[i] = @(key);
    /**** 递归排序 ***/
    //排序基准数左边的
    [self quicksortAlgorithm:numbers leftIndex:leftIndex rightIndex:i - 1];
    //排序基准数右边的
    [self quicksortAlgorithm:numbers leftIndex:i + 1 rightIndex:rightIndex];
}

sdalgorithm's People

Contributors

xlsd avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.