C language Insertion Sort algorithm and instance code _c language

Source: Internet
Author: User
Tags arrays

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 by one to the array already sorted, requiring that the inserted elements must be in the correct position so that the original sorted array is still in order.

In practice, it is usually to sort the entire unordered array, so divide the unordered array into two-part sorted sub arrays and elements to be inserted. In the first round, the first element is used as a sorted array, and the second element is inserted, and the second round, which takes the first two elements as a sorted array, inserts the third element. And so on, when I sort, I insert the first I+1 element in the child array of the first I element. Until all elements are added to the sorted array.

Below, a selection sort description for 3 2 4 1 Inserts the procedure, using J to record where the element needs to be inserted. The sorting goal is to make arrays from small to large.

1th Round

[3] [2 4 1] (in the initial state, divide the 1th element into a sorted array and the rest as the element to be inserted)
[3] [2 4 1] (due to 3>2, so the position to be inserted 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] (because 4 is just in position 3 without inserting)

3rd Round

[2 3 4] [1] (2nd round sort results)
[2 3 4] [1] (due to 1<2, so j=1)
[1 2 3 4] (insert 1 position j, wait for sort element to be empty, sort over)

Algorithm Summary and implementation

Select sort to sort unordered array r[n with size N for N-1 wheel selection process. First, the 1th element is used as a sorted array, and then the remaining N-1 elements are inserted into the sorted array. Therefore, the first I element is always ordered when the first wheel is sorted, inserting the i+1 element into the correct position.

#include <stdio.h>
#include <stdlib.h>
#define N 8
void insert_sort (int a[],int n);
Insert sort implementation, where the number of elements from small to large sort
void insert_sort (int a[],int n)//n array a
{
 //N-1 wheel Insert process for
 (int i=1; i<n; i + +)
 {
  ////First find element a[i] position
  int j=0 to insert;
  while ((a[j]<a[i) && (j<i))
  {
   j + +;
  }
  Inserts an element into the correct position
  if (i!= j)//If i==j, A[i] just in the correct position
  {
   int temp = a[i];
   for (int k = i; k > J; k--)
   {
    a[k] = a[k-1];
   }
   A[J] = temp;
  }
}} int main ()
{
 int num[n] = {m, M, M, M, N, MB;
 Insert_sort (num, N);
 for (int i=0; i<n; i++)
  printf ("%d", Num[i]);
 printf ("\ n");
 System ("pause");
 return 0;
}

Note: The insert sort is a stable sort algorithm that does not change the order of the same numbers in the original sequence.

The insertion sort is based on an already ordered small sequence that inserts one element at a time. Of course, at the beginning of this ordered sequence of only 1 elements, is the first element. The comparison begins at the end of the ordered sequence, where the element that you want to insert and the one that is already ordered is the one that is inserted directly behind it, if it is larger than it is, or until you find where it should be inserted. If you encounter an element that is equal to the insertion element, the insertion element places the elements that you want to insert behind the equality element. Therefore, the order of the equal elements is unchanged, the order from the original unordered sequence is the order after the sequence, so the insertion sort is stable.

The above is a detailed introduction to the insertion sort, hoping to help the C language Development students understand the grasp of the insertion sort.

Related Article

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.