Insertion sort of iOS algorithm (iii)

Source: Internet
Author: User

The basic idea of a direct insert sort (Insertion sort) is to insert a record to be sorted each time, by its keyword size, into the appropriate position in the previously ordered subsequence until all the records have been inserted.

set the array to a[0...n-1].

1. initial time. A[0] self into 1 ordered areas, unordered area is a[1..n-1]. Make I=1

2. merge A[i] into an ordered interval of A[0...I] formed in the current ordered region A[0...i-1] .

3. i++ and repeat the second step until the I==n-1.

Sorting is complete.


Code implementation:

//

Main.m

algorithm ---- insertion sort (insertion sort)

Copyright (c) year [email protected] All rights reserved.

//


#import<Foundation/Foundation.h>

int Main (int argc,const Char * argv[])

{

int array[] = {3,2, 6, 9, 8, 5, 7, 1, C10>4};

to add portability ( take sizeof ()), count the number of array elements

int count = sizeof(array)/sizeof(array[0]);

Record one by one, insert an ordered series

for (int i = 1; i < count; i++) {

int j = i; //j is a pit that determines the position of the pit, and then takes the number out of the pit, paying attention to the order

int temp = array[i]; //temp is to take a number from the pit.

//Put A[i] into an ordered sequence

while (J > 0 && Temp < array[j-1]) { //j > 0 Prevent cross-border. Write && front more efficient

ARRAY[J] = array[j-1];

j--;

}

ARRAY[J] = temp;

}

for (int i = 0; i < count; i++) {

printf ("[%2d]:%d\n", I, array[i]);

}

return 0;

}


Attached: Efficiency analysis

Stability
Space complexity O (1)
Time Complexity O (n2)
Worst case: reverse order. Need to move N (n-1)/2 elements
Best case: Positive order, no need to move elements

When the array is sorted or " approximate sort " . The best time to insert sort efficiency is O (n).

Insert sort worst-case execution time and average execution time are O (n2).

In general, the insertion sort renders the best performance in two sorting algorithms.

For lists with fewer elements, such as n<=15, the two-time algorithm is very effective.

When the list has been sorted, the insertion sort is the linear algorithm O (n).

When the list is " approximate sort " . The insertion sort is still a linear algorithm.

When many elements of the list are already in the correct position. The " approximate sort " condition will appear.

Partial ordering of arrays by using an O (Nlog2N) efficiency algorithm (such as high-speed sorting)

Then select Sort, and some advanced sorting algorithms are implemented in this way.

It can be seen from the above analysis that the direct insertion sort is suitable for the case that the number of records is less and the given sequence is basically orderly.


Insertion sort of iOS algorithm (iii)

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.