{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/bubbleSort","result":{"data":{"blog":{"frontmatter":{"title":"BUBBLE SORT","thumbnail":"blog58","date":"April 23, 2021","dsaCppCodeFile":"https://drive.google.com/file/d/1hIvs-2t-aHRsOvYolf0OWe-UwqPmTtMo/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Bubble Sort is the simplest sorting algorithm that works by\n                  repeatedly swapping the adjacent elements if they are in wrong\n                  order. In every iteration, the largest element bubbles up\n                  towards the end and hence the name bubble sort.\n                </p>\n                <p>\n                  This algorithm falls under Brute Force Technique which is a\n                  straight forward approach to solve a problem. Let&apos;s look at\n                  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>Consider an array of n elements.</li>\n                <li>Loop the array from i = 0 to n-2(Outer loop).</li>\n                <li>Loop the array from j = 0 to n-i-1(Inner loop).</li>\n                <li>\n                  If a[j] &gt; a[j+1] then swap these two elements.(It means that\n                  in every iteration, if the current element is greater than the\n                  next element then we swap them. So the greatest element in the\n                  array gets bubbled towards the end.)\n                </li>\n                <li>\n                  After both the loop ends, we get the required sorted array.\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                  The basic operation in this algorithm is the comparision\n                  between current element and the next element(step 4 in the\n                  above working procedure). So increment the count variable\n                  above this if condition to get the count of no. of times the\n                  basic 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      <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,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABJ0AAASdAHeZh94AAABwUlEQVQ4y4VTi3KCMBDk/z+wnVHwwUsBFZVaETXb3ROoD2xvJiQkl73dy53nnMN/dqprrFYr7Pd7VNUe5/P5ra+nz+FwwGazwfF4NOfT6YSmaWy+XC4oyxK+7yMMI8RxbPsicr1eb/4c8teeBxLMswyz2QxJkmC73RobgazXawukdVVVxnBVFPj4+MRkMsFoPEa+XGKjQV/5GMOCTmEYWnRF/cvERIEURPcaqoIUcb1lcE8MdBCTXZqmfX5Ef2gMGpnVVLajAi/wA2TLDFEUmeS/APt9qtCfY34dmZI2ao7dbgdvsViYhPl8/gLYLm6jW/9qBxMO1/rXrIQeUPlIBiT3dg8q+/oCbwMtUyst5tEAp9Ppe4bPQC0rfH/fxXKPDP2xz9Io7ZVfcqhZg3uOCXe8YHl7yvEDoOpOizhuJfOCAUqOCrgD0roLMPBYPaB+BCrJKRleeWCSlCd2ENoA/1nTvfJ9IUfTGUqyzDlK1meujmGwPM/t4TJ2lGb9q367uShy6xSdWevdassZy5JRQtakHipg/8ppNBpZ2/msWakJggnWDCZ/gSr/Wba0WvZeSmTA1NtjgidJijl7Xj37zn4ADGyKE7oPxqcAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Bubble Sort\" title=\"Bubble Sort\" src=\"/static/6cd3862fde1e3ac3b347d66bbd47f079/fcda8/graph.png\" srcset=\"/static/6cd3862fde1e3ac3b347d66bbd47f079/12f09/graph.png 148w,\n/static/6cd3862fde1e3ac3b347d66bbd47f079/e4a3f/graph.png 295w,\n/static/6cd3862fde1e3ac3b347d66bbd47f079/fcda8/graph.png 590w,\n/static/6cd3862fde1e3ac3b347d66bbd47f079/efc66/graph.png 885w,\n/static/6cd3862fde1e3ac3b347d66bbd47f079/54bf4/graph.png 1007w\" 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    </span>\n                <p class=\"lead\">\n                  <strong>Time Complexity: O(n<sup>2</sup>)</strong>\n                </p>\n              </div>\n              <div class=\"my-2 p-2\">\n                  <strong>Similar posts:</strong>\n                  <a href=\"/blogs/selectionSort\">Selection Sort</a>\n              </div>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Bubble Sort is the simplest sorting algorithm that works by\n                  repeatedly swapping the adjacent elements if they are in wrong\n                  order. In every iteration, the largest element bubbles up\n                  towards the end and hence the name bubble sort.\n                </p>\n                <p>\n                  This algorithm falls under Brute Force Technique which is a\n                  straight forward approach to solve a problem. Let&apos;s look at\n                  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>Consider an array of n elements.</li>\n                <li>Loop the array from i = 0 to n-2(Outer loop).</li>\n                <li>Loop the array from j = 0 to n-i-1(Inner loop).</li>\n                <li>\n                  If a[j] &gt; a[j+1] then swap these two elements.(It means that\n                  in every iteration, if the current element is greater than the\n                  next element then we swap them. So the greatest element in the\n                  array gets bubbled towards the end.)\n                </li>\n                <li>\n                  After both the loop ends, we get the required sorted array.\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                  The basic operation in this algorithm is the comparision\n                  between current element and the next element(step 4 in the\n                  above working procedure). So increment the count variable\n                  above this if condition to get the count of no. of times the\n                  basic 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      <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,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABJ0AAASdAHeZh94AAABwUlEQVQ4y4VTi3KCMBDk/z+wnVHwwUsBFZVaETXb3ROoD2xvJiQkl73dy53nnMN/dqprrFYr7Pd7VNUe5/P5ra+nz+FwwGazwfF4NOfT6YSmaWy+XC4oyxK+7yMMI8RxbPsicr1eb/4c8teeBxLMswyz2QxJkmC73RobgazXawukdVVVxnBVFPj4+MRkMsFoPEa+XGKjQV/5GMOCTmEYWnRF/cvERIEURPcaqoIUcb1lcE8MdBCTXZqmfX5Ef2gMGpnVVLajAi/wA2TLDFEUmeS/APt9qtCfY34dmZI2ao7dbgdvsViYhPl8/gLYLm6jW/9qBxMO1/rXrIQeUPlIBiT3dg8q+/oCbwMtUyst5tEAp9Ppe4bPQC0rfH/fxXKPDP2xz9Io7ZVfcqhZg3uOCXe8YHl7yvEDoOpOizhuJfOCAUqOCrgD0roLMPBYPaB+BCrJKRleeWCSlCd2ENoA/1nTvfJ9IUfTGUqyzDlK1meujmGwPM/t4TJ2lGb9q367uShy6xSdWevdassZy5JRQtakHipg/8ppNBpZ2/msWakJggnWDCZ/gSr/Wba0WvZeSmTA1NtjgidJijl7Xj37zn4ADGyKE7oPxqcAAAAASUVORK5CYII=&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Bubble Sort\" title=\"Bubble Sort\" src=\"/static/6cd3862fde1e3ac3b347d66bbd47f079/fcda8/graph.png\" srcset=\"/static/6cd3862fde1e3ac3b347d66bbd47f079/12f09/graph.png 148w,\n/static/6cd3862fde1e3ac3b347d66bbd47f079/e4a3f/graph.png 295w,\n/static/6cd3862fde1e3ac3b347d66bbd47f079/fcda8/graph.png 590w,\n/static/6cd3862fde1e3ac3b347d66bbd47f079/efc66/graph.png 885w,\n/static/6cd3862fde1e3ac3b347d66bbd47f079/54bf4/graph.png 1007w\" 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    </span>\n                <p class=\"lead\">\n                  <strong>Time Complexity: O(n<sup>2</sup>)</strong>\n                </p>\n              </div>\n              <div class=\"my-2 p-2\">\n                  <strong>Similar posts:</strong>\n                  <a href=\"/blogs/selectionSort\">Selection Sort</a>\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='M85%20100l-1%2010v4h12c12%200%2013-1%2013-3s0-2-3-2c-5%200-6-2-2-3%205%200%206-4%201-4-4%200-4-2%201-2%203-1%204-2%204-4s0-2-3-2l-7%201c-3%200-3%200-3%203%200%208-1%2010-5%2010h-3v-7c0-6%200-7-2-7-1%200-2%201-2%206m49-3l-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%202-2%202-2%202%200%202%204%208%204%2011%200%206-10-2-23-9-14m31-2c-5%200-5%200-5%203l2%202c2%200%202%200%202%207s0%207%202%207c3%200%204-4%204-12%200-2%200-3%202-3s2-1%202-2c-1-3-1-3-9-2M28%2096l-2%209v9h5l6-1c2-1%202-3%202-8l1-4c1-4-1-6-7-6l-5%201m13%204c0%2012%200%2014%206%2014%205%200%207-4%207-12%200-6%200-7-2-7s-3%202-3%209c-1%208-3%206-3-2%200-7%200-7-2-7-3%200-3%200-3%205m16-4l-2%209v9h4c7%200%209-1%209-6l1-7c1-4-1-6-7-6l-5%201m14%202l-1%2010-1%206h5c7%200%2010-2%209-7v-5c2-4%200-7-6-7s-6%200-6%203m76-2l-1%2010-1%208h3c2%200%202%200%202-3%200-4%201-4%203%200%202%203%202%203%204%202s2-2%201-5c-2-1-2-2%200-3v-9h-11m152%2010l-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/3f31bb9c6ea6339844dcc4d5e70631d7/ee604/blog58.png","srcSet":"/static/3f31bb9c6ea6339844dcc4d5e70631d7/69585/blog58.png 200w,\n/static/3f31bb9c6ea6339844dcc4d5e70631d7/497c6/blog58.png 400w,\n/static/3f31bb9c6ea6339844dcc4d5e70631d7/ee604/blog58.png 800w,\n/static/3f31bb9c6ea6339844dcc4d5e70631d7/f3583/blog58.png 1200w,\n/static/3f31bb9c6ea6339844dcc4d5e70631d7/e4d72/blog58.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"bubbleSort","thumbnail":"thumbnails/blog58.png"}},"staticQueryHashes":["2987289216","63159454"]}