# Thread: quicksort program crashes without error

1. ## 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. Why do you think this is correct?

Code:
`int left = Beginning-1;`
Tim S. 3. Originally Posted by stahta01 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. 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? 5. 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. 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. 7. Thanks I finally fixed all the errors.  Popular pages Recent additions int, left, pivot, quicksort, sorting algorithms 