-2

This is the simplest insertion sort program. Unfortunately, it doesn't provide an outcome: it does prompt the user for the size of the array and for the list of numbers, but it doesn't do the sort. I would be grateful for your help!

/** Insertion sort **/
#include <stdio.h>

int main (void)
{
    int size, array[80], i, j, element;

    printf("Enter number of elements: \n");
    scanf ("%d", &size);

    printf("Enter %d integers\n", size);

    for (i = 0; i < size; i++)
    {
        scanf("%d", &array[i]);
    }

    for (i = 0; i < size; i++)
    {
        element = array[i];
        j = i;

        while (j > 0 && array[j-1] > element)
        {
          array[j] = array[j-1];
          array[j-1] = element;
          j--; 
        }
     }

     printf ("Sorted list in ascending order:\n");

     for (i = 0; i < size; i++)
        printf ("%d", array[i]);

     return 0;
 }
Ducol
  • 175
  • 1
  • 11
  • 2
    Tested with input `5 1 2 3 4 5`, `5 5 4 3 2 1` and `5 8 4 1 5 1` and for all of them this program seemed working fine on [Wandbox](http://melpon.org/wandbox/permlink/BUS7cUAS8hhokRtw). Can you provide your input? – MikeCAT Oct 25 '15 at 09:29
  • `printf ("%d ", array[i]);` print a space after each number – Shahriar Oct 25 '15 at 09:31
  • The program works (MSVC), add a space as commented above. – Weather Vane Oct 25 '15 at 09:32
  • 2
    Why would adding a space make it show the output? I would have suggested adding a newline at the end instead. The fact that all numbers appear without separators between them ought not make a difference here. – Jongware Oct 25 '15 at 09:36
  • maybe your stdout is closed or something internal because the code works for me – Itay Sela Oct 25 '15 at 09:37
  • Usual DCV for no aparrent debugging attempt. – Martin James Oct 25 '15 at 12:55

1 Answers1

0

This is one of those confusing moments :) I have forgotten that there is a '\n' character in the second printf, after which the user is prompted for the list. Therefore, I've kept typing in numbers in one row, without hitting ENTER; of course, the program considered that as one number, only one integer not the list ))) Thank you everyone for replying. Sorry for this.

Ducol
  • 175
  • 1
  • 11