-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick_Sort_First_element_Pivot.cpp
65 lines (59 loc) · 1 KB
/
Quick_Sort_First_element_Pivot.cpp
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
63
64
65
#include<stdio.h>
int partition(int[],int,int);
int quicksort(int[],int,int);
int main()
{
int arr[100];
int size,i;
printf("Enter size:\n");
scanf("%d",&size);
printf("Enter the array elements:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
quicksort(arr,0,size-1);
printf("After quicksort , the sorted array is :\n");
for(i=0;i<size;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
int partition(int arr[],int start,int end)
{
int p=start;
while(start<end)
{
while((p<end)&&(arr[p]<arr[end]))
end--;
if((p<end)&&(arr[p]>arr[end]))
{
int temp=arr[p];
arr[p]=arr[end];
arr[end]=temp;
start++;
}
while((p>start)&&(arr[p]>arr[start]))
start++;
if((p>start)&&(arr[p]<arr[start]))
{
int temp=arr[p];
arr[p]=arr[start];
arr[start]=temp;
end--;
}
}
return p;
}
int quicksort(int arr[],int start, int end)
{
int pIndex;
if (start<end)
{
pIndex=partition(arr,start,end);
quicksort(arr,start,pIndex-1);
quicksort(arr,pIndex+1,end);
}
return 0;
}