C + + Insert Sort Demo program

Source: Internet
Author: User

Direct insertion Sorting is one of the simplest sorting methods, and its basic operation is to insert a record into an ordered table that has been sorted,//To get a new ordered table with a 1 increase in the number of records. The front elements of the current element are all ordered, to be inserted, from the left side of the current element to look forward (from the back to the next),//The element larger than the current element is moved to the right one position, and finally put the current element where it should be on the line. Complexity: O (n^2)--the insertion sort is not suitable for a large amount of data sort application # include <iostream>using namespace Std;template<typename datatype> void output (DataType a[], int n) {for (int i = 0; i < n; i++) cout << a[i] << ""; cout << Endl;}    Template<typename datatype>void Insertsort (DataType a[], int n) {DataType key;    Int J;    int scancnt = 0, swapcnt = 0; for (int i = 1; i < n; i++) {key = a[i];//the array element value corresponding to the current I label---current element j = i-1;//The element designator of the current I, J while (J &G t;= 0 && A[j] > key)//If J is in the Legal range (0~N-1) The value of the element referred to is greater than key {a[j+1] = a[j];//put the element J refers to the j+1 of the element j-- ;    Move J Forward, if the current element is longer than the previous small, then J has been changed to 0} a[j + 1] = key;//Set the value of the j+1 element is key output (A, n); }}int Main () {int x[] = {7, 4, 6, 8, 3, 5, 1, 9,-2, 0};cout << "initial state:" << sizeof (x)/sizeof (int) << E Ndl;output (x, sizeof (x)/sizeof (int));insertsort<int> (x, sizeof (x)/sizeof (int)); return 0;} 

C + + Insert Sort Demo program

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.