Skip to content

Commit 8974cc7

Browse files
123vivekrsangamcse
authored andcommitted
quick_sort.c: Add Quick Sort in C
This adds Quick Sort algorithm written in C Closes #146
1 parent b19bd47 commit 8974cc7

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ This repository contains examples of various algorithms written on different pro
1313
| Algorithm | C | CPP | Java | Python |
1414
|:----------------------------------------------------------------------------------------------- |:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|:-------------------------------------:|
1515
| [Euclidean GCD](https://en.wikipedia.org/wiki/Euclidean_algorithm) | [:octocat:](euclidean_gcd/C) | | [:octocat:](euclidean_gcd/Java) | [:octocat:](euclidean_gcd/Python) |
16-
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | | | | [:octocat:](quicksort/Python) |
1716
| [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort) | [:octocat:](merge_sort/C) | | | [:octocat:](merge_sort/Python) |
17+
| [QuickSort](https://en.wikipedia.org/wiki/Quicksort) | [:octocat:](quicksort/C) | | | [:octocat:](quicksort/Python) |
1818
| [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | [:octocat:](insertion_sort/Cpp) | | [:octocat:](insertion_sort/Python) |
1919
| [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) |[:octocat:](counting_sort/C) | [:octocat:](counting_sort/Cpp) | | [:octocat:](counting_sort/Python) |
2020
| [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | [:octocat:](radix_sort/Cpp) | | [:octocat:](radix_sort/Python) |

quicksort/C/quick_sort.c

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <stdio.h>
2+
3+
void quick_sort(int[], int, int);
4+
void print_array(int[], int);
5+
int partition(int[], int, int);
6+
void swap(int*, int*);
7+
8+
int main() {
9+
int arr[] = {45, 92, 54, 23, 6, 4, 12};
10+
int n = sizeof(arr) / sizeof(arr[0]);
11+
quick_sort(arr, 0, n);
12+
printf("Sorted array: \n");
13+
print_array(arr, n);
14+
}
15+
16+
void swap(int *a, int *b) {
17+
int temp = *a;
18+
*a = *b;
19+
*b = temp;
20+
}
21+
22+
/* Quick Sort algorithm */
23+
void quick_sort(int arr[], int lb, int ub) {
24+
if (lb < ub) {
25+
int pi = partition(arr, lb, ub);
26+
27+
quick_sort(arr, lb, pi - 1);
28+
quick_sort(arr, pi + 1, ub);
29+
}
30+
}
31+
32+
/* Partitioning function */
33+
int partition(int arr[], int low, int high) {
34+
int i = low + 1;
35+
int piv = arr[low];
36+
37+
for (int j = low + 1; j <= high; ++j) {
38+
if (arr[j] < piv) {
39+
swap(&arr[i], &arr[j]);
40+
i++;
41+
}
42+
}
43+
swap(&arr[low], &arr[i - 1]);
44+
return i - 1;
45+
}
46+
47+
/* Function to print array */
48+
void print_array(int arr[], int size) {
49+
int i;
50+
for (i = 0; i < size; i++)
51+
printf("%d ", arr[i]);
52+
printf("\n");
53+
}

0 commit comments

Comments
 (0)