Review sorting algorithm

Source: Internet
Author: User
Tags array length

One. Bubble sorting algorithm

1. The first sorting will be the sequence [0 ~ n-1] in the previous two adjacent elements of the comparison, if the former larger exchange, compare n-1 times;
When the first trip is finished, the largest element of the sequence is swapped to position n-1, like a bubble, tumbling back and forward until the last one.

2. Repeat step 1, I need to roll the n-i-1 times, each trip to determine the location of an element, altogether need to n-1 trip.

For example, the initial sequence: [1, 5, 4, 3, 2]

1th trip: [1, 4, 3, 2] 5

2nd trip: [1, 3, 2] 4, 5

......

- (void) Bubblesort: (Nsmutablearray *) array{intI, Y; BOOL bfinish=YES;  for(i =1; i<= [Array Count] && bfinish; i++) {bfinish=NO;  for(Y = (int) [Array count]-1; y>=i; y--) {            if([[[[Array objectatindex:y] intvalue] < [[Array objectatindex:y-1] (Intvalue]) {[Array exchangeobjectatindex:y-1Withobjectatindex:y]; Bfinish=YES; }        }    }}

Second, simple selection sorting algorithm

Simple selection of the basic idea of sorting:

1. The first trip is to find the minimum element in the initial sequence [0 ~ n-1] and swap the position with the element at position 0.

2. The first trip is performed in sequence [I-1, n-1] to find the minimum element and the position of the element exchange position i-1. Each time the position of an element is determined, the initial sequence is ordered after a n-1 trip.

For example, the initial sequence: [1, 5, 4, 3, 2]

1th trip: 1 [5, 4, 3, 2]

2nd trip: 1 2 [4, 3, 5]

......

-(void) Selectsort: (Nsmutablearray *) array{    int  I, J;      for (i=0; i<array.count-1; i++) {                int index = i;          for (j = i+1; j<array.count; j + +) {            if ([Array[j] intvalue] < [Array[index ] Intvalue]) {                = j;            }        }                [Array exchangeobjectatindex:i withobjectatindex:index];    }}

three, fast sorting algorithm (reference: Baidu Encyclopedia )

1) Set two variables I, J, at the beginning of the order: I=0,J=N-1;2) with the first array element as the key data, assigned to key, that is, key=a[0];3) from the start of J forward search, that is, starting from the forward search (j--), find the first value less than key A[j], will a[ J] and A[i] interchange; 4) backward search from I, that is, start backward search (i++), find the first a[i "greater than key], swap a[i] and a[j], 5) Repeat steps 3rd and 4 until i=j; (3,4 step, no qualifying value found, 3 in a[j] Not less than key,4 in a[i] is not larger than the time to change the value of J, I, so that j=j-1,i=i+1 until found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be exactly when the i+ or J completes, at which time the loop ends).
/** Fast Sorting Method @param array Source array @param startIndex start index @param endIndex end index*/-(void) Quicksortdataarray: (Nsmutablearray *) array Withstartindex: (Nsinteger) StartIndex Andendindex: (nsinteger) endindex{//just a basic judgment, if the array length is 0 or 1 o'clock returns.     if(StartIndex >=EndIndex) {                return ; } Nsinteger I=StartIndex; Nsinteger J=EndIndex; //Record Baseline numberNsinteger key =[Array[i] integervalue];  while(I <j) {/** * * start looking for values smaller than the base number first from right J * **/         while(I < J && [Array[j] integervalue] >= key) {//if it is larger than the base number, continue to findj--; }        //if it is smaller than the base number, the found small value is swapped to the position of IArray[i] =Array[j]; /** * * When looking for a value that is smaller than the base number on the right, start looking for a value larger than the base number from I*/         while(I < J && [Array[i] integervalue] <= key) {//if it is smaller than the base number, continue to findi++; }        //if it is larger than the base number, swap the found large value to the position of JARRAY[J] =Array[i]; }        //Place the base number in the correct positionArray[i] =@ (key); /** * * recursive sorting * **/    //to the left of the sort datum number[Self Quicksortdataarray:array withstartindex:startindex andendindex:i-1]; //to the right of the sort datum number[Self Quicksortdataarray:array withstartindex:i +1Andendindex:endindex];}

more sorting algorithm parsing(7 of the most basic)

Review sorting algorithm

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.