One, what is the direct insertion sort:
The first one compares the top two numbers, the second number is then inserted into an ordered table by size; The second one scans the third data with the first two digits, inserting the third number into an ordered table by size, and then, in turn, completes the entire sorting process after the (n-1) scan.
In simple terms, the array to be sorted is divided into order
As a sort algorithm, insertion sorting is stable. Time Complexity: O (N * n), space complexity: O (1 ). The best time complexity is O (n), and the worst and average is O (n * n ).
Algorithm idea: given an unordered array, we start with its second number and assign it to the mark variable to compare the size of mark with the previous number, if the number is smaller than the previous one, move the previous o
Actionscript3.0 insert sorting implementation
/** * ● The basic idea of insertion sort is to insert a record to be sorted each time. * Insert the record to the appropriate position in the subfile that has been sorted according to the keyword size, * Until all records are inserted. * */ Public Static Function insertionsort (Source: array): Array {var Len: Int = Source. length; For (Var I
"How many inverted pairs"-so usually ends up with mergesort solution (of course there is other solutions out there)defmergesort (arr):ifLen (arr) = = 1: return0, arr mid= Len (arr)//2Cnt1, arr1=mergesort (Arr[:mid]) Cnt2, arr2=mergesort (arr[mid:])#RET =[] CNT=0 I=0 J=0 Inx=0 whileI andJ Len (arr2):ifArr1[i] Arr2[j]: Ret.append (arr1[i]) I+ = 1elifARR2[J] Arr1[i]: Ret.append (arr2[j]) CNT+ = Len (arr1)-I#arr2-arr1J + = 1 whileI Len (arr1): Ret.append (Arr1[i]) I+ = 1 whileJ Len (arr2): R
introduction of direct insertion sort
Each time the first element is removed from the unordered table, it is inserted into the proper position of the ordered table, so that the ordered table is still orderly.
The first one compares the top two numbers, the second number is then inserted into an ordered table by size; The second one scans the third data with the first two digits, inserting the third number i
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.