# Array C++ Insertion Sort

by Dinesh Thakur Category: Array Pointer and Union

Insertion Sort is a sorting technique based on the idea of inserting a new element in a set of given elements so that the resulting elements are also in sorted order. Suppose a set of 4 elements A, A[l], A, A are to be sorted in ascending order. Initially sort Aand A[l] in ascending order. If A[l] is less than A, interchange their positions. Otherwise the positions of the elements remain the same. Next insert A in the appropriate position so that the resulting array A, A[l] and A remain sorted. The process is repeated in this manner.

Example

The program illustrates sorting numbers in ascending order using Insertion sort.

#include <iostream.h>

const int n=5;

void main( )

{

int a[n], i,j,k;

int temp;

cout<<"enter the values "<<"\n";

for(i=0;i<n;i++)

cin>>a[i];

for(j=1;j<n;j++)

{

for(k=0;k<j;k++)

{

if (a[j]<a[k])

{

temp=a[j];

for(int l=j;l>k;l--)

a[l]=a[l-1];

a[k]=temp;

break;

}

}

cout<<"pass "<<j<<" ";

for(i=0;i<n;i++)

cout<<a[i]<<" ";

cout<<endl;

}}

The first j elements are sorted where j =2,3 ... upto n. The place where (j+1) th element is to be inserted ,that is k, is found .The elements between places k and j are shifted by one position. After that the (j+l)th element is inserted at the kth position.

Input and Output:

enter the values

34        22        49        10        2

pass     1          22        34        49        10        2

pass     2          22        34        49        10        2

pass     3          10        22        34        49        2

pass     4          2          10        22        34        49 Dinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles
{"ft_lang_label":"__label__en","ft_lang_prob":0.7472107,"math_prob":0.86595714,"size":1769,"snap":"2020-34-2020-40","text_gpt3_token_len":518,"char_repetition_ratio":0.10934844,"word_repetition_ratio":0.013422819,"special_character_ratio":0.3193895,"punctuation_ratio":0.15566038,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98684746,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-03T15:32:26Z\",\"WARC-Record-ID\":\"<urn:uuid:0f5b61df-388d-45d3-af4f-5bc04b8d04fc>\",\"Content-Length\":\"59787\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:879b9edb-acf4-4906-8e5b-4a4b6933efa1>\",\"WARC-Concurrent-To\":\"<urn:uuid:29764c83-0cf1-456d-ba41-19b898646ec2>\",\"WARC-IP-Address\":\"216.194.173.231\",\"WARC-Target-URI\":\"https://ecomputernotes.com/cpp/array-pointer-union/insertion-sort\",\"WARC-Payload-Digest\":\"sha1:JBEP4XQBHOZJIELJR4T2ZAFFX7SPLUCV\",\"WARC-Block-Digest\":\"sha1:MA66SXMOFBCMHF43KRBNLNA73S62P5PV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439735812.88_warc_CC-MAIN-20200803140840-20200803170840-00399.warc.gz\"}"}