Java implementation of direct insertion sort and binary insert sort algorithm example _java

Source: Internet
Author: User

Direct Insert Sort
1 Sort ideas:

Inserts the record RI to be sorted into the already sequenced record R1,r2,......,r (N-1).

For a random sequence, it starts with the second element, which in turn inserts the element into the corresponding position in the element before it. The elements before it have been sorted out.

1th Order: Inserts the 2nd element into the preceding sequence table (at which there is only one element, of course, ordered), and then the first 2 elements of the sequence are ordered.
2nd order: Inserts the 3rd element into an ordered list with a length of 2 in front, making the first 2 elements orderly.
And so on, until the nth element is inserted into a sequence table with a previous length of (N-1).

2 Algorithm implementation:

Direct Insert sort
void straight_insert_sort (int num[], int len) {
 int i,j,key;
 for (j=1;j<len;j++) {
  key=num[j];
  i=j-1;
  while (I>=0&&num[i]>key) {
   num[i+1]=num[i];
   i--;
  }
  Num[i+1]=key
 }
}

3 Performance Analysis:

3.1 Space complexity: As on the code, the use of a auxiliary unit key, space complexity of O (1)

3.2 Time Complexity:

3.2.1 Best case: The record to be sorted is already orderly, then a trip sort, the keyword 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 Sort
1 Sort ideas:

On the basis of direct sorting, the record Ri to be sorted is inserted into the already ordered record R1,r2,......,r (N-1), and because the record R1,r2,......,r (N-1) has been sorted, a "binary lookup" can be used to find the insertion position.

2 Algorithm implementation:

Binary insert sort
void binary_insert_sort (int num[], int len) {
 int i,j,key,low,high,mid;
 for (i=1;i<len;i++) {
  key=num[i];
  low=0;
  high=i-1;
  Mid= (Low+high)/2;
  Look for the insertion point where the final insertion point is in the low while
  (Low<=high) {
   if (Key<num[mid])
    high=mid-1;
   else 
    low=mid+1;
   Mid= (Low+high)/2;
  }
  Move record for
  (j=i;j>low;j--) {
   num[j]=num[j-1];
  }
  Insert record
  Num[low]=key;
 }

3 Performance Analysis:

3.1 Space complexity: As on the code, the use of a auxiliary unit key, space complexity of O (1)

3.2 Time complexity: Although binary lookup reduces the number of records compared, but does not reduce the number of moves, so the time complexity with the direct lookup algorithm.

3.2.1 Best case: Time complexity O (n)

3.2.2 Worst-case scenario: Time complexity O (n^2)

3.2.3 Average Time complexity: O (n^2)

3.3 Stability: Stable

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.