{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/selectionSort","result":{"data":{"blog":{"frontmatter":{"title":"SELECTION SORT","thumbnail":"blog59","date":"April 26, 2021","dsaCppCodeFile":"https://drive.google.com/file/d/1M8yNCsm99h0en0-b5FSo8hYayMNbZpVE/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Selection sort is an in-place sorting algorithm. In every\n                  iteration, we find the minimum value and swap it with the\n                  current index. That is to say that we start looping with index\n                  i and loop again from i+1 till the end, find the min value and\n                  swap it with the element at index i.\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>Assign min = i.</li>\n                <li>Loop the array from j = 0 to n-i-1(Inner loop).</li>\n                <li>Assign min = j if a[j] &lt; a[min].</li>\n                <li>\n                  After the inner loop ends, if min != i then swap a[i] &amp;\n                  a[min]. (It means that if we found the index of minimum value\n                  among the elements i+1 to n-1 then swap it with the current\n                  element i.e i).\n                </li>\n                <li>\n                  After the outer 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 5 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: 75%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABJ0AAASdAHeZh94AAABu0lEQVQ4y4WTi5KCMAxF+/8/qI4LiAIjD3nqgiJk743AoLLamdICyelNmpi+7+XbuFwukueFrr+YTdMs2pFl+KjrGg65GrZtK7fbTef1epWu6yQ8HmW92Yiz24nrupJlmQLu97u0d9gPPhMwiROxbVt831dwkiS6nk4ndea+qiopy1K/bQh3HPmxLD0sjSJJ01TO57MYnkQjnux5nir6NFooCcNQchwUAVQDglDkin0BqCH5BEWE+UGgIY/5WJqLA9AGnAIRGHuQvd/vAfU/AqfviIJvuiIdSL40UF4UhZgAqhjyDgl/BQ6bx3zd0w6wHuFyNLhYBR6hjoknLHgJeRH0qCOFyaBUgVCpQMd2Zgq9d+AcNKhS4Kz2noDb7Vav3HX3b0DNEyfqrUfZ9HDo8b9/yfETkOGyvgjTkOE8AVmsIwgO0wELlzUB+UIgC9VHHjsmGQlmKWhowwHfBjuFHDMvZI9tBaUROiZDXqM4lgw5i1DIzDMLmhGNazR0SAw7dhe/mVE2wQw7zXLxfE8NbdtSI7YlW8+2sJ4rsbBm+O84O4BiORwOCmctm48dMAyqW63W6kB4WVb/2v4BQlqJ2ohxmm0AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Selection Sort\" title=\"Selection Sort\" src=\"/static/dd35e736f554bb3f587c749be1358701/fcda8/graph.png\" srcset=\"/static/dd35e736f554bb3f587c749be1358701/12f09/graph.png 148w,\n/static/dd35e736f554bb3f587c749be1358701/e4a3f/graph.png 295w,\n/static/dd35e736f554bb3f587c749be1358701/fcda8/graph.png 590w,\n/static/dd35e736f554bb3f587c749be1358701/efc66/graph.png 885w,\n/static/dd35e736f554bb3f587c749be1358701/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/bubbleSort\">Bubble 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                  Selection sort is an in-place sorting algorithm. In every\n                  iteration, we find the minimum value and swap it with the\n                  current index. That is to say that we start looping with index\n                  i and loop again from i+1 till the end, find the min value and\n                  swap it with the element at index i.\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>Assign min = i.</li>\n                <li>Loop the array from j = 0 to n-i-1(Inner loop).</li>\n                <li>Assign min = j if a[j] &lt; a[min].</li>\n                <li>\n                  After the inner loop ends, if min != i then swap a[i] &amp;\n                  a[min]. (It means that if we found the index of minimum value\n                  among the elements i+1 to n-1 then swap it with the current\n                  element i.e i).\n                </li>\n                <li>\n                  After the outer 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 5 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: 75%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABJ0AAASdAHeZh94AAABu0lEQVQ4y4WTi5KCMAxF+/8/qI4LiAIjD3nqgiJk743AoLLamdICyelNmpi+7+XbuFwukueFrr+YTdMs2pFl+KjrGg65GrZtK7fbTef1epWu6yQ8HmW92Yiz24nrupJlmQLu97u0d9gPPhMwiROxbVt831dwkiS6nk4ndea+qiopy1K/bQh3HPmxLD0sjSJJ01TO57MYnkQjnux5nir6NFooCcNQchwUAVQDglDkin0BqCH5BEWE+UGgIY/5WJqLA9AGnAIRGHuQvd/vAfU/AqfviIJvuiIdSL40UF4UhZgAqhjyDgl/BQ6bx3zd0w6wHuFyNLhYBR6hjoknLHgJeRH0qCOFyaBUgVCpQMd2Zgq9d+AcNKhS4Kz2noDb7Vav3HX3b0DNEyfqrUfZ9HDo8b9/yfETkOGyvgjTkOE8AVmsIwgO0wELlzUB+UIgC9VHHjsmGQlmKWhowwHfBjuFHDMvZI9tBaUROiZDXqM4lgw5i1DIzDMLmhGNazR0SAw7dhe/mVE2wQw7zXLxfE8NbdtSI7YlW8+2sJ4rsbBm+O84O4BiORwOCmctm48dMAyqW63W6kB4WVb/2v4BQlqJ2ohxmm0AAAAASUVORK5CYII=&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Selection Sort\" title=\"Selection Sort\" src=\"/static/dd35e736f554bb3f587c749be1358701/fcda8/graph.png\" srcset=\"/static/dd35e736f554bb3f587c749be1358701/12f09/graph.png 148w,\n/static/dd35e736f554bb3f587c749be1358701/e4a3f/graph.png 295w,\n/static/dd35e736f554bb3f587c749be1358701/fcda8/graph.png 590w,\n/static/dd35e736f554bb3f587c749be1358701/efc66/graph.png 885w,\n/static/dd35e736f554bb3f587c749be1358701/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/bubbleSort\">Bubble 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='M41%2095c-5%200-5%200-5%204%200%207-1%208-4%205l-3-4h1c2%203%205%202%205-1%200-4-8-6-10-2-3%203-2%206%201%208%204%204%204%205%200%205h-3c-2%204%208%205%2010%202l2-2v2c0%202%200%202%207%202%206%200%206%200%206-2%200-3%200-3-3-3-5%200-6-2-2-3%205%200%206-4%201-4-4%200-4-2%201-2%203-1%204-2%204-4l-2-2-6%201m10%200l-1%2010-1%209h13c12%200%2013-1%2013-3s0-2-4-2c-5%200-5-2-1-3%202%200%203-1%203-2%200-2-1-2-3-2-4%200-4-2%201-2%203-1%204-2%204-4s0-2-3-2l-7%201h-3v13h-7v-7c0-5-1-7-2-7l-2%201m30%200c-5%202-7%2014-3%2017%203%203%2011%201%2011-4%200-2-2-2-4%200l-2%202-1-2c-1-2%200-8%202-9l1%202c-1%202-1%202%201%202l4-1c0-1%201-2%203-2s2%200%202%207c-1%207%200%207%202%207s3-4%203-12c0-2%200-3%202-3%201%200%202-1%202-3s0-2-2-2l-7%201c-4%200-5%200-5%202h-1c-2-2-5-3-8-2m25%200l-1%2010v9h2c3%200%203%200%203-3%200-2%200-2%201%200%203%205%209%204%2012-2%204-11-6-21-11-10l-2%204v-4c0-4%200-5-2-5l-2%201m57%201l-3%207v4l-4-3-2-4%201%201c2%202%205%201%205-2s-2-4-6-4c-6%200-8%208-3%2011%204%203%204%204%200%204-4-1-5%202-1%203%204%202%208%200%209-2l1-2%201%202c1%204%208%204%2011-1%205-8-2-21-9-14m31-1c-5%200-5%200-5%203l2%202c2%200%202%201%202%207%200%207%200%207%203%207%202%200%202%200%202-4%200-7%201-11%203-11s2-1%202-2c-1-3-1-3-9-2m-69%205l-1%209c0%205%200%205%202%205s3-1%204-4v-4l2%204c1%203%202%204%204%204s2%200%202-6l1-10c0-3%200-3-2-3-3%200-3%200-3%203v4l-2-4c-1-2-2-3-4-3-3%200-3%200-3%205m51-4l-1%2010-1%208h2c3%200%203%200%203-3l1-3%202%203c1%203%201%203%204%202%202-1%202-1%201-4-2-2-2-3%200-4v-9h-11m123%2010l-1%208v7h9c8%200%209%201%209%203s0%202-2%202c-5-1-16%2013-21%2026l-3%2020%206-7c4-5%206-7%2010-8%206-3%206-2%206%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%205-17-1-20l-3-2-1-1-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%207v7h22v-3l1-3v-1l-1-3v-3h-22v6m-31%2028l1%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/4274da89267c145e45099abde01aca8e/ee604/blog59.png","srcSet":"/static/4274da89267c145e45099abde01aca8e/69585/blog59.png 200w,\n/static/4274da89267c145e45099abde01aca8e/497c6/blog59.png 400w,\n/static/4274da89267c145e45099abde01aca8e/ee604/blog59.png 800w,\n/static/4274da89267c145e45099abde01aca8e/f3583/blog59.png 1200w,\n/static/4274da89267c145e45099abde01aca8e/e4d72/blog59.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"selectionSort","thumbnail":"thumbnails/blog59.png"}},"staticQueryHashes":["2987289216","63159454"]}