Skip to content

Commit 855b3dd

Browse files
Create quicksort.c
1 parent 7e77a3c commit 855b3dd

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

sorting/Python/quicksort.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include<iostream.h>
2+
#include<conio.h>
3+
4+
void swap(int* a, int* b)
5+
{
6+
int t = *a;
7+
*a = *b;
8+
*b = t;
9+
}
10+
11+
int partition (int arr[], int low, int high)
12+
{
13+
int pivot = arr[high]; // pivot
14+
int i = (low - 1); // Index of smaller element
15+
16+
for (int j = low; j <= high- 1; j++)
17+
{
18+
if (arr[j] <= pivot)
19+
{
20+
i++;
21+
swap(&arr[i], &arr[j]);
22+
}
23+
}
24+
swap(&arr[i + 1], &arr[high]);
25+
return (i + 1);
26+
}
27+
28+
void quickSort(int arr[], int low, int high)
29+
{
30+
if (low < high)
31+
{
32+
int pi = partition(arr, low, high);
33+
34+
quickSort(arr, low, pi - 1);
35+
quickSort(arr, pi + 1, high);
36+
}
37+
}
38+
39+
void printArray(int arr[], int size)
40+
{
41+
int i;
42+
for (i=0; i < size; i++)
43+
cout<<arr[i];
44+
cout<<n;
45+
}

0 commit comments

Comments
 (0)