-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63a4afe
commit af89662
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//QuickSort in C++ | ||
#include<iostream> | ||
using namespace std; | ||
|
||
|
||
int Partition(int *A, int start, int end) | ||
{ | ||
int i; | ||
int pivot=A[end]; | ||
int part_i=start; | ||
for(i=start; i<end; i++) | ||
{ | ||
if(A[i]<=pivot) | ||
{ | ||
swap(A[i],A[part_i]); | ||
part_i++; | ||
} | ||
} | ||
swap(A[part_i],A[end]); | ||
return part_i; | ||
} | ||
|
||
void quicksort(int *A, int start, int end) | ||
{ | ||
if(start<end) | ||
{ | ||
int part_i=Partition(A,start,end); | ||
quicksort(A,start,part_i-1); | ||
quicksort(A,part_i+1,end); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int arr[20],i,n; | ||
cout<<"Enter the number of elements that your array will contain:"<<endl; | ||
cin>>n; | ||
cout<<"Enter the elements:"<<endl; | ||
for(i=0;i<n;i++) | ||
cin>>arr[i]; | ||
|
||
quicksort(arr,0,n-1); | ||
for(i=0;i<n;i++) | ||
cout<<arr[i]<<" "; | ||
|
||
return 0; | ||
} |