Understanding the sorting algorithm in a comprehensible sort-insertion sort

Source: Internet
Author: User

#include <iostream>


/* Insert Sort


basic idea: inserting records into sorted ordered tables

Features: A stable sequencing method, time complexity O (n^2)

*/

void insertsort (int array[],int len) {

int i,j;

int temp;

for (i =1; i < Len; i++) { //I:1 ~ &L T;len (i++)

temp = Array[i];

for (j = i-1; J >=0; j--) { J:i-1 ~ >=0 (j--)

if (temp < array[j]) { // Assuming the element to be inserted < previous sequence, the post-move element Vegetarian

array[j+1] = array[j];

}else{

break;

}

}

array[j+1] = temp; // vacancy fill in the element you want to insert.

}

}


int Main (int argc,constchar * argv[])

{

int i =0;

int a[] = {5,4,9,8 ,7,6,0,1,3 ,2};

int len =sizeof(a)/sizeof(a[0 ]);

// Insert Sort

insertsort(a,len);

for (i =0; i < Len; i++) {

printf("%d", A[i]);

}

printf("\ n");

return0;

}


Understanding the sorting algorithm in a comprehensible sort-insertion sort

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.