0

Can anyone please tell what's wrong with my code for quick sort?? vscode ain't showing any error, and even in online ide, they're just showing limited resources or something and I just don't know what's wrong with it its just not showing output Here this is the code enter code here

      #include<stdio.h>


      void swap( int* x, int* y)
      {
        int t = *x;
        *x = *y;
        *y = t;
      }

      int part( int a[], int l, int h )
      {
         int i = l-1, p = a[h] ;

         for( int j=l; j<= h-1; j++ )
      {
         if( a[j]<p )
        {   
            i++;
            swap( &a[i], &a[j] );
        }   
      }

        swap( &a[i+1], &p);

        return (i+1);
      }

      void qsort( int a[], int l, int h )
      {
        if( l<h )
          {
            int pi = part( a, l, h);

            qsort( a, (pi+1), h );

           qsort( a, l, (pi-1) );
          }
      }

     void print_arr( int a[], int n )
     {
        for( int i=0; i<n; i++ )
         {
            printf("%d", a[i] );
         } 
     }


   int main()
  {
     int a[100], n ;

     printf("Enter the total no. of elements in the array : ");
       scanf("%d", &n );

     printf("Enter the elements in the array : ");
       for(int i=0; i<n; i++)
     {
           scanf("%d", &a[i] );
     }

     qsort( a, 0, (n-1) );

     printf("The sorted array is : ");
      print_arr( a, n );

    return 0 ;
  }
  • Seems like a good time to learn how to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to catch crashes and locate when and where they happen in your code. Or how to use the debugger to step through the code statement by statement while monitoring variables and their values to see what's going on. – Some programmer dude Sep 03 '22 at 12:44
  • Hi! It's impossible to know from looking at this (not even consistently indented) code what goes wrong. You'll need to do a bit more of your own debugging. Print your intermediate states, maybe? Use a debugger? We need questions to contain sufficient debugging information. – Marcus Müller Sep 03 '22 at 12:44
  • Do you expect anything intelligable from `printf("%d", a[i] );` with the numbers all run together in an unbroken string? At least put a space in there, or a newline to flush the output. – Weather Vane Sep 03 '22 at 12:48
  • 1
    Please provide a [mre] which does not require input. I.e. use hardcoded initialisation for input data. That way a) it is easier to demonstrate what makes you think that there is a problem b) people trying to help you have less work c) input is clearly defined d) your risky way of using scanf is eliminated from possible problems ... – Yunnosch Sep 03 '22 at 12:56
  • `int i = l-1` followed by `a[i]` will underflow when `l` is `0` (as in the first call). – Weather Vane Sep 03 '22 at 12:59
  • Aside `j <= h-1` is bad practice. it is harder to read than `j < h` and can lead to errors when the types are unsigned. – Weather Vane Sep 03 '22 at 13:02
  • `swap( &a[i+1], &p);` I don't think this is what you want. `p` is not a part of the array. – n. m. could be an AI Sep 03 '22 at 13:13
  • Using more significant variable names would make your code more readable – mikyll98 Sep 03 '22 at 13:15

1 Answers1

0

I tried out your code. It actually ran, but gave unwanted results. Reviewing the methodology for the "quick sort" algorithm and your code, I spotted the issue in the partitioning function. In your code, your final swap is updating a work field and not the array pivot value. Following is a snippet of code with an updated version of the function.

int part( int a[], int l, int h )
{
    int i = l-1, p = a[h] ;



    for( int j=l; j< h; j++ )
    {
        if( a[j]<p )
        {
            i++;
            swap( &a[i], &a[j] );
        }
    }

    //swap( &a[i+1], &p);       
    swap( &a[i+1], &a[h]);      /* Using &p just updates a work field and not the array */

    return (i+1);
}

Give that a try.

NoDakker
  • 3,390
  • 1
  • 10
  • 11