1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Quick Sort Using C

Discussion in 'C' started by angad_aks, Feb 15, 2011.

  1. angad_aks

    angad_aks Banned

    Joined:
    Feb 15, 2011
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include<stdio.h>
    #include<conio.h>
    void quick(int *,int,int);
    void main()
    {
     int n,i,a[50],left,right;
     clrscr();
     printf("enter the size of the array");
     scanf("%d",&n);
     printf("enter the sequence");
     for(i=0;i<=n-1;i++)
     {
      scanf("%d",&a[i]);
     }
     printf("the sorted array is:");
     left=0;
     right=n-1;
     quick(&a[0],left,right);
     for(i=0;i<=n-1;i++)
     {
      printf("%d\t",a[i]);
     }
     getch();
    }
    void quick(int *a,int left,int right)
    {
     int pivot,l_hold,r_hold,t;
     pivot=a[left];
     l_hold=left;
     r_hold=right;
     while(left<right)
     {
      while(a[right]>=pivot && left<right)
      {
       right=right-1;
      }
      while(a[left]<=pivot && left<right)
      {
       left=left+1;
      }
      if(left!=right)
       {
    	t=a[left];
    	a[left]=a[right];
    	a[right]=t;
       }
     }
     a[l_hold]=a[left];
     a[left]=pivot;
     if(l_hold<left)
       quick(a,l_hold,left-1);
     if(r_hold>left)
      quick(a,left+1,r_hold);
     return;
    }
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    clrscr() and getch() again. Single letter variable names. No comments. My score for this post: 1/10 for effort. To get the other 9 you'll need to post considerably better code than this.

    Oh and void main() as well. Mark adjusted to 0/10.
     

Share This Page