{"id":25142,"date":"2017-10-15T14:45:35","date_gmt":"2017-10-15T09:15:35","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25142"},"modified":"2017-10-15T14:45:35","modified_gmt":"2017-10-15T09:15:35","slug":"quicksort","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/quicksort\/","title":{"rendered":"QuickSort"},"content":{"rendered":"<p>Like <a href=\"http:\/\/quiz.geeksforgeeks.org\/merge-sort\/\" target=\"_blank\" rel=\"noopener noreferrer\">Merge Sort<\/a>, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. <span id=\"more-142309\"><\/span>There are many different versions of quickSort that pick pivot in different ways.<\/p>\n<ol>\n<li>Always pick first element as pivot.<\/li>\n<li>Always pick last element as pivot (implemented below)<\/li>\n<li>Pick a random element as pivot.<\/li>\n<li>Pick median as pivot.<\/li>\n<\/ol>\n<p>The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.<\/p>\n<p><strong>Pseudo Code for recursive QuickSort function :<\/strong><\/p>\n<pre>\/* low  --&gt; Starting index,  high  --&gt; Ending index *\/\r\nquickSort(arr[], low, high)\r\n{\r\n    if (low &lt; high)\r\n    {\r\n        \/* pi is partitioning index, arr[p] is now\r\n           at right place *\/\r\n        pi = partition(arr, low, high);\r\n\r\n        quickSort(arr, low, pi - 1);  \/\/ Before pi\r\n        quickSort(arr, pi + 1, high); \/\/ After pi\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-25147\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/QuickSort2.png\" alt=\"Quick Sort\" width=\"703\" height=\"312\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/QuickSort2.png 703w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/QuickSort2-300x133.png 300w\" sizes=\"(max-width: 703px) 100vw, 703px\" \/><\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Partition Algorithm<\/strong><br \/>\nThere can be many ways to do partition, following pseudo code adopts the method given in CLRS book. The logic is simple, we start from the leftmost element and keep track of index of smaller (or equal to) elements as i. While traversing, if we find a smaller element, we swap current element with arr[i]. Otherwise we ignore current element.<\/p>\n<pre>\/* low  --&gt; Starting index,  high  --&gt; Ending index *\/\r\nquickSort(arr[], low, high)\r\n{\r\n    if (low &lt; high)\r\n    {\r\n        \/* pi is partitioning index, arr[p] is now\r\n           at right place *\/\r\n        pi = partition(arr, low, high);\r\n\r\n        quickSort(arr, low, pi - 1);  \/\/ Before pi\r\n        quickSort(arr, pi + 1, high); \/\/ After pi\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Pseudo code for partition()<\/strong><\/p>\n<pre>\/* This function takes last element as pivot, places\r\n   the pivot element at its correct position in sorted\r\n    array, and places all smaller (smaller than pivot)\r\n   to left of pivot and all greater elements to right\r\n   of pivot *\/\r\npartition (arr[], low, high)\r\n{\r\n    \/\/ pivot (Element to be placed at right position)\r\n    pivot = arr[high];  \r\n \r\n    i = (low - 1)  \/\/ Index of smaller element\r\n\r\n    for (j = low; j &lt;= high- 1; j++)\r\n    {\r\n        \/\/ If current element is smaller than or\r\n        \/\/ equal to pivot\r\n        if (arr[j] &lt;= pivot)\r\n        {\r\n            i++;    \/\/ increment index of smaller element\r\n            swap arr[i] and arr[j]\r\n        }\r\n    }\r\n    swap arr[i + 1] and arr[high])\r\n    return (i + 1)\r\n}\r\n<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Illustration of partition() :<\/strong><\/p>\n<pre>arr[] = {10, 80, 30, 90, 40, 50, 70}\r\nIndexes:  0   1   2   3   4   5   6 \r\n\r\nlow = 0, high =  6, pivot = arr[h] = 70\r\nInitialize index of smaller element, <strong>i = -1<\/strong>\r\n\r\nTraverse elements from j = low to high-1\r\n<strong>j = 0<\/strong> : Since arr[j] &lt;= pivot, do i++ and swap(arr[i], arr[j])\r\n<strong>i = 0 <\/strong>\r\narr[] = {10, 80, 30, 90, 40, 50, 70} \/\/ No change as i and j \r\n                                     \/\/ are same\r\n\r\n<strong>j = 1<\/strong> : Since arr[j] &gt; pivot, do nothing\r\n\/\/ No change in i and arr[]\r\n\r\n<strong>j = 2<\/strong> : Since arr[j] &lt;= pivot, do i++ and swap(arr[i], arr[j])\r\n<strong>i = 1<\/strong>\r\narr[] = {10, 30, 80, 90, 40, 50, 70} \/\/ We swap 80 and 30 \r\n\r\n<strong>j = 3<\/strong> : Since arr[j] &gt; pivot, do nothing\r\n\/\/ No change in i and arr[]\r\n\r\n<strong>j = 4<\/strong> : Since arr[j] &lt;= pivot, do i++ and swap(arr[i], arr[j])\r\n<strong>i = 2<\/strong>\r\narr[] = {10, 30, 40, 90, 80, 50, 70} \/\/ 80 and 40 Swapped\r\n<strong>j = 5<\/strong> : Since arr[j] &lt;= pivot, do i++ and swap arr[i] with arr[j] \r\n<strong>i = 3<\/strong> \r\narr[] = {10, 30, 40, 50, 80, 90, 70} \/\/ 90 and 50 Swapped \r\n\r\nWe come out of loop because j is now equal to high-1.\r\n<strong>Finally we place pivot at correct position by swapping\r\narr[i+1] and arr[high] (or pivot)<\/strong> \r\narr[] = {10, 30, 40, 50, 70, 90, 80} \/\/ 80 and 70 Swapped \r\n\r\nNow 70 is at its correct place. All elements smaller than\r\n70 are before it and all elements greater than 70 are after\r\nit.<\/pre>\n<div id=\"practice\">\n<h4 id=\"recommended-please-solve-it-on-practice-first-before-moving-on-to-the-solution\">Recommended: Please solve it on &#8220;<b><u>PRACTICE<\/u><\/b>&#8221; first, before moving on to the solution.<\/h4>\n[ad type=&#8221;banner&#8221;]\n<\/div>\n<p><strong>Implementation:<\/strong><br \/>\nFollowing are C++, Java and Python implementations of QuickSort.<\/p>\n<p><strong>C++<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">c<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/* C implementation QuickSort *\/<br\/>#include&lt;stdio.h&gt;<br\/> <br\/>\/\/ A utility function to swap two elements<br\/>void swap(int* a, int* b)<br\/>{<br\/>    int t = *a;<br\/>    *a = *b;<br\/>    *b = t;<br\/>}<br\/> <br\/>\/* This function takes last element as pivot, places<br\/>   the pivot element at its correct position in sorted<br\/>    array, and places all smaller (smaller than pivot)<br\/>   to left of pivot and all greater elements to right<br\/>   of pivot *\/<br\/>int partition (int arr[], int low, int high)<br\/>{<br\/>    int pivot = arr[high];    \/\/ pivot<br\/>    int i = (low - 1);  \/\/ Index of smaller element<br\/> <br\/>    for (int j = low; j &lt;= high- 1; j++)<br\/>    {<br\/>        \/\/ If current element is smaller than or<br\/>        \/\/ equal to pivot<br\/>        if (arr[j] &lt;= pivot)<br\/>        {<br\/>            i++;    \/\/ increment index of smaller element<br\/>            swap(&amp;arr[i], &amp;arr[j]);<br\/>        }<br\/>    }<br\/>    swap(&amp;arr[i + 1], &amp;arr[high]);<br\/>    return (i + 1);<br\/>}<br\/> <br\/>\/* The main function that implements QuickSort<br\/> arr[] --&gt; Array to be sorted,<br\/>  low  --&gt; Starting index,<br\/>  high  --&gt; Ending index *\/<br\/>void quickSort(int arr[], int low, int high)<br\/>{<br\/>    if (low &lt; high)<br\/>    {<br\/>        \/* pi is partitioning index, arr[p] is now<br\/>           at right place *\/<br\/>        int pi = partition(arr, low, high);<br\/> <br\/>        \/\/ Separately sort elements before<br\/>        \/\/ partition and after partition<br\/>        quickSort(arr, low, pi - 1);<br\/>        quickSort(arr, pi + 1, high);<br\/>    }<br\/>}<br\/> <br\/>\/* Function to print an array *\/<br\/>void printArray(int arr[], int size)<br\/>{<br\/>    int i;<br\/>    for (i=0; i &lt; size; i++)<br\/>        printf(&quot;%d &quot;, arr[i]);<br\/>    printf(&quot;\\n&quot;);<br\/>}<br\/> <br\/>\/\/ Driver program to test above functions<br\/>int main()<br\/>{<br\/>    int arr[] = {10, 7, 8, 9, 1, 5};<br\/>    int n = sizeof(arr)\/sizeof(arr[0]);<br\/>    quickSort(arr, 0, n-1);<br\/>    printf(&quot;Sorted array: \\n&quot;);<br\/>    printArray(arr, n);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Java<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java program for implementation of QuickSort<br\/>class QuickSort<br\/>{<br\/>    \/* This function takes last element as pivot,<br\/>       places the pivot element at its correct<br\/>       position in sorted array, and places all<br\/>       smaller (smaller than pivot) to left of<br\/>       pivot and all greater elements to right<br\/>       of pivot *\/<br\/>    int partition(int arr[], int low, int high)<br\/>    {<br\/>        int pivot = arr[high]; <br\/>        int i = (low-1); \/\/ index of smaller element<br\/>        for (int j=low; j&lt;high; j++)<br\/>        {<br\/>            \/\/ If current element is smaller than or<br\/>            \/\/ equal to pivot<br\/>            if (arr[j] &lt;= pivot)<br\/>            {<br\/>                i++;<br\/> <br\/>                \/\/ swap arr[i] and arr[j]<br\/>                int temp = arr[i];<br\/>                arr[i] = arr[j];<br\/>                arr[j] = temp;<br\/>            }<br\/>        }<br\/> <br\/>        \/\/ swap arr[i+1] and arr[high] (or pivot)<br\/>        int temp = arr[i+1];<br\/>        arr[i+1] = arr[high];<br\/>        arr[high] = temp;<br\/> <br\/>        return i+1;<br\/>    }<br\/> <br\/> <br\/>    \/* The main function that implements QuickSort()<br\/>      arr[] --&gt; Array to be sorted,<br\/>      low  --&gt; Starting index,<br\/>      high  --&gt; Ending index *\/<br\/>    void sort(int arr[], int low, int high)<br\/>    {<br\/>        if (low &lt; high)<br\/>        {<br\/>            \/* pi is partitioning index, arr[pi] is <br\/>              now at right place *\/<br\/>            int pi = partition(arr, low, high);<br\/> <br\/>            \/\/ Recursively sort elements before<br\/>            \/\/ partition and after partition<br\/>            sort(arr, low, pi-1);<br\/>            sort(arr, pi+1, high);<br\/>        }<br\/>    }<br\/> <br\/>    \/* A utility function to print array of size n *\/<br\/>    static void printArray(int arr[])<br\/>    {<br\/>        int n = arr.length;<br\/>        for (int i=0; i&lt;n; ++i)<br\/>            System.out.print(arr[i]+&quot; &quot;);<br\/>        System.out.println();<br\/>    }<br\/> <br\/>    \/\/ Driver program<br\/>    public static void main(String args[])<br\/>    {<br\/>        int arr[] = {10, 7, 8, 9, 1, 5};<br\/>        int n = arr.length;<br\/> <br\/>        QuickSort ob = new QuickSort();<br\/>        ob.sort(arr, 0, n-1);<br\/> <br\/>        System.out.println(&quot;sorted array&quot;);<br\/>        printArray(arr);<br\/>    }<br\/>}<br\/>\/*This code is contributed by Rajat Mishra *\/<\/code><\/pre> <\/div>\n<p><strong>Python<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-python code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\"># Python program for implementation of Quicksort Sort<br\/> <br\/># This function takes last element as pivot, places<br\/># the pivot element at its correct position in sorted<br\/># array, and places all smaller (smaller than pivot)<br\/># to left of pivot and all greater elements to right<br\/># of pivot<br\/>def partition(arr,low,high):<br\/>    i = ( low-1 )         # index of smaller element<br\/>    pivot = arr[high]     # pivot<br\/> <br\/>    for j in range(low , high):<br\/> <br\/>        # If current element is smaller than or<br\/>        # equal to pivot<br\/>        if   arr[j] &lt;= pivot:<br\/>         <br\/>            # increment index of smaller element<br\/>            i = i+1<br\/>            arr[i],arr[j] = arr[j],arr[i]<br\/> <br\/>    arr[i+1],arr[high] = arr[high],arr[i+1]<br\/>    return ( i+1 )<br\/> <br\/># The main function that implements QuickSort<br\/># arr[] --&gt; Array to be sorted,<br\/># low  --&gt; Starting index,<br\/># high  --&gt; Ending index<br\/> <br\/># Function to do Quick sort<br\/>def quickSort(arr,low,high):<br\/>    if low &lt; high:<br\/> <br\/>        # pi is partitioning index, arr[p] is now<br\/>        # at right place<br\/>        pi = partition(arr,low,high)<br\/> <br\/>        # Separately sort elements before<br\/>        # partition and after partition<br\/>        quickSort(arr, low, pi-1)<br\/>        quickSort(arr, pi+1, high)<br\/> <br\/># Driver code to test above<br\/>arr = [10, 7, 8, 9, 1, 5]<br\/>n = len(arr)<br\/>quickSort(arr,0,n-1)<br\/>print (&quot;Sorted array is:&quot;)<br\/>for i in range(n):<br\/>    print (&quot;%d&quot; %arr[i]),<br\/> <\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>Sorted array:\r\n1 5 7 8 9 10<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Analysis of QuickSort<\/strong><br \/>\nTime taken by QuickSort in general can be written as following.<\/p>\n<pre> T(n) = T(k) + T(n-k-1) + <img decoding=\"async\" class=\"ql-img-inline-formula quicklatex-auto-format\" title=\"Rendered by QuickLaTeX.com\" src=\"http:\/\/www.geeksforgeeks.org\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.svg\" alt=\"\\theta\" width=\"9\" height=\"12\" \/>(n)<\/pre>\n<p>The first two terms are for two recursive calls, the last term is for the partition process. k is the number of elements which are smaller than pivot.<br \/>\nThe time taken by QuickSort depends upon the input array and partition strategy. Following are three cases.<\/p>\n<p><em><strong>Worst Case:<\/strong><\/em> The worst case occurs when the partition process always picks greatest or smallest element as pivot. If we consider above partition strategy where last element is always picked as pivot, the worst case would occur when the array is already sorted in increasing or decreasing order. Following is recurrence for worst case.<\/p>\n<pre> T(n) = T(0) + T(n-1) + <img decoding=\"async\" class=\"ql-img-inline-formula quicklatex-auto-format\" title=\"Rendered by QuickLaTeX.com\" src=\"http:\/\/www.geeksforgeeks.org\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.svg\" alt=\"\\theta\" width=\"9\" height=\"12\" \/>(n)\r\nwhich is equivalent to  \r\n T(n) = T(n-1) + <img decoding=\"async\" class=\"ql-img-inline-formula quicklatex-auto-format\" title=\"Rendered by QuickLaTeX.com\" src=\"http:\/\/www.geeksforgeeks.org\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.svg\" alt=\"\\theta\" width=\"9\" height=\"12\" \/>(n)\r\n<\/pre>\n<p>The solution of above recurrence is <img decoding=\"async\" class=\"ql-img-inline-formula quicklatex-auto-format\" title=\"Rendered by QuickLaTeX.com\" src=\"http:\/\/www.geeksforgeeks.org\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.svg\" alt=\"\\theta\" width=\"9\" height=\"12\" \/>(n<sup>2<\/sup>).<\/p>\n<p><em><strong>Best Case:<\/strong><\/em> The best case occurs when the partition process always picks the middle element as pivot. Following is recurrence for best case.<\/p>\n<pre> T(n) = 2T(n\/2) + <img decoding=\"async\" class=\"ql-img-inline-formula quicklatex-auto-format\" title=\"Rendered by QuickLaTeX.com\" src=\"http:\/\/www.geeksforgeeks.org\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.svg\" alt=\"\\theta\" width=\"9\" height=\"12\" \/>(n)<\/pre>\n<p>The solution of above recurrence is <img decoding=\"async\" class=\"ql-img-inline-formula quicklatex-auto-format\" title=\"Rendered by QuickLaTeX.com\" src=\"http:\/\/www.geeksforgeeks.org\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.svg\" alt=\"\\theta\" width=\"9\" height=\"12\" \/>(nLogn). It can be solved using case 2 of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Master_theorem\" target=\"_blank\" rel=\"noopener noreferrer\">Master Theorem<\/a>.<\/p>\n<p><em><strong>Average Case:<\/strong><\/em><br \/>\nTo do average case analysis, we need to <a href=\"http:\/\/www.geeksforgeeks.org\/analysis-of-algorithms-set-2-asymptotic-analysis\/\" target=\"_blank\" rel=\"noopener noreferrer\">consider all possible permutation of array and calculate time taken by every permutation which doesn\u2019t look easy<\/a>.<br \/>\nWe can get an idea of average case by considering the case when partition puts O(n\/9) elements in one set and O(9n\/10) elements in other set. Following is recurrence for this case.<\/p>\n<pre> T(n) = T(n\/9) + T(9n\/10) + <img decoding=\"async\" class=\"ql-img-inline-formula quicklatex-auto-format\" title=\"Rendered by QuickLaTeX.com\" src=\"http:\/\/www.geeksforgeeks.org\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.svg\" alt=\"\\theta\" width=\"9\" height=\"12\" \/>(n)<\/pre>\n<p>Solution of above recurrence is also O(nLogn)<\/p>\n<p>Although the worst case time complexity of QuickSort is O(n<sup>2<\/sup>) which is more than many other sorting algorithms like <a href=\"http:\/\/quiz.geeksforgeeks.org\/merge-sort\/\" target=\"_blank\" rel=\"noopener noreferrer\">Merge Sort<\/a> and <a href=\"http:\/\/quiz.geeksforgeeks.org\/heap-sort\/\" target=\"_blank\" rel=\"noopener noreferrer\">Heap Sort<\/a>, QuickSort is faster in practice, because its inner loop can be efficiently implemented on most architectures, and in most real-world data. QuickSort can be implemented in different ways by changing the choice of pivot, so that the worst case rarely occurs for a given type of data. However, merge sort is generally considered better when data is huge and stored in external storage.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>What is 3-Way QuickSort?<\/strong><br \/>\nIn simple QuickSort algorithm, we select an element as pivot, partition the array around pivot and recur for subarrays on left and right of pivot.<br \/>\nConsider an array which has many redundant elements. For example, {1, 4, 2, 4, 2, 4, 1, 2, 4, 1, 2, 2, 2, 2, 4, 1, 4, 4, 4}. If 4 is picked as pivot in Simple QuickSort, we fix only one 4 and recursively process remaining occurrences. In 3 Way QuickSort, an array arr[l..r] is divided in 3 parts:<br \/>\na) arr[l..i] elements less than pivot.<br \/>\nb) arr[i+1..j-1] elements equal to pivot.<br \/>\nc) arr[j..r] elements greater than pivot.<br \/>\nSee <a href=\"http:\/\/www.geeksforgeeks.org\/3-way-quicksort\/\" target=\"_blank\" rel=\"noopener\">this<\/a> for implementation.<\/p>\n<p><strong>How to implement QuickSort for Linked Lists?<\/strong><br \/>\n<a href=\"http:\/\/www.geeksforgeeks.org\/quicksort-on-singly-linked-list\/\" target=\"_blank\" rel=\"noopener noreferrer\">QuickSort on Singly Linked List<\/a><br \/>\n<a href=\"http:\/\/www.geeksforgeeks.org\/quicksort-for-linked-list\/\" target=\"_blank\" rel=\"noopener noreferrer\">QuickSort on Doubly Linked List<\/a><\/p>\n<p><strong>Can we implement QuickSort Iteratively?<\/strong><br \/>\nYes, please refer <a href=\"http:\/\/www.geeksforgeeks.org\/iterative-quick-sort\/\" target=\"_blank\" rel=\"noopener\">Iterative Quick Sort<\/a>.<\/p>\n<p><strong>Why Quick Sort is preferred over MergeSort for sorting Arrays<\/strong><br \/>\nQuick Sort in its general form is an in-place sort (i.e. it doesn\u2019t require any extra storage) whereas merge sort requires O(N) extra storage, N denoting the array size which may be quite expensive. Allocating and de-allocating the extra space used for merge sort increases the running time of the algorithm. Comparing average complexity we find that both type of sorts have O(NlogN) average complexity but the constants differ. For arrays, merge sort loses due to the use of extra O(N) storage space.<\/p>\n<p>Most practical implementations of Quick Sort use randomized version. The randomized version has expected time complexity of O(nLogn). The worst case is possible in randomized version also, but worst case doesn\u2019t occur for a particular pattern (like sorted array) and randomized Quick Sort works well in practice.<\/p>\n<p>Quick Sort is also a cache friendly sorting algorithm as it has good locality of reference when used for arrays.<\/p>\n<p>Quick Sort is also tail recursive, therefore tail call optimizations is done.<\/p>\n<p><strong>Why MergeSort is preferred over QuickSort for Linked Lists?<\/strong><br \/>\nIn case of linked lists the case is different mainly due to difference in memory allocation of arrays and linked lists. Unlike arrays, linked list nodes may not be adjacent in memory. Unlike array, in linked list, we can insert items in the middle in O(1) extra space and O(1) time. Therefore merge operation of merge sort can be implemented without extra space for linked lists.<\/p>\n<p>In arrays, we can do random access as elements are continuous in memory. Let us say we have an integer (4-byte) array A and let the address of A[0] be x then to access A[i], we can directly access the memory at (x + i*4). Unlike arrays, we can not do random access in linked list. Quick Sort requires a lot of this kind of access. In linked list to access i\u2019th index, we have to travel each and every node from the head to i\u2019th node as we don\u2019t have continuous block of memory. Therefore, the overhead increases for quick sort. Merge sort accesses data sequentially and the need of random access is low.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>QuickSort &#8211; Searching and Sorting &#8211; Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions.There are many different versions of quickSort that pick pivot in different ways.<\/p>\n","protected":false},"author":1,"featured_media":25150,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83502,71670],"tags":[70054,71128,71706,71133,71723,70461,71121,71135,71134,71137,71130,71122,71129,70780,70465,71710,70497,71708,71496,71720,70271,70928,71216,70017,71699,71705,70969,70262,71262,71263,71698,71701,71716,71718,71070,71700,71703,71267,71728,71713,71709,71172,71693,71704,71707,71711,70046,71714,71712,71702,71715,71717,71721,71725,71265,71677,71722,71696,71724,71719,71694,71727,71266,70056,71697,71695,70020,70967,71726],"class_list":["post-25142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-quick-sort","category-searching-and-sorting","tag-algorithm","tag-algorithm-for-bubble-sort","tag-algorithm-for-quicksort","tag-algorithm-of-bubble-sort","tag-algorithm-of-quicksort","tag-bubble-sort","tag-bubble-sort-algorithm","tag-bubble-sort-algorithm-in-data-structure","tag-bubble-sort-algorithm-with-example","tag-bubble-sort-example-step-by-step","tag-bubble-sort-in-data-structure","tag-bubble-sort-java","tag-bubble-sort-program","tag-bubblesort","tag-bucket-sort","tag-c-program-for-quick-sort","tag-counting-sort","tag-dr-scheme","tag-dynamic-programming-algorithm","tag-example-of-quicksort","tag-heap-sort","tag-heap-sort-algorithm","tag-insertion-sort","tag-insertion-sort-algorithm","tag-java","tag-java-process","tag-java-sorting-algorithms","tag-merge-sort","tag-merge-sort-algorithm","tag-merge-sort-java","tag-oops","tag-parallel-quicksort","tag-program-for-quick-sort","tag-program-for-quicksort-in-c","tag-programming","tag-programming-algorithms","tag-quick-short","tag-quick-sort-algorithm","tag-quick-sort-algorithm-in-c","tag-quick-sort-c-program","tag-quick-sort-ppt","tag-quick-sort-program","tag-quick-sort-program-in-c","tag-quick-sort-program-in-c-with-explanation","tag-quick-sort-program-in-cpp","tag-quick-sort-program-in-java","tag-quicksort","tag-quicksort-algorithm-in-data-structure","tag-quicksort-algorithm-with-example","tag-quicksort-algorithm-with-example-in-data-structure","tag-quicksort-analysis","tag-quicksort-animation","tag-quicksort-c-code","tag-quicksort-code-in-c","tag-quicksort-example","tag-quicksort-explained","tag-quicksort-explanation-with-example","tag-quicksort-in-c","tag-quicksort-in-c-program","tag-quicksort-in-data-structure-with-example","tag-quicksort-in-java","tag-quicksort-tutorial","tag-radix-sort-algorithm","tag-recursion","tag-shell-sort","tag-sorted","tag-sorting-algorithms","tag-sorting-algorithms-java","tag-what-is-quicksort"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25142"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25142\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/25150"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}