{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/quickSort","result":{"data":{"blog":{"frontmatter":{"title":"QUICK SORT","thumbnail":"blog62","date":"May 4, 2021","dsaCppCodeFile":"https://drive.google.com/file/d/19Q37GfaFmbTMGnxOXyYpgyLGBFCXAMs5/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Quick Sort divides the array(or given data structure) into two\n                  partition recursively and does in place sorting while forming\n                  partition. It doesn&apos;t require any extra storage as it does in\n                  place sorting.\n                </p>\n                <p>\n                  For every partition, a pivot is selected such that this pivot\n                  in bought to a point(somewhere in the middle) such that the\n                  elements on the left side of this pivot are lesser than it and\n                  the elements on the right side of the pivot are greater than\n                  it.\n                </p>\n                <p>\n                  This algorithm falls under Divide &amp; Conquer Technique which is\n                  divides the problem, finds the solution to smaller problem and\n                  then merges these to get the solution to the original problem.\n                  Let&apos;s look at the working of this algorithm.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Working Procedure</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following theory.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  Consider an array,arr of n elements. Let start be the starting\n                  index and end be the ending index.\n                </li>\n                <li>\n                  <strong>Quick Sort: </strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Recursion calls will be made if there exist more than one\n                      element in the array.\n                    </li>\n                    <li>\n                      Find the partition p, using\n                      <strong>partition</strong> function.\n                    </li>\n                    <li>Make the first recursive call from start to p-1.</li>\n                    <li>Make the second recursive call from p+1 to end.</li>\n                  </ol>\n                </li>\n                <li>\n                  <strong>Partition Function:</strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Let the last element be the pivot, pivot=arr[end](You can\n                      select any element as your pivot) and pIndex=start(you\n                      will understand the need of this variable at the last\n                      step).\n                    </li>\n                    <li>Loop from start to end-1.</li>\n                    <li>\n                      If arr[i] &lt; pivot then swap arr[pIndex] &amp; arr[i] and\n                      increment pIndex. This step essentially makes all the\n                      elements on left side of pIndex to be lesser than pivot\n                      and right side of pIndex to be greater than the pivot.\n                    </li>\n                    <li>\n                      After the loop, swap arr[pIndex] and arr[end]. This brings\n                      our pivot(which was selected as the last element at the\n                      point where the partition is to be made).\n                    </li>\n                    <li>\n                      Return pIndex(this will be the index where the partition\n                      has to be made for further recursive calls).\n                    </li>\n                  </ol>\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Time Complexity</h4>\n              <div class=\"m-2\">\n                <p class=\"text-muted\">\n                  <strong>PS: We give random inputs because the no. of input will\n                    generally be 100 or 1000 to find the time complexity and\n                    manually giving so many inputs is cumbersome work.</strong>\n                </p>\n                <p>\n                  There exists two recursive calls in which the elements gets\n                  partitioned in each call. The basic operation in this\n                  algorithm is the comparison between at step 3.3 in the above\n                  working procedure. So increment the count variable above this\n                  if condition to get the count of no. of times the basic\n                  operation has been performed.\n                </p>\n                <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \">\n      <a class=\"gatsby-resp-image-link\" href=\"/static/163b2d062286a85637811d834b4885b8/2c5fd/graph.png\" style=\"display: block\" target=\"_blank\" rel=\"noopener\">\n    <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 74.32432432432432%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABJ0AAASdAHeZh94AAABwUlEQVQ4y41Ti66CMBTb//+gMYI8FFREFAXEF73tZAQTvXqSw2AbZ21PZ7quw7e43W7Y7XYoyxLH4/HfvUYPbcrzHJfLBW3bvqTmTqcTwjBEslxiu93ifr8/kwddr1e0+o/j4/GAEcKCpy8WC2RZhv1+P2RRFAOysjzawr7vYzKZwPN8TKdTbNZrHAgmSxJUXLcI9bMKFsX+K33t3Ww2z8P4fmER0kObpqjI1IiqKAud0CiE+lO+BGnjcAC5oj6fLQMTURtRns1mVp+3Bcdz7p0gOkrRsZiirioLzMRxjKZpbFMOOm30MxyiMTI2gLwBIrJL/fSZ33Vdw6wp6s4i9JCNEbpCrpiQSC/ZpkeFkQwqZhF6noemboYOvyDEcDzY5ic6F/262yeEFWkbaSdLLOkx0XY0bNJnVisiG+be6PqCULrJP3EUI3dd5mlWcCVRdT90XgVPzof6WMmg0pCwLcUfruQ4dKtsUx69wOp0IguRdt7fkHybD9pqlJllLaXW3Zq+ZXaxNQ5yS1RBECCh46MoQhxHSPmuqyYnaC7h9ZLNUjsuMJ/7WK1W1sNa014z7tSnSJIUYRAimM9ZOLYd/RR/sHCKizHlTqwAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Quick Sort\" title=\"Quick Sort\" src=\"/static/163b2d062286a85637811d834b4885b8/fcda8/graph.png\" srcset=\"/static/163b2d062286a85637811d834b4885b8/12f09/graph.png 148w,\n/static/163b2d062286a85637811d834b4885b8/e4a3f/graph.png 295w,\n/static/163b2d062286a85637811d834b4885b8/fcda8/graph.png 590w,\n/static/163b2d062286a85637811d834b4885b8/efc66/graph.png 885w,\n/static/163b2d062286a85637811d834b4885b8/2c5fd/graph.png 1006w\" sizes=\"(max-width: 590px) 100vw, 590px\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\" loading=\"lazy\">\n  </a>\n    </span>\n                <p class=\"lead\">\n                  <strong>Time Complexity: &#x3B8;(nlog(n))</strong>\n                </p>\n              </div>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Quick Sort divides the array(or given data structure) into two\n                  partition recursively and does in place sorting while forming\n                  partition. It doesn&apos;t require any extra storage as it does in\n                  place sorting.\n                </p>\n                <p>\n                  For every partition, a pivot is selected such that this pivot\n                  in bought to a point(somewhere in the middle) such that the\n                  elements on the left side of this pivot are lesser than it and\n                  the elements on the right side of the pivot are greater than\n                  it.\n                </p>\n                <p>\n                  This algorithm falls under Divide &amp; Conquer Technique which is\n                  divides the problem, finds the solution to smaller problem and\n                  then merges these to get the solution to the original problem.\n                  Let&apos;s look at the working of this algorithm.\n                </p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Working Procedure</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following theory.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  Consider an array,arr of n elements. Let start be the starting\n                  index and end be the ending index.\n                </li>\n                <li>\n                  <strong>Quick Sort: </strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Recursion calls will be made if there exist more than one\n                      element in the array.\n                    </li>\n                    <li>\n                      Find the partition p, using\n                      <strong>partition</strong> function.\n                    </li>\n                    <li>Make the first recursive call from start to p-1.</li>\n                    <li>Make the second recursive call from p+1 to end.</li>\n                  </ol>\n                </li>\n                <li>\n                  <strong>Partition Function:</strong>\n                  <ol class=\"pl-4\">\n                    <li>\n                      Let the last element be the pivot, pivot=arr[end](You can\n                      select any element as your pivot) and pIndex=start(you\n                      will understand the need of this variable at the last\n                      step).\n                    </li>\n                    <li>Loop from start to end-1.</li>\n                    <li>\n                      If arr[i] &lt; pivot then swap arr[pIndex] &amp; arr[i] and\n                      increment pIndex. This step essentially makes all the\n                      elements on left side of pIndex to be lesser than pivot\n                      and right side of pIndex to be greater than the pivot.\n                    </li>\n                    <li>\n                      After the loop, swap arr[pIndex] and arr[end]. This brings\n                      our pivot(which was selected as the last element at the\n                      point where the partition is to be made).\n                    </li>\n                    <li>\n                      Return pIndex(this will be the index where the partition\n                      has to be made for further recursive calls).\n                    </li>\n                  </ol>\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Time Complexity</h4>\n              <div class=\"m-2\">\n                <p class=\"text-muted\">\n                  <strong>PS: We give random inputs because the no. of input will\n                    generally be 100 or 1000 to find the time complexity and\n                    manually giving so many inputs is cumbersome work.</strong>\n                </p>\n                <p>\n                  There exists two recursive calls in which the elements gets\n                  partitioned in each call. The basic operation in this\n                  algorithm is the comparison between at step 3.3 in the above\n                  working procedure. So increment the count variable above this\n                  if condition to get the count of no. of times the basic\n                  operation has been performed.\n                </p>\n                <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \">\n      <a class=\"gatsby-resp-image-link\" href=\"/static/163b2d062286a85637811d834b4885b8/2c5fd/graph.png\" style=\"display: block\" target=\"_blank\" rel=\"noopener\">\n    <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 74.32432432432432%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABJ0AAASdAHeZh94AAABwUlEQVQ4y41Ti66CMBTb//+gMYI8FFREFAXEF73tZAQTvXqSw2AbZ21PZ7quw7e43W7Y7XYoyxLH4/HfvUYPbcrzHJfLBW3bvqTmTqcTwjBEslxiu93ifr8/kwddr1e0+o/j4/GAEcKCpy8WC2RZhv1+P2RRFAOysjzawr7vYzKZwPN8TKdTbNZrHAgmSxJUXLcI9bMKFsX+K33t3Ww2z8P4fmER0kObpqjI1IiqKAud0CiE+lO+BGnjcAC5oj6fLQMTURtRns1mVp+3Bcdz7p0gOkrRsZiirioLzMRxjKZpbFMOOm30MxyiMTI2gLwBIrJL/fSZ33Vdw6wp6s4i9JCNEbpCrpiQSC/ZpkeFkQwqZhF6noemboYOvyDEcDzY5ic6F/262yeEFWkbaSdLLOkx0XY0bNJnVisiG+be6PqCULrJP3EUI3dd5mlWcCVRdT90XgVPzof6WMmg0pCwLcUfruQ4dKtsUx69wOp0IguRdt7fkHybD9pqlJllLaXW3Zq+ZXaxNQ5yS1RBECCh46MoQhxHSPmuqyYnaC7h9ZLNUjsuMJ/7WK1W1sNa014z7tSnSJIUYRAimM9ZOLYd/RR/sHCKizHlTqwAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Quick Sort\" title=\"Quick Sort\" src=\"/static/163b2d062286a85637811d834b4885b8/fcda8/graph.png\" srcset=\"/static/163b2d062286a85637811d834b4885b8/12f09/graph.png 148w,\n/static/163b2d062286a85637811d834b4885b8/e4a3f/graph.png 295w,\n/static/163b2d062286a85637811d834b4885b8/fcda8/graph.png 590w,\n/static/163b2d062286a85637811d834b4885b8/efc66/graph.png 885w,\n/static/163b2d062286a85637811d834b4885b8/2c5fd/graph.png 1006w\" sizes=\"(max-width: 590px) 100vw, 590px\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\" loading=\"lazy\">\n  </a>\n    </span>\n                <p class=\"lead\">\n                  <strong>Time Complexity: &#x3B8;(nlog(n))</strong>\n                </p>\n              </div>\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M31%2095c-3%201-5%205-5%2010%200%207%202%209%208%209%205-1%205-1%206-8%200-8-3-13-9-11m25%201l-1%2010v8h2c3%200%203%200%203-3v-3l2%203c1%202%203%203%205%203%203%200%207-3%207-6%200-2-2-2-4%200l-2%202-1-2c-1-2%200-8%202-9l1%202c-1%202-1%202%201%202%205%200%205-6%200-8-3-1-6%200-8%203l-2%204v-4c0-4%200-4-2-4s-3%201-3%202m21-1l-2%2010v9h2c3%200%203%200%203-3%200-4%201-4%203%200%202%203%202%203%204%202s2-1%200-6l-2-4%202-3c3-3%203-3%201-4-2-2-2-2-4%201l-2%202-1-2c-1-2-3-4-4-2m36%202l-3%206v4l-3-3-3-4h1c2%203%205%202%205-1s-2-4-6-4c-6%200-8%208-3%2011%204%202%204%204%200%204-3-1-5%201-2%203%204%202%208%201%2010-2%201-2%201-2%202%200%204%207%2012%202%2012-7%201-9-5-13-10-7m16-2c-4%200-4%200-4%2011l-1%208h2c3%200%203%200%203-3l1-3%202%203c1%203%202%204%204%202s2-2%201-4c-2-2-2-3%200-4%204-6%200-11-8-10m15%200c-5%200-5%200-5%203l2%202c2%200%202%201%202%207%200%207%200%207%202%207%203%200%203%200%204-7%200-7%200-8%202-8s2-1%202-2c-1-3-1-3-9-2M42%2097v16c2%202%208%201%2010-1l2-6%201-8c0-3%200-3-2-3-3%200-3%200-4%207-1%209-3%2010-3%200%200-7%200-7-2-7l-2%202m257%209l-1%208v7h9c8%200%209%201%209%203%200%201%200%202-2%202l-2-1c0-2-8%207-13%2015-4%208-8%2017-8%2026-1%207-1%207%205-1%204-5%206-7%209-8%206-3%207-2%207%201%200%202%200%202%201%201h3l-2-5-2-7-1-3v-3c3-2%2013-2%2013%200l-1%201h-2c0%202%203%202%204%201h1l-1%202-1%203-2%207-2%204h3c2%200%202-1%201-3%200-2%200-2%207%200%204%201%205%202%207%205l3%204%201%203%202%206%202%204%201%204c2%207-1%2010-9%208h-4v-24h-15v-3l-1-3-1%203v3h-17v7l-1%208c-2%202-2%203-1%203%202%200%203%204%202%206h-15c-2-3-2-5%201-8%204-6%202-7-2-2-3%203-4%205-4%207%200%203%200%203-9%204-9%200-9%200-9%202s1%203%205%203h5l-1%204v3h97l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2c-1-2-1-2-1%200%200%203-8%203-8-1%200-2%200-2%202-2%202%201%203%200%201-1-2-2-1-4%203-12%206-11%206-14%200-20l-5-3-1-4-1-5c-1-1-1%201-1%204v5l-3-1c-3-1-22%200-24%201-1%201-1-1-1-4v-5l-14-1-13%201m2%208v6h22v-3l1-3v-1l-1-3v-3h-22v7m-31%2027l1%209h12v-18h-13v9m3%2023l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%203-2%202-2-2%200-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/b6131b11cc644aea075588b78308d77e/ee604/blog62.png","srcSet":"/static/b6131b11cc644aea075588b78308d77e/69585/blog62.png 200w,\n/static/b6131b11cc644aea075588b78308d77e/497c6/blog62.png 400w,\n/static/b6131b11cc644aea075588b78308d77e/ee604/blog62.png 800w,\n/static/b6131b11cc644aea075588b78308d77e/f3583/blog62.png 1200w,\n/static/b6131b11cc644aea075588b78308d77e/e4d72/blog62.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"quickSort","thumbnail":"thumbnails/blog62.png"}},"staticQueryHashes":["2987289216","63159454"]}