xxxxxxxxxx
Step 1 − Choose the highest index value has pivot
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot
xxxxxxxxxx
Implementation of Quick Sort Algorithm in C:
# include <stdio.h>
// to swap two numbers
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // selecting last element as pivot
int i = (low - 1); // index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If the current element is smaller than or equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/*
a[] is the array, p is starting index, that is 0,
and r is the last index of array.
*/
void quicksort(int a[], int p, int r)
{
if(p < r)
{
int q;
q = partition(a, p, r);
quicksort(a, p, q-1);
quicksort(a, q+1, r);
}
}
// function to print the array
void printArray(int a[], int size)
{
int i;
for (i=0; i < size; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
int main()
{
int arr[] = {9, 7, 5, 11, 12, 2, 14, 3, 10, 6};
int n = sizeof(arr)/sizeof(arr[0]);
// call quickSort function
quicksort(arr, 0, n-1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
xxxxxxxxxx
// in c++
#include<iostream>
#include<algorithm>
using namespace std;
int partition(int arr[], int low, int high){
// i is used for determining the correct position of pivot element
/*
We can choose first element of the array as pivot element,
or we can choose middle element/ last element / any element randomly as pivot element.
In this program, we are using last element as pivot element.
*/
int i = low -1;
int pivot = arr[high];
for(int j=low; j<= high-1; j++){
if(arr[j] < pivot){
i = i+1;
// to do the swapping directly include "algorithms" library in the header, in c++
swap(arr[i], arr[j]);
}
}
// put the pivot elements at its correct position
swap(arr[i+1], arr[high]);
return i+1;
}
void quickSort(int arr[], int low, int high){
if(low < high){
int pivot = partition(arr, low, high);
quickSort(arr, low, pivot-1);
quickSort(arr, pivot+1, high);
}
}
// for printing the array
void printArray(int arr[], int n){
for(int i=0; i<n; i++){
cout<<arr[i]<<" " ;
}
}
// main function
int main()
{
int arr[100], pivot, low, high, n;
cout<<"how many elements you want to put in the array"<<endl;
cin>>n;
cout<<endl;
cout<<"insert elements in the array"<<endl;
for(int i =0; i<n; i++){
cin>>arr[i];
}
cout<<"Array before Quick Sort"<<endl;
printArray(arr, n);
quickSort(arr, 0, n);
cout<<endl;
cout<<"Array after quick sort"<<endl;
printArray(arr, n);
return 0 ;
}
xxxxxxxxxx
Implementation of Quick Sort Algorithm in C:
# include <stdio.h>
// to swap two numbers
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // selecting last element as pivot
int i = (low - 1); // index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If the current element is smaller than or equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
/*
a[] is the array, p is starting index, that is 0,
and r is the last index of array.
*/
void quicksort(int a[], int p, int r)
{
if(p < r)
{
int q;
q = partition(a, p, r);
quicksort(a, p, q-1);
quicksort(a, q+1, r);
}
}
// function to print the array
void printArray(int a[], int size)
{
int i;
for (i=0; i < size; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
int main()
{
int arr[] = {9, 7, 5, 11, 12, 2, 14, 3, 10, 6};
int n = sizeof(arr)/sizeof(arr[0]);
// call quickSort function
quicksort(arr, 0, n-1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
xxxxxxxxxx
#function for doing partitining
from array import array
def partition(array,low,high):
i = low-1
pivot = array[high]
for j in range(low,high):
if array[j] <= pivot:
i = i +1
(array[i] , array[j]) = (array[j], array[i])
(array[i+1],array[high]) = (array[high], array[i+1])
return i+1
# function for quick sort
def quickSort(array, low, high):
if (low<high):
pivot = partition(array,low,high)
quickSort(array,low, pivot-1)
quickSort(array,pivot+1,high)
# driver code
array = [10,7,8,9,1,5]
n = len(array)
print("array before quick sort",array)
quickSort(array,0,n-1)
print("after quicksort",array)