QUICK SORT USING C PROGRAM





Source code of simple quick sort implementation using array ascending order in c programming language

#include<stdio.h>

void quicksort(int [10],int,int);

int main(){
  int x[20],size,i;

  printf("Enter size of the array: ");
  scanf("%d",&size);

  printf("Enter %d elements: ",size);
  for(i=0;i<size;i++)
    scanf("%d",&x[i]);

  quicksort(x,0,size-1);

  printf("Sorted elements: ");
  for(i=0;i<size;i++)
    printf(" %d",x[i]);

  return 0;
}

void quicksort(int x[10],int first,int last){
    int pivot,j,temp,i;

     if(first<last){
         pivot=first;
         i=first;
         j=last;

         while(i<j){
             while(x[i]<=x[pivot]&&i<last)
                 i++;
             while(x[j]>x[pivot])
                 j--;
             if(i<j){
                 temp=x[i];
                  x[i]=x[j];
                  x[j]=temp;
             }
         }

         temp=x[pivot];
         x[pivot]=x[j];
         x[j]=temp;
         quicksort(x,first,j-1);
         quicksort(x,j+1,last);

    }
}

Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8






1. Write a c program for bubble sort.
5. Write a c program for heap sort.
7. Write a c program for shell sort.

30 comments:

  1. Thanks, its working! But while executing the program, the 'Run' window is disappearing before showing the result. Although, I have fixed it. If anyone is having the same problem then see below:

    In the main function, put "getch();" before the "return 0" statement.

    To run the program on a fresh screen:
    include this header file at the top--> #include
    and inside main function type "clrscr();" just after the variable declaration is done.

    Thanks...

    ReplyDelete
    Replies
    1. u can press ctrl f5 or alt f5 to see the output in turboc without need of getch

      Delete
  2. Sorry, I havn't completed the header file name above..
    To run on a new window include this header file>>
    #include
    and inside main function type "clrscr();" just after the variable declaration is done.

    Hope it works!!
    Thanks...

    ReplyDelete
  3. debaan every1 can understand that much if he is trying this program

    ReplyDelete
  4. can u please explain how it works?

    ReplyDelete
  5. working well but difficult to understand. Can anyone send discription at lovababu.golthi@gmail.com

    Thanks in advance.

    ReplyDelete
  6. if you want to show the result without getch()
    go to window--->output

    ReplyDelete
  7. @all: http://www.cise.ufl.edu/~ddd/cis3020/summer-97/lectures/lec17/sld003.htm
    here is the description.

    ReplyDelete
  8. Add prototype "int quicksort(int,int,int);" after the header files also add "#include" too to use gtech();

    ReplyDelete
  9. /*Add prototype "int quicksort(int,int,int);" after the header files also add "#include" too to use gtech();*/

    ReplyDelete
  10. Add prototype "int quicksort(int,int,int);" after the header files also add "conio.h" header file too to use getch();

    ReplyDelete
  11. //easy to understand

    #include
    #include
    void quicksort(int [],int,int);
    int main()
    {
    int x[20],size,i;

    printf("Enter size of the array [<20]: ");
    cin>>size;
    printf("Enter %d elements: ",size);
    for(i=0;i>x[i];
    }
    quicksort(x,0,size-1);

    cout<<"Sorted elements: ";
    for(i=0;i<size;i++)
    cout<<x[i]<<" ";
    getch();
    return 0;
    }
    void quicksort(int x[],int first,int last)
    {

    int pivot,j,temp,i;
    pivot=last;
    for(i=first;i<pivot;i++)
    {
    if(x[pivot]<x[i])
    {
    temp=x[i];
    x[i]=x[pivot-1];
    x[pivot-1]=x[pivot];
    x[pivot]=temp;
    }
    quicksort(x,first,pivot-1);
    quicksort(x,pivot+1,last);
    }
    }

    ReplyDelete
  12. if any one is looking to execute quick_sort using linked list , here's the code:

    #include
    #include
    #include
    struct linked_list
    {
    int data;
    struct linked_list *link;
    struct linked_list *prev;
    };
    typedef struct linked_list node;
    void create(node *first);
    void print(node *first);
    node *quick_sort(node *first);
    int count(node *first);
    main()
    {
    node *first;
    int x,n,i,j=0,m;
    clrscr();
    first=(node *)malloc(sizeof(node));
    first->prev=0;

    create(first);
    printf("the numbers you have entered are :\n");
    print(first);
    n=count(first);
    printf("\n the number of elements you have entered are: %d\n",n);
    for(i=0;idata);
    printf( " do you want to add more?(y=1/n=2)\n");
    scanf("%d",&x);
    if(x==2)
    {
    first->link=0;
    }
    else
    {
    first->link= (node *)malloc(sizeof(node));
    first->link->prev=first;
    create(first->link);
    }
    return;
    }


    void print(node *list)
    {
    if(list->link!=0)
    {
    printf("%d-> ",list->data);

    if(list->link->link==0)
    printf("%d",list->link->data);
    print(list->link);
    }
    }



    node *quick_sort(node *first)
    {
    node *save,*temp,*pivot,*p;
    pivot=first;
    temp=pivot->link;
    while(temp!=0)
    {
    if(pivot->data > temp->data)
    {
    if(temp->link==0)
    {
    temp->prev->link=0;
    temp->link=first;
    first->prev=temp;
    temp->prev=0;
    first=temp;
    break;
    }
    else
    {
    temp=temp->link;
    temp->prev->link=first;
    temp->prev->prev->link=temp;
    first->prev=temp->prev;
    temp->prev=temp->prev->prev;
    first->prev->prev=0;
    first=first->prev;
    }
    }

    else
    temp=temp->link;
    }

    pivot=pivot->link;
    while(pivot->link!=0)
    {
    temp=pivot->link;
    while(temp!=0)
    {

    if(pivot->data > temp->data)
    {
    if(temp->link==0)
    {
    temp->prev->link=0;
    temp->link=pivot;
    pivot->prev->link=temp;
    temp->prev=pivot->prev;

    break;
    }
    else
    {
    p=pivot->prev;
    temp=temp->link;
    temp->prev->link=pivot;
    temp->prev->prev->link=temp;
    pivot->prev=temp->prev;
    temp->prev=temp->prev->prev;
    pivot->prev->prev=p;
    pivot->prev->prev->link=pivot->prev;

    }
    }

    else
    temp=temp->link;
    }
    pivot=pivot->link;
    }
    printf("\n first=%d\n",first->data);
    return(first);
    }


    int count(node *first)
    {
    node *save=first;
    int n=0;
    while(save!=0)
    {
    n++;
    save=save->link;
    }
    return(n);
    }

    ReplyDelete
  13. i want to write a program to sort weather condition for past 3 years for 5 cities using multi dimenstional arrays......

    ReplyDelete
  14. any one can know the program ...you please explain that....

    ReplyDelete
  15. can u explain this program

    ReplyDelete
  16. easy to understand

    ReplyDelete
  17. it cant work....

    ReplyDelete
  18. #include
    #include
    void quick_sort(int*,int,int);
    int partision(int*,int,int);
    void main()
    {
    int i,n,j,a[50],lower=0,upper;
    clrscr();
    printf("Enter the size of an array=");
    scanf("%d",&n);
    upper=n-1;
    printf("Enter the array elements=");

    for(i=0;ilower)
    {
    i= partision(a,lower,upper);
    quick_sort(a,lower,i-1);
    quick_sort(a,i+1,upper);
    }
    }
    int partision(int a[],int lower,int upper)
    {
    int i,p,q,temp;
    p=lower+1;
    q=upper;
    i=a[lower];
    while(q>=p)

    {

    while(a[p]i)

    {

    q--;
    }
    if(q>p)

    {
    temp=a[p];
    a[p]=a[q];
    a[q]=temp;
    }
    }
    temp=a[lower];
    a[lower]=a[q];
    a[q]=temp;
    return q;
    }

    ReplyDelete
  19. void main()
    {
    char c=125;
    pritnf("%d",c);
    }

    ReplyDelete
  20. What is the time Complexity of this program???

    ReplyDelete
  21. please tell me program for quick sort in descending order.

    ReplyDelete
  22. what's the ending condition763

    ReplyDelete
  23. unable to open include file 'stdio.h' and 'conio.h'. what to do if this error comes?

    ReplyDelete
  24. this is not working.... wtf...!!!!

    ReplyDelete
  25. can you plss help me?.our final project is to make a program that implementing bubble, sort,insertion,quick and selection in one program.

    ReplyDelete
  26. buddy..msg me at anand.sinha85@gmail.com for any help on sorting(The guy who posted it as final year project).Although the project is one of the easiest you can get stil if you need any help kindly let me know.

    ReplyDelete
  27. is it working???

    ReplyDelete
  28. hey u forgot to include the exit condition for recursion so its going in an infinite loop

    ReplyDelete

Share It