Created
April 22, 2018 08:26
-
-
Save Darkborderman/f4cd3551fe455e13aa40925b2de8d84b to your computer and use it in GitHub Desktop.
A quickSort algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
void quickSort(int* data, int left, int right); | |
void swap(int* data, int i, int j); | |
int partition(int* data,int left,int right); | |
int main() | |
{ | |
int input[10]={1,6,3,2,5,8,7,0,9,4}; | |
int left=0,right=9; | |
quickSort(input,left,right); | |
for(int i=0;i<=9;i++) cout<<input[i]<<" "; | |
return 0; | |
} | |
void quickSort(int* data, int left, int right) | |
{ | |
if(left<right) | |
{ | |
int index=partition(data,left,right); | |
quickSort(data,left,index-1); | |
quickSort(data,index+1,right); | |
} | |
} | |
void swap(int* data, int i, int j) | |
{ | |
int temp=data[i]; | |
data[i]=data[j]; | |
data[j]=temp; | |
} | |
int partition(int* data,int left, int right) | |
{ | |
int index=left-1; | |
for(int i=left;i<right;i++) | |
{ | |
if(data[i]<data[right]) | |
{ | |
index++; | |
swap(data,index,i); | |
} | |
} | |
swap(data,index+1,right); | |
return index+1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment