Skip to content
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

create my_quicksort.c #236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions Sorting/my_quicksort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <stdio.h>
#include <stdlib.h>

void swap(int *a,int *b){

int temp;
temp=*a;
*a=*b;
*b=temp;

};
void printarray(int *arr, int n){

int i=0;

for( i=0; i<n; ++i){

printf("%d ",arr[i]);

}

printf("\n");

};
int partition(int *arr, int start, int end){

int pivot=arr[end];

int i=(start-1),j=0;

for( j=start; j<end; j++){

if(arr[j]<pivot){

i++;
swap(&arr[i],&arr[j]);

}

}

swap(&arr[i+1],&arr[end]);

return i+1;

};
void quicksort(int *arr, int start, int end){

if(start<end){

int pn=partition(arr,start,end);

quicksort(arr,start,pn-1);

quicksort(arr,pn+1,end);

}

};

int main(){

int n=0,i=0;

printf("Enter the bumber of elements to be sorted\n");

scanf("%d",&n);

int *arr=(int*)malloc(sizeof(int)*n);

for( i=0; i<n; i++){

scanf("%d",&arr[i]);
}

quicksort(arr,0,n-1);

printf("The sorted array is\n");

printarray(arr,n);

}