title: Selection Sort through malloc function
body: <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  int main()
  {
      int i,n,min,j;
      int *array; 
      printf("The no of elements &gt;&gt; ");
      scanf("%d\n",&amp;n);
      printf("Making array\n" );
      array = (int *)malloc (sizeof(int) * n);
      printf("array made!\n");
  
      //INPUT NUMBERS
      for (i=0 ; i&lt;n ; i++)
          {
              printf("&gt;&gt;Enter the element %d\n" , i+1);
              scanf("%d\n", &amp;array[i]);
          }
  
      //SORT THE ARRAY
      for (i = 0; i &lt; n; ++i)
          {
              min = array[0];
              for (int j = i+1; j &lt; n; ++j)
              {
                  if (array[i] &gt; array[j])
                      min = array[j];
              }
              if (min != array[i])
              {
                  int temp;
                  temp = array[i];
                  array[i]= min;
                  array[j]=temp;
              }
          }
  
      //PRINTING ARRAY
      for (i=0 ; i&lt;n ; i++)
          {
              printf("-- %d --\n" , array[i]);
          };
  }
  </code></pre>
  
  <p>I have this particular code for SelectionSort in c using Malloc for arrays. However, it is going through intake of input 2 times after "the no of elements>>" and won't sort either.
  If I go on with the way it is, it is giving me an unsorted array in the following way</p>
  
  <pre><code>The no of elements &gt;&gt; 5
  4
  Making array
  array made!
  &gt;&gt;Enter the element 1
  5
  &gt;&gt;Enter the element 2
  2
  &gt;&gt;Enter the element 3
  1
  &gt;&gt;Enter the element 4
  7
  &gt;&gt;Enter the element 5
  8
  -- 7 --
  -- 1 --
  -- 1 --
  -- 2 --
  -- 1 --
  </code></pre>
  
  <p>I have read a few other posts and corrected my code , but this still won't work. 
  Can't figure out why.</p>
  
  <p><strong>Edit 1</strong>  The problem for double input did resolve by making a few changes to code.</p>
  
  <pre><code>int main()
  {   int i,n,min,j;
      int *array; 
      printf("The no of elements &gt;&gt; ");
      scanf("%d",&amp;n);
      array = (int *)malloc (sizeof(int) * n);
  
      for (i=0 ; i&lt;n ; i++)
          {
              printf("&gt;&gt;Enter the element %d\n" , i+1);
              scanf("%d", &amp;array[i]);
          }
  
      //SORT THE ARRAY
      for (i = 0; i &lt; n; ++i)
          {
              min = array[i];
              for (int j = i+1; j &lt; n; ++j)
              {
                  if (array[i] &gt; array[j])
                      min = array[j];
              }
              if (min != array[i])
              {
                  int temp;
                  temp = array[i];
                  array[i]= min;
                  array[j]=temp;
              }
          }
  
      //PRINTING ARRAY
      for (i=0 ; i&lt;n ; i++)
          {
              printf("-- %d --\n" , array[i]);
          };
  }
  </code></pre>
  
  <p>however , now the input is still not correct</p>
  
  <pre><code>The no of elements &gt;&gt; 5
  Making array
  array made!
  &gt;&gt;Enter the element 1
  5
  &gt;&gt;Enter the element 2
  4
  &gt;&gt;Enter the element 3
  6
  &gt;&gt;Enter the element 4
  9
  &gt;&gt;Enter the element 5
  2
  -- 9 --
  -- 2 --
  -- 2 --
  -- 2 --
  -- 2 --
  </code></pre>
  
  <p><strong>Edit 2</strong>
  After a few corrections, and still few errors away from the final code, am at</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
      int i,n,min,j;
      int *array; 
      printf("The no of elements &gt;&gt; ");
      scanf("%d",&amp;n);
      printf("Making array\n" );
      array = (int *)malloc (sizeof(int) * n);
      printf("array made!\n");
  
      //INPUT NUMBERS
      for (i=0 ; i&lt;n ; i++)
          {
              printf("&gt;&gt;Enter the element %d\n" , i+1);
              scanf("%d", &amp;array[i]);
          }
  
      //SORT THE ARRAY
      for (i = 0; i &lt; n; i++)
          {
              min = i;
              for ( j = i+1; j &lt; n; j++)
              {
                  if (array[j] &lt; array[i])
                      min = j;
              }
              if (min != i)
              {
                  int temp;
                  temp = array[i];
                  array[i]= array[min];
                  array[min]=temp;
              }
          }
  
      //PRINTING ARRAY
      for (i=0 ; i&lt;n ; i++)
          {
              printf("-- %d --\n" , array[i]);
          };
  }
  </code></pre>
  
  <p>which is leading to a somewhat sorted , still incorrect, array</p>
  
  <pre><code>The no of elements &gt;&gt; 5
  Making array
  array made!
  &gt;&gt;Enter the element 1
  4
  &gt;&gt;Enter the element 2
  5
  &gt;&gt;Enter the element 3
  2
  &gt;&gt;Enter the element 4
  1
  &gt;&gt;Enter the element 5
  7
  -- 1 --
  -- 4 --
  -- 2 --
  -- 5 --
  -- 7 --
  </code></pre>
  
  <p><strong>Edit 3 :final code</strong>
  I edited the code several times for hit and trial. I have finally found this code as working.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
      int i,n,min,j;
      int *array; 
      printf("The no of elements &gt;&gt; ");
      scanf("%d",&amp;n);
      printf("Making array\n" );
      array = (int *)malloc (sizeof(int) * n);
      printf("array made!\n");
  
      //INPUT NUMBERS
      for (i=0 ; i&lt;n ; i++)
          {
              printf("&gt;&gt;Enter the element %d\n" , i+1);
              scanf("%d", &amp;array[i]);
          }
  
      //SORT THE ARRAY
      for (i = 0; i &lt; n; ++i)
          {
              min = i;
              for ( j = i+1; j &lt; n; ++j)
              {
                  if (array[j] &lt; array[min])
                      min = j;
              }
              if (min != i)
              {
                  int temp;
                  temp = array[i];
                  array[i]= array[min];
                  array[min]=temp;
              }
          }
  
      //PRINTING ARRAY
      for (i=0 ; i&lt;n ; i++)
          {
              printf("-- %d --\n" , array[i]);
          };
  }
  </code></pre>
  
  <p><strong>However I will still be interested to know why ++i /++j make no difference when compared to i++/j++ respectively.</strong></p>