Skip to content

Instantly share code, notes, and snippets.

@harish-r
Created April 7, 2014 18:02
Show Gist options
  • Save harish-r/10025689 to your computer and use it in GitHub Desktop.
Save harish-r/10025689 to your computer and use it in GitHub Desktop.
Quick Sort Algorithm in C++
/*
* Quick Sort Algorithm
* Language: C++
* Created by: Harish R
*/
#include<iostream>
using namespace std;
int partition(int *a, int m, int n)
{
int i,j,pindex,pivot;
pindex = m;
pivot = a[n];
for(i=m;i<n;i++)
{
if(a[i] <= pivot)
{
swap(a[pindex], a[i]);
pindex++;
}
}
swap(a[pindex], a[n]);
return pindex;
}
int quicksort(int *a, int m, int n)
{
int index;
if(m>=n)
return 0;
{
index = partition(a,m,n);
quicksort(a, m, index-1);
quicksort(a, index+1, n);
}
}
int main()
{
int a[] = {7,2,1,6,8,5,3,4};
int i;
quicksort(a,0,7);
cout <<"After Sorting" << endl;
for(i=0;i<8;i++)
cout <<a[i] <<endl;
}
@crisunder
Copy link

hello good sir! im new to programming and i want to learn c++, im just curious on what is the "int j" for.........
ps: im trying to understand this quicksort algorithm because i will be reporting about it on school

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment