Thread: problem implementing quicksort

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    180

    problem implementing quicksort

    Can anyone tell me what's wrong with my quicksort? It seems fine but when i run it seg faults. I've been looking for some time but can't figure out the problem.

    Code:
    void quicksort(int *a, int start, int end)
    {
        if (start < end)
        {
            int p = partition(a, start, end);
            quicksort(a, start, p - 1);
            quicksort(a, p + 1, end);
        }
    }
    Code:
    int partition(int *a, int start, int end)
    {
        int x = a[end];
        int pInd = start - 1;
        int i;
        for (i = 0; i < end; i++)
        {
            if (a[i] <= x)
            {
                ++pInd;
                swap(&a[i], &a[pInd]);
            }
        }
        swap(&a[pInd + 1], &a[end]);
        return (pInd + 1);
    }
    Code:
    void swap(int *a, int *b)
    {
        int temp = *a;
        *a = *b;
        *b = temp;
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    What system are you on? What compiler and debugger are you using?

    What line is it segfaulting on? What are the values of the pertinent variables at that point?

    BTW, you should post your code in one block, complete with main and headers so that we can copy/paste and run it. To run your code as given I'd have to add stuff to it, which I don't feel like doing.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    Sorry about that, here's the code:

    Code:
    #include <stdio.h>
    
    void quicksort(int *a, int start, int end);
    int partition(int *a, int start, int end);
    void print_array(int *a, int len);
    void swap(int *a, int *b);
    
    int main()
    {
        int a[9] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
        print_array(a, 9);
        printf("\n");
        quicksort(a, 0, 8);
        print_array(a, 9);
        return 0;
    }
    
    void quicksort(int *a, int start, int end)
    {
        if (start < end)
        {
            int p = partition(a, start, end);
            quicksort(a, start, p - 1);
            quicksort(a, p + 1, end);
        }
    }
    
    int partition(int *a, int start, int end)
    {
        int x = a[end];
        int pInd = start - 1;
        int i;
        for (i = 0; i < end; i++)
        {
            if (a[i] <= x)
            {
                ++pInd;
                swap(&a[i], &a[pInd]);
            }
        }
        swap(&a[pInd + 1], &a[end]);
        return (pInd + 1);
    }
    
    void print_array(int *a, int len)
    {
        int i;
        for (i = 0; i < len; i++)
        {
            printf("%d ", a[i]);
        }
    }
    
    void swap(int *a, int *b)
    {
        int temp = *a;
        *a = *b;
        *b = temp;
    
    }

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Your basic error is in partition where you begin your loop at 0 instead of start.

    Also in partition, you could avoid all the -/+ 1's by simply getting rid of all of them and swapping the two lines inside the if.

    You could also use a newline at the end since (on linux at least) your program leaves my prompt on the same line as the last line of output. Maybe put the newline in the print routine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. QuickSort Problem
    By ChickenChowMinh in forum C Programming
    Replies: 15
    Last Post: 02-15-2013, 08:37 AM
  2. Problems implementing quicksort function
    By rvanbeusichem in forum C Programming
    Replies: 3
    Last Post: 11-24-2004, 09:00 PM
  3. quicksort problem
    By spinner in forum C Programming
    Replies: 9
    Last Post: 05-04-2003, 02:02 PM
  4. Replies: 2
    Last Post: 10-18-2002, 08:30 AM
  5. Quicksort problem
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-17-2002, 08:23 AM

Tags for this Thread