QuickSort

Introduction

Quicksort is basically work on divide and conquer algorithm. Firstly quicksort divide the complete large array into small sub array, the low elements and the high elements. Quicksort also known as partition-exchange sort, use these steps:

  1. Choose any element of the array to be the pivot.
  2. Divide all others elements into two partition.

    • All Elements less than the pivot must be first partition.
    • All elements greater than the pivot must be the second partition.

  3. Use the recursion to sort both partition.
  4. Join the first sorted partition, the pivot, and the second sorted partition.

Algorithm

Quicksort(a,p,r)

  1. If p<r
  2. q=partition(a,p,r)
  3. Quicksort(a,p,q-1)
  4. Quicksort(a,q+1,r)
  5. Exit

Partition(a,p,r)

  1. X=a[r]
  2. I=p-1
  3. J=p to r-1
  4. Do if a[j]<=x
    I=i+1
    Exchange a[i]=a[j]
  5. A[i+1]=a[j]
  6. Return i+1

Quicksort

Program

Now place the following code in you console app page:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace array  
  8. {  
  9.         class Program  
  10.         {  
  11.            static public int partition(int[] item, int left, int right)  
  12.             {  
  13.                 int pivot = item[left];  
  14.                 while (true)  
  15.                 {  
  16.                     while (item[left] < pivot)  
  17.                         left++;  
  18.   
  19.                     while (item[right] > pivot)  
  20.                         right--;  
  21.   
  22.   
  23.                     if (left < right)  
  24.                     {  
  25.                         int tem;  
  26.                         tem = item[left];  
  27.                         item[left] = item[right];  
  28.                         item[right] = tem;  
  29.                     }  
  30.                     else  
  31.                     {  
  32.                         return right;  
  33.                     }  
  34.                 }  
  35.             }  
  36.            static public void quicksort(int [] arr,int left,int right)  
  37.             {  
  38.             if (left<right)  
  39.             {  
  40.                 int pivot = partition(arr,left, right);  
  41.                 if (pivot>1)  
  42.                 {  
  43.                     quicksort(arr,left,pivot-1);  
  44.                 }  
  45.                 if (pivot+1<right)  
  46.                 {  
  47.                     quicksort(arr,pivot+1,right);  
  48.                 }  
  49.             }  
  50.             }  
  51.             static void Main(string[] args)  
  52.             {  
  53.                 int [] item=new int[10];  
  54.                Console.WriteLine("Enter 10 Element");  
  55.                 for (int i = 0; i < 10; i++)  
  56.             {  
  57.             item[i]=Int32.Parse(Console.ReadLine());  
  58.             }  
  59.                 quicksort(item,0,10-1);  
  60.                 Console.WriteLine("\nSorted Array :");  
  61.                 for (int j  = 0; j  < 10; j ++)  
  62.             {  
  63.              Console.WriteLine(+item[j]);  
  64.             }  
  65.                 Console.ReadLine();  
  66.             }  
  67.         }  
  68.     }  
Output

output of Quilsort