OBJECT-C algorithm of sorting algorithm

Source: Internet
Author: User


This article turns from: http://www.cocoachina.com/ios/20170803/20117.html

Directory

* Select sort

* Bubble Sort

* Insert Sort

* Quick Sort

* Two-way fast sorting

* Three-way quick sorting

* Heap Sort

* Summary and Harvest

* Reference and Reading

Select Sort

Selection sorting is a simple and intuitive sort algorithm, regardless of what data is in the time complexity of O (n?). So when you use it, the smaller the data, the better. The only benefit may be that you don't take up the extra memory space.

1. Algorithm steps

1. First find the smallest (large) element in the unordered sequence, and hold it to the starting position of the sort sequence.

2. Then continue to look for the smallest (large) element from the remaining unsorted elements and place it at the end of the sorted sequence.

3. Repeat the second step until all the elements are sorted.

2. Code implementation

/**
 ? Ascending select Sort
 /
+ (void) Upcompartorandlogwitharrray: (Nsarray *) array{
    Nsmutablearray*newarray = [ Nsmutablearray Arraywitharray:array];
    for (int i=0; i<newarray.count-1; i++) {//1,6,2,9,3
        id min = newarray[i];
        for (int j = i+1 j<newarray.count; J + +) {
            if (min > Newarray[j]) {
                id temp = min;
                min = newarray[j];
                NEWARRAY[J] = temp;
            }
        }
        newarray[i]=min;
    }
    NSLog (@ "Sorted in ascending order array =%@", NewArray);
}

Bubble Sort

Bubble sort (Bubble sort) is also a simple and intuitive sort algorithm. It repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if their order is wrong. The task of visiting the series is repeated until no further exchange is needed, which means that the sequence has been sorted. The algorithm is named because the smaller elements float slowly through the exchange to the top of the sequence.

1. Algorithm steps

1. Compare adjacent elements. If the first one is bigger than the second one, swap them both.

2. For each pair of adjacent elements to do the same work, from the beginning to the end of the first pair. After this step, the final element will be the maximum number.

3. Repeat the above steps for all elements except the last one.

4. Continue to repeat the steps above for fewer elements at a time until no pair of digits need to be compared.

2. Code implementation

/**
 Bubble Sort method
 @param array to sort arrays of */
+ (void) Bubblesortwitharray: (Nsarray *) array{
    Nsmutablearray * NewArray = [Nsmutablearray Arraywitharray:array];
    for (int i=0;i<newarray.count;i++) {for
        (int j=0;j<newarray.count-i-1;j++)
        {
            if (newarray[j]> Newarray[j+1])
            {
                id temp;
                temp = Newarray[j];
                NEWARRAY[J] = newarray[j+1];
                NEWARRAY[J+1] = temp;
            }
    }} NSLog (@ "bubble sort order in ascending order =%@", NewArray);
}


Insert Sort

Insert Sort code Implementation Although there is no bubble sort and choice of sorting so simple and rough, but its principle should be the easiest to understand, because as long as playing poker people should be able to understand seconds. Insertion sequencing is the simplest and most intuitive sort algorithm, which works by building ordered sequences, scanning the sorted sequence for unsorted data, and then locating and inserting the corresponding position.

1. Algorithm steps

1. Consider the first element of the first order sequence as an ordered sequence, and the second element to the last element as an unordered sequence.

2. Scans the unordered sequence sequentially, inserting each element that is scanned into the appropriate location of the ordered sequence. (If the element to be inserted is equal to an element in an ordered sequence, insert the element to be inserted after the equality element.) )

2. Code implementation

/**
 Insert Sort method
 @param array to sort arrays of
 */
+ (void) Insertcompartorwitharray: (nsarray*) array{
    Nsmutablearray * NewArray = [Nsmutablearray Arraywitharray:array];
    for (int i = 1;i < newarray.count;i++) {
        id temp = newarray[i];
        Int J;
        for (j = i-1;j>=0 && Temp < newarray[j];j--) {
            newarray[j+1] = newarray[j];
        }
        NEWARRAY[J+1] = temp;
    }
    NSLog (@ "Insert Sort method =%@", NewArray);
}


Merge Sort

Merge sort is an efficient sorting algorithm based on merging operations. The algorithm is a very typical application of the partition method (Divide and Conquer).

As a typical algorithm application of divide and conquer, the realization of merge sort is two ways:

>1. Top-down recursion (all recursive methods can be rewritten with iterations, so there's a 2nd way)

>2. a bottom-up iteration;

This article uses the merge sort of * * from top to bottom *

1. Algorithm steps

1. Application space so that its size is the sum of two sorted sequences, which are used to store the merged sequence;

2. Set two pointers, the initial position is two already sorted sequence starting position;

3. Compare the elements pointed to by the two pointers, select the relatively small elements into the merged space, and move the pointer to the next position;

4. Repeat step 3 until a pointer reaches the end of the sequence;

5. Copy all remaining elements of another sequence directly to the end of the merge sequence.

2. Code implementation

1 2 3 4 5 6 7 8 9

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.