Thread: quicksort program crashes without error

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    4

    quicksort program crashes without error

    I'm trying to create a QuickSort algorithm but while I did have success in writing one that doesn't give me an error, the programm crashes after entering the unsorted numbers.

    I already searched but couldn't find anything, could someone help me?


    Code:
    void swap(int left, int right, int Numbers[]) {
        int temp = Numbers[left];
        Numbers[left] = Numbers[right];
        Numbers[right] = temp;
        return;
    }
     
    void quicksort(int Beginning, int End, int Numbers[]) {
        int pivot = Numbers[((Beginning+1)+End)/2];
        int left = Beginning-1;
        int right = End;
        do {
     
            while (Numbers[left] < pivot) {
                left++;
            }
            while (Numbers[right] < pivot) {
                right--;
            }
     
            if (right > left) {
                swap(left,right,Numbers);
                left++;
                right--;
            }
        } while (left <= right);
        if (Beginning < right) {
            quicksort(Beginning,right,Numbers);
        }
        if (End < left) {
            quicksort(End,left,Numbers);
        }
    }
     
    int main()
    {
        int End,Beginning,Dim,i;
        printf("Number of Elements: ");
        scanf("%d",&Dim);
        int Numbers[Dim];
        for ( i = 0 ; i < Dim ; i++ ) {
            printf("Enter the %d. element: ",i+1);
            scanf("%d",&Numbers[i]);
        }
        Beginning = 0;
        End = Dim-1;
        quicksort(Beginning,End,Numbers);
        printf("\ngsorted:");
        for ( i = 0 ; i < Dim ; i++ ) {
            printf("[%d]: %d",i+1,Numbers[i]);
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Why do you think this is correct?

    Code:
    int left = Beginning-1;
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    4
    Quote Originally Posted by stahta01 View Post
    Why do you think this is correct?

    Code:
    int left = Beginning-1;
    Tim S.
    Well, because I learned that you can declare a variable while setting a value on it and Beginning-1 should be 1 subtracted from the variable Beginning. Is it wrong?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well if you do beginning-1, when beginning is 0 to begin with, which array element do you think you'll access with an index of -1?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2018
    Posts
    4
    Well seems like I found the first mistake. Thanks for that, I tried another way before (Beginning was set to 1) but then changed my mind to this way and forgot to remove the -1. Well, now I just have to find out why it's stuck in an endless loop

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    This is where a debugger comes in handy.
    You can set breakpoints, examine variables and step the code one line at a time to really examine what your code is actually doing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2018
    Posts
    4
    Thanks I finally fixed all the errors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Won't Run Quicksort in Main?? in Unix
    By Jimi T Sun Li in forum C Programming
    Replies: 9
    Last Post: 07-25-2015, 05:11 PM
  2. Program Crashes
    By astroboy739 in forum C Programming
    Replies: 11
    Last Post: 08-20-2012, 06:21 AM
  3. Replies: 9
    Last Post: 09-29-2010, 12:18 PM
  4. Replies: 10
    Last Post: 09-24-2010, 01:09 AM
  5. 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

Tags for this Thread