forked from tmoynandy/DataStructure-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.c
59 lines (45 loc) · 867 Bytes
/
quicksort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
int partition(int* arr, int p, int r){
int i,j,temp,temp1,x;
j=p;
i=j-1;
x=*(arr+r);
for(j=p;j<r;j++){
if(x > *(arr+j)){
i++;
temp=*(arr+j);
*(arr+j)=*(arr+i);
*(arr+i)=temp;
}
}
temp1=*(arr+i+1);
*(arr+i+1)=*(arr+r);
*(arr+r)=temp1;
return i+1;
}
void quicksort(int* arr, int p, int r){
int q,i;
if(p<r){
q=partition(arr,p,r);
quicksort(arr,p,q-1);
quicksort(arr,q+1,r);
}
}
void main(){
int n,i;
int *arr;
printf("\nEnter size of array");
scanf("%d",&n);
arr=malloc(n*sizeof(int));
printf("\nENter elements of the array :-");
for(i=0;i<n;i++){
printf("\nEnter element no.%d : ",i);
scanf("%d",arr+i);
}
quicksort(arr,0,n-1);
printf("\nThe sorted array is :-");
for(i=0;i<n;i++){
printf("\n%d",*(arr+i));
}
}