Question Name:Insertion sort 1

#include<bits/stdc++.h>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
void insertionSort(int ar_size, int *ar) {
 
    int small = 0;
    for(int i = 0; i < ar_size-1;i++){
     
        if(ar[i] > ar[i+1]){
            small = ar[i+1];
            int j = i;
            while(ar[j] > small){
                ar[j+1] = ar[j];
                j--;
            }
            ar[j+1] = small;
        }
        for(int k = 0; k < ar_size; k++)cout << ar[k] << " ";
        cout << endl;
    }
}
int main(void) {
 
    int _ar_size;
    cin >> _ar_size;
    //scanf("%d", &_ar_size);
    int _ar[_ar_size], _ar_i;
    for(_ar_i = 0; _ar_i < _ar_size; _ar_i++) {
        cin >> _ar[_ar_i];
    }
   insertionSort(_ar_size, _ar);
   return 0; //sujan
}

Problem Description

In this challenge, don’t print every time you move an element. Instead, print the array after each iteration of the insertion-sort, i.e., whenever the next element is placed at its correct position.

Since the array composed of just the first element is already “sorted”, begin printing from the second element and on.

Input Format:

There will be two lines of input:
the size of the array
a list of numbers that makes up the array

Output Format : On each line, output the entire array at every iteration.

  • Test Case 1

    Input (stdin)

    6
    1 4 3 5 6 2
    

    Expected Output

    1 4 3 5 6 2 
    1 3 4 5 6 2 
    1 3 4 5 6 2 
    1 3 4 5 6 2 
    1 2 3 4 5 6 
  • Test Case 2

    Input (stdin)

    5
    8 7 1 0 9
    

    Expected Output

    7 8 1 0 9 
    1 7 8 0 9 
    0 1 7 8 9 
    0 1 7 8 9 

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.