help with quicksort

This is a discussion on help with quicksort within the C Programming forums, part of the General Programming Boards category; i have come back into praticing my c skills, and i came across an error with using my quicksort algorithm. ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123

    help with quicksort

    i have come back into praticing my c skills, and i came across an error with
    using my quicksort algorithm.

    Code:
    #include<stdio.h>
    
    void Swaparray( int  array1, int array2);
    int Partition(int *array, int begin, int end, int pivotIndex);
    void Quicksortnum(int *array, int begin, int end);
    
    
    
    
    int main(void)
    {
      int numbers[10];
      int i;
      for(  i = 0; i < 10; ++ i)
      {
          printf("Enter a number: ");
          scanf("%d", &numbers[i] );
      }
    
      Quicksortnum(&numbers, 0, 10);
    
      for( i = 0; i < 10; ++i)
      {
        printf(" array[%d] = %d",i,numbers[i]);
    
      }
    
      getchar();
    
      return 0;
    }
    
    
    
    
    
    
    
    void Swaparray( int array1, int  array2)
    {
      int temp = array1;
     array1 = array2;
     array2 =  temp;
    }
    
    int Partition(int *array, int begin, int end, int pivotIndex)
    {
      int pivotValue = array[pivotIndex];
      Swaparray(array[pivotIndex], array[end]);
      int storeIndex = begin;
      for( int i = begin; i < end; ++i)
      {   if(array[i]  <=  pivotValue)
             {  Swaparray(array[i], array[storeIndex]);
                 storeIndex++;
              }
        }
    
         Swaparray(array[storeIndex], array[end]);
         return storeIndex;
    }
    
    void Quicksortnum(int * array, int begin, int end)
    {
      if(end > begin)
     {
       int pivotIndex = begin;
       int pivotNewIndex =  Partition(array,begin,end,pivotIndex);
       Quicksortnum(array,begin, pivotNewIndex -1);
       Quicksortnum(array,pivotNewIndex + 1, end);
       }
    
    }
    my log is
    Compiling: C:\Documents and Settings\justin\My Documents\Quicksort.c
    C:\Documents and Settings\justin\My Documents\Quicksort.c: In function `int main()':
    C:\Documents and Settings\justin\My Documents\Quicksort.c:20: error: cannot convert `int (*)[10]' to `int*' for argument `1' to `void Quicksortnum(int*, int, int)'
    C:\Documents and Settings\justin\My Documents\Quicksort.c:72:2: warning: no newline at end of file
    Process terminated with status 1 (0 minutes, 0 seconds)

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Code:
      Quicksortnum(&numbers, 0, 10);
    Drop the '&'. Arrays are always passed as if they were a pointer to the first element.

    Also, add a newline at the end of the file.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    hey thanks, for point out my error heh

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    New York
    Posts
    123
    i have a new problem now seen i changed the program in which run time i get an error code
    Code:
    #include<stdio.h>
    
    void Swaparray( int  *array1, int *array2);
    int Partition(int *array, int begin, int end, int pivotIndex);
    void Quicksortnum(int *array, int begin, int end);
    
    
    
    
    int main(void)
    {
      int numbers[10];
      int i;
      for(  i = 0; i < 10; ++ i)
      {
          printf("Enter a number: ");
          scanf("%d", &numbers[i] );
      }
    
      Quicksortnum(numbers, 0, 10);
    
      for( i = 0; i < 10; ++i)
      {
        printf(" array[%d] = %d",i,numbers[i]);
    
      }
    
      getchar();
    
    return 0;
    
    }
    
    
    
    
    
    
    
    void Swaparray( int *array1, int  *array2)
    {
      int temp = *array1;
     *array1 = *array2;
     *array2 =  temp;
    }
    
    int Partition(int *array, int begin, int end, int pivotIndex)
    {
      int pivotValue = array[pivotIndex];
      Swaparray(array[pivotIndex], array[end]);
      int storeIndex = begin;
      int i;
      for(  i = begin; i < end; ++i)
      {   if(array[i]  <=  pivotValue)
             {  Swaparray(array[i], array[storeIndex]);
                 storeIndex++;
              }
        }
    
         Swaparray(array[storeIndex], array[end]);
         return storeIndex;
    }
    
    void Quicksortnum(int * array, int begin, int end)
    {
      if(end > begin)
     {
       int pivotIndex = begin;
       int pivotNewIndex =  Partition(array,begin,end,pivotIndex);
       Quicksortnum(array,begin, pivotNewIndex -1);
       Quicksortnum(array,pivotNewIndex + 1, end);
       }
    
    }

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,705
    I think it would help if you swapped correctly. Call your swap with pointer arguments. This means that you have to take the address of array elements. Such as in this line:

    Swaparray(&array[i], &array[storeIndex]);


    Other than that, I'm not sure. You're going to have to be a little more specific from now on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scalability problems with Quicksort
    By hbejkosa in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2008, 10:26 PM
  2. Question 39: Quicksort vs Linear Search
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-17-2005, 11:03 AM
  3. Using quicksort and radix sort for an anagram program
    By RazielX in forum C Programming
    Replies: 2
    Last Post: 05-03-2004, 09:33 AM
  4. quicksort problem
    By spinner in forum C Programming
    Replies: 9
    Last Post: 05-04-2003, 02:02 PM
  5. Quicksort
    By Nutshell in forum C Programming
    Replies: 1
    Last Post: 01-15-2002, 08:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21