Quick sort algorithm and source code

#include<iostream>
#include<conio.h>
using namespace std;

void swap(int arr[], int x, int y){
	int temp=arr[x];
	arr[x]=arr[y];
	arr[y]=temp;
}
int partition(int arr[], int s, int e){
	int pivot = arr[s];
	int count=0;
	for(int i=s+1; i<=e; i++ ){
		if(arr[i]<=pivot){
			count++;
		}
	}	
	int pivotIndex=s+count;
	swap(arr, pivotIndex, s);
	int i=s, j=e;
	while(i< pivotIndex && j> pivotIndex){
		while(arr[i]< pivot){
			i++;
		}
		while(arr[j]> pivot){
			j--;
		}
		if( i<pivotIndex && j>pivotIndex ){
			swap(arr, i++, j--);
		}
	}
}


void quickSort(int arr[], int s, int e){
	if(s<e){
		int pivotIndex = partition(arr, s, e);
		quickSort(arr, s, pivotIndex-1);
		quickSort(arr, pivotIndex+1, e);		
	}
}

int main(){

	int arr[]={8, 10, 2, 20, 12, 15, 11, 25};
	int n=8;
	quickSort(arr, 0, n-1);
	
	for(int i=0; i<8; i++){
		cout<< arr[i] << "\t";
	}
		
	getch();
	return 0;
}
© 2016 - 2022, All Rights are Reserved.