compares 1 times, the record moves 2 times. The entire process
Compare Times to
Record number of moves is
Time complexity O (n)
3.2.2 Worst case: The pending records are already in reverse order, then a sequence, the keyword comparison times I (from i-1 to 0), record Movement (i+2) times. The whole process
Compare Times to
Record number of moves is
Time complexity O (n^2)
3.2.3 Average Time complexity: O (n^2)
3.3 Stability: Stable
Binary Insert Sort1
#include /* Insert Sort basic idea: inserting records into sorted ordered tables Features: A stable sequencing method, time complexity O (n^2)*/void insertsort (int array[],int len) { int i,j; int temp; for (i =1; i //I:1 ~ L T;len (i++)temp = Array[i];for (j = i-1; J >=0; j--) { J:i-1 ~ >=0 (j--) if (temp // Assuming the element to be inserted previous sequence, the post-move element Vegetarianarray[j+1] = array[j];}else{ break;}}a
Sort
Time limit:1000ms Memory limit:32678k
Title DescriptionGive you the number of N (nInputInput data The first line is a positive integer n, and the second row has n integers.OutputOutput one line, from small to large output this n number, the middle with a space separated.Sample input51 4 3) 2 5Sample output1 2 3) 4 5#include Sort: Binary insertion
Direct Insert Sort
The idea of a direct insertion sort is easy to understand and it is like this:1. The sorted array is divided into sorted and unsorted parts, and the first element is considered to be in order at the beginning.2. Starting with the second element, find the appropriate location for the element in the sorted array and insert the location.3. Repeat
1. Insert SortThe Insert sort (insertion sort) is a simple and intuitive sort algorithm. It works by building an ordered sequence, scanning the sorted sequence for unsorted data, and finding the location and inserting it. Insert sorting on implementation, usually using in-place sorting (that is, just use the extra spac
Number of array elements: NAlgorithm idea: Divided into a set of ordered groups and a group of unordered groups, each in order from the unordered group to take a comparison with the front, if less than the former exchange, otherwise break; until the last unordered element is compared, the first order group is an array of elements, and the remainder is unordered.The code is as follows:PHP$arr=Array(232,3,44,234,456,3432,289); functionInsertsort ($arr) { for($i= 1;$iCount($arr);$i++)
The basic idea of inserting a sort (insertion sort) is to insert a record to be sorted each time, by its keyword size, into the appropriate location in the previously ordered sub-file until all records are inserted.basic ideas and pseudo-codeAfter j-1 the processing, a[1..j-1] has been in good order. J-Pass processing only a[j] into the appropriate position of l[
The insertion sort is a sort algorithm that does not change the original sequence (array), but instead creates a new sequence that operates on the new sequence.
Here is a small to large sorting as an example to explain.
Basic ideas and illustrative examples
The basic idea of inserting a sort is to add elements one b
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=12. merge A[i] int
: Simple select sort, heap sort
Merge sort
Base sort
Hurry into the topic, why call insertion sort? Insert sort divides the sequence of orders into ordered and unordered ar
The shell sorting algorithm was first proposed by the D.L shell in 1959, assuming that the elements to be sorted have n, then each insertion sort is not the same as the elements, but goes to an interval.
The shell first sets the interval to N/2, then jump to the insertion sort, and then set the interval to N/4, jump t
Sort a linked list using insertion sort.Hide TagsLinked List SortAnalysis: The chain list is divided into two parts: ordered and sorted, the order of the null end, cur inserted between preinsertion and insertion, next save Cur->next. Also, note that the use of dummy simplifies the insertion of head nodes.Time complexit
; } } if(flag) { Break; } } }According to the above bubbling implementation, the Wakahara array itself is ordered (this is the best case), only need to n-1 the comparison can be completed; In reverse order, The number of comparisons is n-1+n-2+ ... +1=n (n-1)/2, the number of exchanges and the number of comparisons are equivalent. Therefore, its time complexity is still O (n2). Overall, the bubble sort performance is st
1. Some concepts in sorting(1) Algorithmic Stability: if there are two elements a and b in the sequence to be sorted, the corresponding keywords key_a and key_b are equal, and a precedes B in the original sequence. If a sort algorithm is used, a is still in front of B, then the algorithm is called stable, otherwise it is called unstable. It is worth noting that the stability of the ranking algorithm does not measure the merits and demerits of an algor
First, binary Insert Sort ( Two-point insertion sort )the method of inserting the insertion position in the direct insertion sort to find a[i] is replaced by the binary comparison, and the binary
Topic:1.sort a linked list using insertion sort2. Merge sorted linked lists and return it as a new list. The new list should is made by splicing together the nodes of the first of the lists.3. Sort A linked list inO(NLogN) time using constant space complexityThree questions on the Leetcode, summarized in one piece. I think the three problems have a layer of progr
//record backward, high means that each time it is compared from right to left in an ordered interval - for(j = i-1; J >= High +1; j--) { tor[j+1] =R[j]; + } -r[high+1] = tmp;//Inserting Records the } *} Three, algorithm analysis Time complexity: By the algorithm code is known, the binary insertion sort consists of two loops, for n sort
Use the Insert sort to sort the random ordinal group.Analysis: The idea of inserting sort ()The time complexity of the insertion sort. ()The stability of the insertion sort.Idea of inserting sort:The idea of inserting a
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.