Insertion Sort - Part 2 - HackerRank || HackerRank Insertion Sort - Part 2 Solution

Problem Name: Insertion Sort - Part 2
Judge: HackerRank
Problem link: Insertion Sort - Part 2






HackerRank Insertion Sort - Part 2 Solution in C++/Cpp

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int s;
    cin >> s;
    int ar[s];
    for (int i = 0; i < s; i++) cin >> ar[i];
    for (int i = 1; i < s; i++)
    {
        int idx = i, val = ar[i];
        for (int j = 0; j < i; j++)
            if (val < ar[j])
            {
                idx = j;
                break;
            }
        for (int j = i; j > idx; j--) ar[j] = ar[j - 1];
        ar[idx] = val;
        for (int j = 0; j < s; j++) cout << ar[j] << " ";
        cout << endl;
    }

    return 0;
}




Next Post Previous Post
No Comment
Add Comment
comment url