I recently learned quick sort through https://www.geeksforgeeks.org/quick-sort/ but found it hard to follow. So, from what I understand wrote the following program.
#include <stdio.h>
void quick_sort(int[],int,int);
int main(){
int arr[100];
int n;
printf("Enter the elements :");
scanf("%d",&n);
for(int i = 0 ; i < n ; i++){
printf("%d:",i+1);
scanf("%d",&arr[i]);
}
int arr_size = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < n; i++){
printf("%d ",arr[i]);
}
printf("\n");
quick_sort(arr,0,n - 1);
for(int i = 0; i < n; i++){
printf("%d ",arr[i]);
}
printf("\n");
}
void quick_sort(int arr[],int pivot_, int right_){
//Base condtion.
if(pivot_ == right_ )//pivot = left = right == no more check
return;
int i ;
int pivot, left ,right;
pivot = pivot_;//first element...
right = right_;//last element...
left = pivot + 1; // second element.
int middle;
while( left <= right ){
if(arr[left] >= arr[pivot]){
if(arr[right] <= arr[pivot]){
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
right --;
left ++;
}else{
right --; // left > pivot but right !< pivot
}
}else{
left ++;// left is not > pivot.
}
}
i = pivot + 1;
while(arr[i] < arr[pivot]) i++;
i--; // last smaller value than pivot encountered.
middle = i; // swappppppp..
int temp = arr[pivot];
arr[pivot] = arr[i];
arr[i] = temp;
// now left of i is less than pivot and right of is greater than pivot.
quick_sort(arr,0,middle);
quick_sort(arr,middle + 1,right_);
}
//Base condtion.-->condition\$\endgroup\$