Quantcast
Channel: Active questions tagged crash - Stack Overflow
Viewing all articles
Browse latest Browse all 7199

I'm making quicksort in C and can't see what's wrong

$
0
0

I'm trying to make quicksort in C for practice, it is crashing and I can't see why.

The code i have so far (it's just the part with throwing numbers higher than pivot on one side and smaller than pivot on the other side + it's only sorting "array"):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ARRSZ 10 //ARRay SiZe

int array[ARRSZ];

void quicksort(int start, int end){
    int pivot = start + (end - start)/2;

    for(int i = start; i < pivot; i++){
        if(array[i] > pivot){
            int x = array[i];
            for(int j = i; i < pivot; j++){
                array[j] = array[j+1];
            }
            array[pivot] = x;
            pivot--;
        }
    }

    printf("\nqs first half\n\n"); //I put these here afterwards so I could see how far the program got.

    for(int i = end; i > pivot; i--){
        if(array[i] < pivot){
            int x = array[i];
            for(int j = i; i > pivot; j--){
                array[j] = array[j-1];
            }
            array[pivot] = x;
            pivot++;
        }
    }
}

int main()
{
    srand(time(NULL));

    for(int i = 0; i < ARRSZ; i++){
        array[i] = rand() % (ARRSZ*2) + 1 - ARRSZ; //Wanted to have numbers form -ARRSZ to +ARRSZ
        printf("%d\n", array[i]);
    }

    printf("\nbreak\n\n");

    quicksort(0, ARRSZ-1);

    for(int i = 0; i < ARRSZ; i++){
        printf("%d\n", array[i]);
    }

    return 0;
}

It allways reaches the "printf("\nbreak\n\n");" (line 46) and sometimes the "printf("\nqs first half\n\n");" (line 23).

Then windows says that it stopped working and the output says:

Process returned: -1073741795 (0xC000001D)

I'm using CodeBlocks 17.12, Windows 7 Pro if it helps.

Thanks for any answers.


Viewing all articles
Browse latest Browse all 7199

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>