dimanche 3 mars 2019

remove output space in C++

I am new in cpp language. I trying solve a problem on Insertion sort. However, presentation error happened while submit to online judge. in program I have to show intermediate sequence in a line for each step of Insertion sort. Here is my code so far:

void InsertionSort(int *arr, int n)
{

for (int i = 1; i < n; i++)
{
    int key = arr[i];
    int j = i - 1;
    while (j >= 0 && arr[j] > key)
    {

        arr[j + 1] = arr[j];
        j--;
    }

    arr[j + 1] = key;
    for (int k = 0; k < n; k++)
        cout << arr[k] << " ";
    cout << endl;
}}

Output:

1 2 3<Here is space.want to remove it> 
1 2 3<Here is space.want to remove it>

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire