forked from anish2210/hacktober2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QUICK_SORT.c
60 lines (51 loc) · 1.13 KB
/
QUICK_SORT.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
#include<stdio.h>
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int partition(int arr[],int left,int right)
{
int i,j;
int pivot=arr[left];
i=left+1;
j=right;
do
{
while (arr[i] <= pivot)
i++;
while (arr[j] > pivot)
j--;
if (i < j)
swap(&arr[left],&arr[j]);
}while(i<j);
swap(&arr[left],&arr[j]);
return j;
}
void quicksort(int arr[],int left,int right)
{
if(left < right)
{
int p=partition(arr,left,right);
quicksort(arr,left,p-1);
quicksort(arr,p+1,right);
}
}
void main()
{
int i,j,n;
printf("Enter the size of the array: ");
scanf("%d",&n);
int arr[n];
printf("Enter the numbers to be sorted using Quick Sort in the array below:-\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("Unsorted array: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
quicksort(arr,0,n-1);
printf("\nSorted array: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
}