0% found this document useful (0 votes)
20 views2 pages

Quick Sort Algorithm in C Code

The document contains a C program implementing the Quick Sort algorithm, which sorts an array using a divide-and-conquer approach. It includes functions for swapping elements, partitioning the array around a pivot, and recursively sorting the partitions. The program also prints the original and sorted arrays, and provides an explanation of the Quick Sort process and its time complexity.

Uploaded by

sumitjangra0602
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Quick Sort Algorithm in C Code

The document contains a C program implementing the Quick Sort algorithm, which sorts an array using a divide-and-conquer approach. It includes functions for swapping elements, partitioning the array around a pivot, and recursively sorting the partitions. The program also prints the original and sorted arrays, and provides an explanation of the Quick Sort process and its time complexity.

Uploaded by

sumitjangra0602
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

quick.

1 #include <stdio.h>
2
3 // Function to swap two elements
4 void swap(int* a, int* b) {
5 int temp = *a;
6 *a = *b;
7 *b = temp;
8 }
9
10 // Partition function for Quick Sort
11 int partition(int arr[], int low, int high) {
12 int pivot = arr[high]; // Choosing the last element as pivot
13 int i = (low - 1); // Index of smaller element
14
15 for (int j = low; j < high; j++) {
16 // If current element is smaller than pivot
17 if (arr[j] < pivot) {
18 i++; // Move the smaller element index forward
19 swap(&arr[i], &arr[j]); // Swap smaller element with the larger one
20 }
21 }
22 swap(&arr[i + 1], &arr[high]); // Place pivot in its correct position
23 return (i + 1); // Return pivot index
24 }
25
26 // Quick Sort function (recursive function)
27 void quickSort(int arr[], int low, int high) {
28 if (low < high) {
29 // Find partition index
30 int pi = partition(arr, low, high);
31
32 // Recursively apply Quick Sort to left part
33 quickSort(arr, low, pi - 1);
34 // Recursively apply Quick Sort to right part
35 quickSort(arr, pi + 1, high);
36 }
37 }
38
39 // Function to print the array
40 void printArray(int arr[], int size) {
41 for (int i = 0; i < size; i++)
42 printf("%d ", arr[i]);
43 printf("\n");
44 }
45
46 int main() {
47 int arr[] = {10, 7, 8, 9, 1, 5};
48 int n = sizeof(arr) / sizeof(arr[0]);
49
50 printf("Original array: ");
51 printArray(arr, n);
52
53 // Quick Sort Algorithm
54 quickSort(arr, 0, n - 1);
55
56 printf("Sorted array: ");
57 printArray(arr, n);
58
59 return 0;
60 }
61
62 /*
63 ---------------------- QUICK SORT EXPLANATION ----------------------
64 1. Quick Sort follows the divide-and-conquer approach.
65 2. A pivot element is chosen (last element in this case).
66 3. The array is partitioned such that:
67 - Elements smaller than the pivot go to the left.
68 - Elements greater than the pivot go to the right.
69 4. The process is recursively applied to the left and right partitions.
70 5. The time complexity:
71 - Best/Average Case: O(n log n)
72 - Worst Case (Already Sorted Array with Bad Pivot Choice): O(n²)
73 6. Quick Sort is one of the fastest sorting algorithms in practice.
74 ---------------------------------------------------------------------
75 */
76

You might also like