Quick Sort method from small to large

Source: Internet
Author: User

/**

* sort from small to large

*

* @param left 0

* @param The number of right arrays

*/

-(void) Fastsortleftindex: (nsinteger) left Withrightindex: (nsinteger)right

{

Nsinteger I, J;

if(left > right) return;

nsinteger Tempmodel = [self. Dataarr[left] integervalue]; // benchmark model

i = left;

j = right;

while (i! = j) {

// starting from the right to find a model with a smaller score than the benchmark model

while (i<j && [self. Dataarr[j] IntegerValue]>tempmodel) {

j--;

}

// find from left to right

while (i < J && [self. Dataarr[i] integervalue] <= tempmodel) {

i++;

}

// If both sides have found the Exchange model

If(i < j)

{

nsinteger model_i = [self. Dataarr[i] integervalue];

nsinteger Model_j = [self. Dataarr[j] IntegerValue];

[self. Dataarr replaceobjectatindex: I withobject:@ (model_j)];

[self. Dataarr replaceobjectatindex: J withobject:@ (model_i)];

}

}

// If it meets

self. Dataarr[Left] = self. Dataarr[i];

self. Dataarr[i] = @ (tempmodel);

// After the end of the first round, the recursive - dichotomy method is used

[self fastsortleftindex: Left withrightindex:-1];

[self fastsortleftindex: i+1 withrightindex: right];

return;

// Quick Sort time complexity : N * Logn- So I prefer a quick sort of .

}

Quick Sort method from small to large

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.