-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick.c
More file actions
57 lines (57 loc) · 1.03 KB
/
Copy pathquick.c
File metadata and controls
57 lines (57 loc) · 1.03 KB
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>
void swap(int a, int b,int A[])
{
int t= A[a];
A[a] = A[b];
A[b] = t;
}
int part(int A[], int low, int high)
{
int pivot = A[high];
int i = (low-1);
for (int j = low; j <= high-1; j++)
{
if (A[j] < pivot)
{
i++;
swap(i,j,A);
}
}
swap(i+1,high,A);
return (i+1);
}
void quick(int A[], int low, int high)
{
if (low < high)
{
int pivot = part(A, low, high);
quick(A, low, pivot - 1);
quick(A, pivot + 1, high);
}
}
void printArray(int A[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", A[i]);
}
printf("\n");
}
int main()
{
int x;
printf("Enter number of elements: ");
scanf("%d",&x);
int A[x];
printf("Enter your elements: ");
for (int i = 0; i < x; i++)
{
scanf("%d", &A[i]);
}
printf("Original array: \n");
printArray(A,x);
quick(A,0,x-1);
printf("Sorted array: \n");
printArray(A,x);
return 0;
}