isertion sort programm

Insertion sort
Hello everyone! I am begginer in this, and really need some help. It is simple programm, but i have a problem with line 39 how can i pass that number back to main? Thank you very much!!!!!
(I need numcount to count how many timesdata needed to be moved in order to sort 10 integers)

int main()
{
   int array[10];
   int last=9;
   int i;
   int numcount;
   int num;
   for (i=0;i < 10;i++)
       array[i] = rand() % 10;
    
   insertionSort(array, last , numcount);
   int t;

   printf(" Integers have been sorted:");
   for ( t=0; t < 10; t++)
       printf(" %d ,", array[t]); 
    printf(" \nNumber of moves needed to sort array is: %d", numcount);
  system("PAUSE");	
  return 0;
}

int insertionSort ( int list[], int last, int numcount ){
     int hold;
     int counting=0;
     int walker;
     int current;
     for ( current = 1; current <= last; current++){
         hold = list[current];
         counting++;
         for (walker = current - 1; walker >= 0 && hold < list[walker]; walker--){
                    list[walker+1] = list[walker];
                    counting++;
                    }
         list [walker + 1] = hold;
         }
          numcount= counting;
                 
         return numcount;
         }

Comments

  • numcount = insertionSort(blah,blah,blah);  //on line 12
    
    is the normal format in C, to "catch" the return value from a function. Don't worry about numcount having the same name as the variable being returned from insertionSort(). To the compiler, they are completely different variables.
Sign In or Register to comment.