Data structure--sorting related issues

Source: Internet
Author: User

The number of comparisons for any n keyword sort is at least log2 (n!).
1, for any of the 7 keywords based on the comparison of the sorting, at least 13 times between the keywords 22 comparison.

First, the direct insertion of the basic idea of sorting a record to be sorted each step, by its sequential code size inserted into the previous sorted sequence of words in the appropriate position until all the insertions are sorted out. The time complexity of direct insertion sorting is O (n^2), and the space complexity is O (1). Code implementation
Class sorttest{ Public Static void Main(string[] args) {intarr[]={8,6,2,3,7,4};        SOP (ARR);    Insert_sort (arr); } Public Static void Insert_sort(intArr[]) { for(intI=1; i<arr.length;i++) {intj = I1;intkey = Arr[i];//Sentry used to remember the element to be inserted             while(j>=0&&arr[j]>key)//Judging conditions{arr[j+1]=ARR[J];            j--; } arr[j+1]=key;        SOP (ARR); }    } Public Static void SOP(intArr[]) { for(intI=0; i<arr.length;i++) {System. out. Print (arr[i]+" "); } System. out. println (" "); }}

Results:
8 6 2 3 7 4
6 8 2 3 7 4
2 6 8 3 7 4
2 3 6 8 7 4
2 3 6 7 8 4
2 3 4 6 7 8

Second, the basic idea of binary insertion sort

1. In the direct insert sort, find the location to be inserted pos by binary Lookup
2, pos+1 to i-1 these elements move back one position
3. Copy the elements to be sorted to Pos

Code implementation
Class Search1 { Public Static void Main(string[] args) {intarr[]={8,5,3,5,6,7};            SOP (ARR);            Binary_insert_sort (arr);        SOP (ARR); } Public Static void Binary_insert_sort(intArr[]) {intI,j,key; for(i=1; i<arr.length;i++) {key = Arr[i];intLow =0;intHigh = i1; while(Low<=high) {intMid = (Low+high)/2;if(Key>arr[mid]) low = mid+1;ElseHigh =mid-1; } for(j=i-1; j>high;j--) {arr[j+1]=ARR[J]; } arr[j+1]=key; }    } Public Static void SOP(intArr[]) { for(intI=0; i<arr.length;i++) {System. out. Print (arr[i]+" "); } System. out. Print ("\ n"); }    }

Results:
8 5 3 5 6 7
3 5 5 6 7 8
We look at the fact that the binary insertion sort has a time complexity of O (n^2), the binary insertion sort only reduces the number of comparison elements, about O (nlog2n), which is independent of the initial state to be sorted, depending only on the number of elements in the table n;

Data structure--sorting related issues

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.