Tuesday, 17 February 2015

Insertion Sort

INSERTION SORT PROGRAM IN C++


//
//  main.cpp
//  insert
//
//  Created by Gurinderbeer Singh on 2015-02-17.
//  Copyright (c) 2015 Gurinderbeer Singh. All rights reserved.
//

#include <iostream>


int main()
{
    int testCases ;
    scanf("%d",&testCases);
    while(testCases--)
    {
        int n,key=0  ;
        scanf("%d",&n);
        int arr[n];
        // enter array
        for(int i=0;i<n;i++)
            scanf("%d",&arr[i]);
        // insertion so
        int j;
        for(int i=1;i<n;i++)
        {
            key=arr[i];
            j=i-1;
            while(j>=0&&key<arr[j])
            {
                arr[j+1]=arr[j];
                
                j=j-1;
            }
            arr[j+1]=key;
            
            
        }
        //print values
        
        for(int i =0;i<n;i++)
        {
            printf("%d ",arr[i]);
        }
        
    }
    return 0;
    

}

No comments:

Post a Comment