-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathquicksort.c
63 lines (53 loc) · 1.24 KB
/
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
58
59
60
61
62
#include<stdio.h>
void Quick_Sort(int [],int , int );
int partation(int [],int,int);
void swap(int*, int* );
int main(){
int n;
printf("Enter the size of array : ");
scanf("%d",&n);
int arr[n+1];
printf("Enter the value of array : ");
for (int i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}
printf("\nBefore Sort : \n\n ");
for (int i = 0; i < n; i++)
{
printf("%d ",arr[i]);
}
int left = 0;
int right = n-1;
Quick_Sort(arr,left,right);
printf("\nAfter Sort : \n\n ");
for (int i = 0; i < n; i++)
{
printf("%d ",arr[i]);
}
return 0;
}
void Quick_Sort(int arr[],int left , int right ){
int midpart;
if(left < right){
midpart = partation(arr,left,right);
Quick_Sort(arr,left, midpart);
Quick_Sort(arr,midpart+1,right);
}
}
int partation(int arr[25],int left, int right){
int i = left, j = right, temp, pivot = left;
while(i<j){
while(arr[i]<=arr[pivot]) i++;
while(arr[j]>arr[pivot]) j--;
if(i< j ) swap(&arr[i],&arr[j]);
}
swap(&arr[pivot],&arr[j]);
return j;
}
void swap(int* arri, int* arrj){
int temp;
temp = *arri;
*arri = *arrj;
*arrj = temp;
}