{"id":595,"date":"2025-01-20T22:27:47","date_gmt":"2025-01-20T22:27:47","guid":{"rendered":"https:\/\/funwithdev.com\/?page_id=595"},"modified":"2025-02-23T16:32:25","modified_gmt":"2025-02-23T16:32:25","slug":"selection-sort","status":"publish","type":"page","link":"https:\/\/funwithdev.com\/?page_id=595","title":{"rendered":"Selection Sort"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Selection Sort v1.0.0\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/O_43DMBBNpw?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><strong>Title: Understanding Selection Sort: A Simple Yet Effective Sorting Algorithm<\/strong><\/p>\n\n\n\n<p>Sorting algorithms are fundamental in computer science, and one of the simplest yet effective ones is the Selection Sort. In this article, we\u2019ll dive into what Selection Sort is, how it works, and its advantages and disadvantages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is Selection Sort?<\/h3>\n\n\n\n<p>Selection Sort is a comparison-based sorting algorithm. It works by repeatedly selecting the smallest (or largest, depending on the sorting order) element from the unsorted portion of the list and moving it to the sorted portion. This process continues until the entire list is sorted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How Does Selection Sort Work?<\/h3>\n\n\n\n<p>Here\u2019s a step-by-step breakdown of the Selection Sort algorithm:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialize<\/strong>: Start with the first element of the list.<\/li>\n\n\n\n<li><strong>Find the Minimum<\/strong>: Scan the entire list to find the smallest element.<\/li>\n\n\n\n<li><strong>Swap<\/strong>: Swap the smallest element with the first element.<\/li>\n\n\n\n<li><strong>Repeat<\/strong>: Move to the next element and repeat the process until the entire list is sorted.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Pseudocode<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function selectionSort(array)\n    n = length of array\n    for i = 0 to n-1\n        minIndex = i\n        for j = i+1 to n\n            if array[j] < array[minIndex]\n                minIndex = j\n        if minIndex != i\n            swap(array[i], array[minIndex])\n    return array<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Python<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>  def selection_sort(arr):\n    # Traverse through all array elements\n    for i in range(len(arr)):\n        #Find the minimum element in the remaining unsorted array\n        min_idx = i\n        for j in range(i + 1, len(arr)):\n            if arr[j] < arr[min_idx]:\n                min_idx = j\n\n        # Swap the found minimum element with the first element\n        arr[i], arr[min_idx] = arr[min_idx], arr[i]<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Test the selection sort function<\/h1>\n\n\n\n<p>arr = [64, 25, 12, 22, 11]<br>selection_sort(arr)<br>print(\u201cSorted array:\u201d, arr)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Let\u2019s consider an example to illustrate how Selection Sort works:<\/p>\n\n\n\n<p>Suppose we have the following list: <code>[64, 25, 12, 22, 11]<\/code><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>First Pass<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Find the smallest element: <code>11<\/code><\/li>\n\n\n\n<li>Swap <code>11<\/code> with <code>64<\/code><\/li>\n\n\n\n<li>List after first pass: <code>[11, 25, 12, 22, 64]<\/code><\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Second Pass<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Find the smallest element in the remaining list: <code>12<\/code><\/li>\n\n\n\n<li>Swap <code>12<\/code> with <code>25<\/code><\/li>\n\n\n\n<li>List after second pass: <code>[11, 12, 25, 22, 64]<\/code><\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Third Pass<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Find the smallest element in the remaining list: <code>22<\/code><\/li>\n\n\n\n<li>Swap <code>22<\/code> with <code>25<\/code><\/li>\n\n\n\n<li>List after third pass: <code>[11, 12, 22, 25, 64]<\/code><\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Fourth Pass<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The remaining elements are already in order.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Advantages of Selection Sort<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Simplicity<\/strong>: Easy to understand and implement.<\/li>\n\n\n\n<li><strong>In-Place Sorting<\/strong>: Does not require additional memory for sorting.<\/li>\n\n\n\n<li><strong>Performance<\/strong>: Performs well on small lists.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Disadvantages of Selection Sort<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Inefficiency<\/strong>: Not suitable for large lists due to its O(n^2) time complexity.<\/li>\n\n\n\n<li><strong>Stability<\/strong>: Not a stable sort, meaning it does not preserve the relative order of equal elements.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>Selection Sort is a straightforward and easy-to-implement sorting algorithm. While it may not be the most efficient for large datasets, its simplicity makes it a great choice for educational purposes and small lists. Understanding Selection Sort provides a solid foundation for learning more complex sorting algorithms.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Title: Understanding Selection Sort: A Simple Yet Effective Sorting Algorithm Sorting algorithms are fundamental in computer science, and one of the simplest yet effective ones is the Selection Sort. In this article, we\u2019ll dive into what Selection Sort is, how it works, and its advantages and disadvantages. What is Selection Sort? Selection Sort is a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-595","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages\/595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/funwithdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=595"}],"version-history":[{"count":9,"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages\/595\/revisions"}],"predecessor-version":[{"id":632,"href":"https:\/\/funwithdev.com\/index.php?rest_route=\/wp\/v2\/pages\/595\/revisions\/632"}],"wp:attachment":[{"href":"https:\/\/funwithdev.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}