Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Quick sort in C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 484
    Comment on it

    Quick sort in C programming is used to sort the elements of a list or an array either in ascending or in descending order.

     

    Quick sort is based on the pivoting of the element and comparison and sorting is being made based on this element.

     

     

     

    Algorithm for Quick Sort

     

    ef quickSort(alist):
       quickSortHelper(alist,0,len(alist)-1)
    
    def quickSortHelper(alist,first,last):
       if first<last:
    
           splitpoint = partition(alist,first,last)
    
           quickSortHelper(alist,first,splitpoint-1)
           quickSortHelper(alist,splitpoint+1,last)
    
    
    def partition(alist,first,last):
       pivotvalue = alist[first]
    
       leftmark = first+1
       rightmark = last
    
       done = False
       while not done:
    
           while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
               leftmark = leftmark + 1
    
           while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
               rightmark = rightmark -1
    
           if rightmark < leftmark:
               done = True
           else:
               temp = alist[leftmark]
               alist[leftmark] = alist[rightmark]
               alist[rightmark] = temp
    
       temp = alist[first]
       alist[first] = alist[rightmark]
       alist[rightmark] = temp
    
    
       return rightmark
    
    alist = [54,26,93,17,77,31,44,55,20]
    quickSort(alist)
    print(alist)
    

     

     

    In this algorithm first the pivot element is selected from the list , after that the comparison is being made based on the pivot element and sorting is performed by placing values that are less than pivot element to  the left and values higher than the pivot element to the right.

     

     

     

    Program for QuickSort

     

     #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);
    
        }
    }
    

     

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: