C language--Direct insertion algorithm

Source: Internet
Author: User

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.

This is explained by the example of small to large sort.
The basic idea of inserting a sort is to add elements one by one to an already sorted array, while requiring that the inserted elements be in the correct position, so that the original sorted array is still in order.

In actual use, it is usually the order of the entire unordered array, so the unordered array is divided into two parts sorted by the sub-array and the element to be inserted, the first round, the first element as a sorted sub-array, insert the second element, the second round, the first two elements as a sorted array, insert a third element; I insert the i+1 element in the sub-array of the first I element when I turn the sort. The array is sorted until all elements are added.

Below, a selection of 3 2 4 1 is used to sort the insertion procedure, using J to record where the element needs to be inserted. The sort goal is to arrange the array from small to large.

1th Round
[3] [2 4 1] (the initial state, the 1th element is divided into a sorted sub-array, the remaining elements to be inserted)
[3] [2 4 1] (due to 3>2, so to insert position J=1)
[2 3] [4 1] (insert 2 to position J)

2nd round
[2 3] [4 1] (1th round sort result)
[2 3] [4 1] (due to 2<4, so first assume j=2)
[2 3] [4 1] (due to 3<4, so j=3)
[2 3 4] [1] (due to 4 just in position 3, no need to insert)

3rd Round
[2 3 4] [1] (2nd round sort result)
[2 3 4] [1] (due to 1<2, so j=1)
[1 2 3 4] (will be 1 insert position J, to be sorted element is empty, sort end) algorithm summary and implementation Select sort the unordered array of size N r[n] to sort, make the N-1 wheel selection process. The 1th element is first used as a sorted sub-array, and then the remaining N-1 elements are inserted one after another into the sorted sub-array; Therefore, in the first round of sorting, the first I element is always ordered, and the i+1 element is inserted into the correct position.
Copy Plain Text New window
  
 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. using namespace Std;
  5. void Insert_sort  (int a[],int &n); Use reference operations in C + +;
  6. int Main ()
  7. {
  8. int num[] = { 11,. , ------ , - };
  9. Number of data elements:
  10. int n=sizeof (num)/sizeof (n[0]);
  11. Insert_sort (num,n);
  12. for (int i=0; i<N; i+ +)
  13. cout<<num[i]<<endl;
  14. system ("pause");
  15. return 0 ;
  16. }
  1. //Insert sort implementation, here by small to large sort
  2. void Insert_sort (int a[],int &n) //n The number of elements in the array A, where the reference operation in C + + is used;
  3. {
  4. int t=0;
  5. for (int i=2-1; i<=n-1; i+ +)
  6. {
  7. if (A[i]<a[i-1] )//First find element A[i] position to insert
  8. {
  9. T=a[i];
  10. for (int j=i-1;a[j]>t;j--)
  11. A[J+1]=A[J];
  12. //Insert element in the correct position
  13. a[j+1]=t;
  14. }
  15. }
  16. }
Note: Insertion sorting is a stable sorting algorithm that does not change the order of the same numbers in the original sequence.

An insert sort is an element that is inserted one at a time, based on an already ordered small sequence. Of course, there are only 1 elements at the beginning of this ordered sequence, which is the first element. The comparison starts at the end of the ordered sequence, that is, the element that you want to insert and the one that is already in order, is inserted directly behind it if it is larger than it is, or until it is found where it is inserted. If you encounter an element equal to the insert, the insertion element places the element you want to insert behind the equal element. So, the order of the equal elements is not changed, the order from the original unordered sequence is the order of the sequence, so the insertion sort is stable.

C language--Direct insertion algorithm

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.