Wednesday 29 November 2017

Implementation of Quick Sort in C/C++

// Created By Mohd Mujtaba... Visit https://playwithcplus.blogspot.in fb-> //facebook.com/playwithcplus

#include<bits/stdc++.h>
using namespace std;

int part(int arr[],int low,int high)
{
int i=(low-1);
int pivot=arr[high];
for(int j=low;j<high;j++)
{
if(arr[j]<=pivot)
{
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[high]);
return(i+1);
}
void quicksort(int arr[],int low,int high)
{
if(low<high)
{
int pi=part(arr,low,high);
quicksort(arr,low,pi-1);
quicksort(arr,pi+1,high);
}
}
main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    quicksort(arr, 0, n-1);
    printf("Sorted array:");
    for(int i=0;i<n;i++)
    cout<<" "<< arr[i];
}

No comments:

Post a Comment

Amazon1Ads