why is my code giving me runtime error, this code is for quicksort

Viewed 15

this is a code i wrote for quicksort, as conventionally done i did not add a separate partition function and integrated that part of the code in the qs function itself. when i run the code it gives me runtime error and i even did dry run, but it seems all correct. i cant understand why is it giving runtime error

    void swap(int input[],int a, int b){
        int temp=input[a];
        input[a]=input[b];
        input[b]=temp;
        return;
    }
//swap fn to use 
    
    void qs(int input[],int s,int e){
        int size=e-s+1;
        if (size==0 || size==1){ 
            return;
        }
        int pivot=input[s];
        int count=0;
        for (int i=0;i<size;i++){
            if (input[i]<=pivot){
                count++;
            }
        }
        int pi=s+count; 
        swap(input,pi,s);
        int i=s,j=pi+1;
//moving all the elements smaller to the left and larger to the right of the pivot
        while (i<pi && j<=e){
              if (input[i]<=pivot){
                i++;
              }
              else if (input[j]>pivot){
                j++;
              }
              else if (i<pi && j<=e){
              swap(input,i,j);
                i++;
                j++;
              }
        }
        qs(input,s,pi-1);
        qs(input,pi+1,e);
    }
    
    
    void quickSort(int input[], int size) {
        qs(input,0,size-1);
    }
0 Answers
Related