Skip to content

Update Repository #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion Bubble-sort.cpp
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ void sort(int *a, int n) //a=>array, n=>length of array
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
for(j=i;j<n-i-1;j++) //as last element of array gets automatically sorted.Thus, can be ignored.
{
if(a[i]>a[j])
{
47 changes: 47 additions & 0 deletions quicksort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include<iostream>
#define n 8
using namespace std;
int partition(int a[],int first,int last){
int pivot=a[first],i=first,j=last+1;
do{
do{
++i;
}while(a[i]<pivot && i<=last);

do{
--j;
}while(pivot<a[j]);
if(i<j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[first]=a[j];
a[j]=pivot;
return first;
}

void quicksort(int a[],int p, int q){
if(p<q){
int x=partition(a,p,q);
quicksort(a,p,x-1);
quicksort(a,x+1,q);
}
}
void print(int a[]){
for(int i=0;i<n;i++){
cout<<a[i]<<endl;
}
}
int main(){
cout<<"enter "<<n<<" elements:\n";
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"sorted array: \n";
quicksort(a,0,n-1);
print(a);
return 0;
}