-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp15.c
54 lines (50 loc) · 832 Bytes
/
exp15.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
#include<stdio.h>
void qs(int arr[],int p,int r)
{
if(p<r)
{
int q;
q=partition(arr,p,r);
qs(arr,p,q-1);
qs(arr,q+1,r);
}
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int partition(int arr[],int p,int r)
{
int x,j,i;
x=arr[r];
i=p-1;
for (j = p; j <= r- 1; j++)
{
if (arr[j] < x)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[r]);
return (i + 1);
}
void main()
{
int n,i;
printf("Enter number of elements : ");
scanf("%d",&n);
int arr[n];
printf("Enter elements in array -> \n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
qs(arr, 0, n-1);
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}