Algorithm precision (1)-insert sorting, algorithm precision insert sorting

Source: Internet
Author: User

Algorithm precision (1)-insert sorting, algorithm precision insert sorting

<Pre name = "code" class = "cpp"> <span style = "background-color: rgb (255,255,255 ); "> // insert sorting /// O (n ^ 2). When one element is inserted in the incremental sequence, O (n) # include" sort. h "void issort (void * data, int size, int esize, int (* compare) (const void * key1, const void * key2) {int I, j; int * a = (int *) data; int * key; // request space for the key if (key = (int *) malloc (sizeof (char )))! = NULL) {return;} // initialize jj = 1; while (j <= size) // j is the element to be sorted, move {// key as the cache memcpy (key, & a [j], sizeof (int); I = j-1; // I starts from the left position of j while (I <= j) {if (compare (key, & a [I]) <0) // a [j] <a [I] {memcpy (& a [I + 1], & a [I], sizeof (int )); // move the entire array to the right by position I --;} // insert a [j] to the correct position memcpy (& a [I + 1], key, sizeof (int); j ++;} free (key); return ;}</span>




Insert sorting algorithm?

Insert sorting:
Compare the new data with the sorted data one by one. If it is larger (smaller) than a specific data, the new data will be placed behind the data (Front ), the rest of the data is moved back. Insert sorting is usually convenient for linked lists. Because the linked list does not need to be moved back as a whole.
Array:
Int a [10] = {6, 1, 2, 7, 4, 8, 9, 0, 5, 3 };
Int B [10] = {0}
Int I, j;
For (I = 0; I <10: I ++)
{
For (j = I; j> 0; j --)
{
If (B [J-1]> = a [I])
B [j] = B [J-1];
}
B [j] = a [I];
}

Algorithm: [insert sort] is different from [insert sort?

Insert sort directly

The method of directly inserting the sort (straight insertion sort) is:
Each time the first element is extracted from the unordered table, it is inserted to the proper position of the ordered table, so that the ordered table is still ordered.
The first step is to compare the first two numbers, and then insert the second number into an ordered table by size. The second step is to scan the third data and the first two numbers from the front to the back, insert the third number to the ordered table by size. perform the following operations in sequence. After (n-1) scanning, the entire sorting process is completed.
Directly inserted sorting is a stable sorting. The worst time complexity is O (n ^ 2), and the space complexity is O (1 ).
Direct insertion sorting is composed of two nested loops. The outer loop identifies and determines the value to be compared. The inner loop determines its final position for the value to be compared. Direct insertion and sorting compare the value to be compared with the previous value, so the outer loop starts from the second value. When the current value is greater than the value to be compared, continue the loop comparison until a value smaller than the value to be compared is found and the value to be compared is placed in the next position to end the loop.
It is worth noting that we must use a bucket to save the value to be compared, because when the comparison is complete, we need to place the last digit of the value to be smaller than the value to sort the cards like playing cards. The basic way to insert sorting is: each step inserts a record to be sorted into the appropriate position in the previously sorted sequence according to the size of its keyword until all records are inserted.

Insert sort

There is an ordered data sequence that requires inserting a number in the sorted data sequence, but the data sequence is still ordered after insertion, in this case, we need to use a new sorting method-insert sorting method. The basic operation of insert sorting is to insert a data to the sorted data, in this way, a new ordered data with a number plus one is obtained. The algorithm is suitable for sorting a small amount of data, and the time complexity is O (n ^ 2 ). Is a stable sorting method. The insert algorithm divides the array to be sorted into two parts: the first part contains all elements of the array, except the last element, and the second part contains only this element. After sorting the first part, insert the last element to the position in the first part of the order.

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.