{"id":542,"date":"2023-03-23T07:34:25","date_gmt":"2023-03-23T07:34:25","guid":{"rendered":"http:\/\/learnlearn.uk\/ibcs\/?page_id=542"},"modified":"2025-04-05T11:01:01","modified_gmt":"2025-04-05T11:01:01","slug":"selection-sort","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/","title":{"rendered":"Selection Sort"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Introduction<\/h2>\n<div class=\"tabcontent\">\n\n<h2>Introduction to Selection Sort<\/h2>\n<p>Selection Sort is a comparison-based sorting algorithm. It operates by dividing the input list into a\u00a0<b>sorted and an unsorted region<\/b>, repeatedly selecting the smallest (or largest) element from the unsorted region and moving it to the end of the sorted region.<\/p>\n<p>The algorithm operates in-place and works by repeatedly selecting the minimum element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire list is sorted.<\/p>\n\n<\/div><h2 class=\"tabtitle\">Selection Sort Demonstration<\/h2>\n<div class=\"tabcontent\">\n\n<h2>Selection Sort Demonstration<\/h2>\n<p><iframe loading=\"lazy\" src=\"https:\/\/revise.learnlearn.uk\/static\/static_pages\/selection_sort.html\" width=\"100%\" height=\"600\" data-mce-fragment=\"1\"><\/iframe>\n<\/div><h2 class=\"tabtitle\">Time Complexity<\/h2>\n<div class=\"tabcontent\">\n<\/p>\n<h2>Time Complexity<\/h2>\n<p>The time complexity of selection sort is O(n\u00b2) in all cases\u2014best, average, and worst. This is because selection sort always performs a linear scan to find the minimum element for each position in the array.<\/p>\n<p>Despite its simplicity, the quadratic time complexity makes selection sort inefficient for large datasets compared to more advanced sorting algorithms such as quicksort and mergesort.<\/p>\n\n<\/div><h2 class=\"tabtitle\">Space Complexity<\/h2>\n<div class=\"tabcontent\">\n\n<h2>Space Complexity<\/h2>\n<p class=\"\">Selection Sort is an in-place sorting algorithm, which means it requires a minimal amount of extra storage. The algorithm operates by selecting the minimum element and rearranging the elements within the same array.<\/p>\n<p class=\"\">Therefore the space complexity of\u00a0<b>Selection Sort is O(1)<\/b>, indicating that it uses a constant amount of additional memory regardless of the input size.<\/p>\n\n<\/div><h2 class=\"tabtitle\">Python Example<\/h2>\n<div class=\"tabcontent\">\n\n<h2>Python Example<\/h2>\n<div>\n<pre>def selection_sort(arr):\r\n    n = len(arr)\r\n    for i in range(n - 1):\r\n        min_index = i\r\n        for j in range(i + 1, n):\r\n            if arr[j] &lt; arr[min_index]:\r\n                min_index = j\r\n        arr[i], arr[min_index] = arr[min_index], arr[i]  # Swap the elements\r\n\r\n# Example usage\r\narr = [64, 25, 12, 22, 11]\r\nselection_sort(arr)\r\nprint(\"Sorted array:\", arr)\r\n<\/pre>\n<\/div>\n\n<\/div><h2 class=\"tabtitle\">Pros and Cons<\/h2>\n<div class=\"tabcontent\">\n\n<h2>Pros and Cons of Selection Sort<\/h2>\n<h3 class=\"\">Advantages<\/h3>\n<p>Selection Sort is easy to understand and implement, making it a great choice for teaching sorting concepts. It has a constant space complexity, requiring only a small amount of additional memory.<\/p>\n<h3 class=\"\">Disadvantages<\/h3>\n<p class=\"\">The performance of Selection Sort is subpar for large datasets, with a time complexity of O(n\u00b2). Additionally, it is unstable, meaning that it does not preserve the relative order of equal elements.<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to Selection Sort Selection Sort is a comparison-based sorting algorithm. It operates by dividing the input list into a\u00a0sorted and an unsorted region, repeatedly selecting the smallest (or largest) element from the unsorted region and moving it to the end of the sorted region. The algorithm operates in-place and works by repeatedly selecting the&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Selection Sort<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"off","neve_meta_content_width":70,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Selection Sort - IB Computer Science<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Selection Sort - IB Computer Science\" \/>\n<meta property=\"og:description\" content=\"Introduction to Selection Sort Selection Sort is a comparison-based sorting algorithm. It operates by dividing the input list into a\u00a0sorted and an unsorted region, repeatedly selecting the smallest (or largest) element from the unsorted region and moving it to the end of the sorted region. The algorithm operates in-place and works by repeatedly selecting the&hellip;&nbsp;Read More &raquo;Selection Sort\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/\" \/>\n<meta property=\"og:site_name\" content=\"IB Computer Science\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-05T11:01:01+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/\",\"name\":\"Selection Sort - IB Computer Science\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#website\"},\"datePublished\":\"2023-03-23T07:34:25+00:00\",\"dateModified\":\"2025-04-05T11:01:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"IB Computer Science\",\"item\":\"https:\/\/learnlearn.uk\/ibcs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Selection Sort\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#website\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/\",\"name\":\"IB Computer Science\",\"description\":\"- learnlearn..uk\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/ibcs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#organization\",\"name\":\"IB Computer Science\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png\",\"width\":300,\"height\":41,\"caption\":\"IB Computer Science\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Selection Sort - IB Computer Science","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/","og_locale":"en_GB","og_type":"article","og_title":"Selection Sort - IB Computer Science","og_description":"Introduction to Selection Sort Selection Sort is a comparison-based sorting algorithm. It operates by dividing the input list into a\u00a0sorted and an unsorted region, repeatedly selecting the smallest (or largest) element from the unsorted region and moving it to the end of the sorted region. The algorithm operates in-place and works by repeatedly selecting the&hellip;&nbsp;Read More &raquo;Selection Sort","og_url":"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/","og_site_name":"IB Computer Science","article_modified_time":"2025-04-05T11:01:01+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/","url":"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/","name":"Selection Sort - IB Computer Science","isPartOf":{"@id":"https:\/\/learnlearn.uk\/ibcs\/#website"},"datePublished":"2023-03-23T07:34:25+00:00","dateModified":"2025-04-05T11:01:01+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/ibcs\/selection-sort\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/ibcs\/selection-sort\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"IB Computer Science","item":"https:\/\/learnlearn.uk\/ibcs\/"},{"@type":"ListItem","position":2,"name":"Selection Sort"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/ibcs\/#website","url":"https:\/\/learnlearn.uk\/ibcs\/","name":"IB Computer Science","description":"- learnlearn..uk","publisher":{"@id":"https:\/\/learnlearn.uk\/ibcs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/ibcs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/ibcs\/#organization","name":"IB Computer Science","url":"https:\/\/learnlearn.uk\/ibcs\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png","contentUrl":"https:\/\/learnlearn.uk\/ibcs\/wp-content\/uploads\/sites\/25\/2022\/09\/LearnLearnLogowhite-300x41.png","width":300,"height":41,"caption":"IB Computer Science"},"image":{"@id":"https:\/\/learnlearn.uk\/ibcs\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/ibcs\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"Introduction to Selection Sort Selection Sort is a comparison-based sorting algorithm. It operates by dividing the input list into a\u00a0sorted and an unsorted region, repeatedly selecting the smallest (or largest) element from the unsorted region and moving it to the end of the sorted region. The algorithm operates in-place and works by repeatedly selecting the&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages\/542"}],"collection":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/comments?post=542"}],"version-history":[{"count":3,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages\/542\/revisions"}],"predecessor-version":[{"id":1197,"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/pages\/542\/revisions\/1197"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/ibcs\/wp-json\/wp\/v2\/media?parent=542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}