CINXE.COM
Sorting algorithms/Selection sort - Rosetta Code
<!DOCTYPE html> <html class="client-nojs vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-sticky-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-0 vector-feature-appearance-pinned-clientpref-1 vector-feature-night-mode-disabled skin-theme-clientpref-day vector-toc-available" lang="en" dir="ltr"> <head> <meta charset="UTF-8"> <title>Sorting algorithms/Selection sort - Rosetta Code</title> <script>(function(){var className="client-js vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-sticky-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-0 vector-feature-appearance-pinned-clientpref-1 vector-feature-night-mode-disabled skin-theme-clientpref-day vector-toc-available";var cookie=document.cookie.match(/(?:^|; )rosettacodewikimwclientpreferences=([^;]+)/);if(cookie){cookie[1].split('%2C').forEach(function(pref){className=className.replace(new RegExp('(^| )'+pref.replace(/-clientpref-\w+$|[^\w-]+/g,'')+'-clientpref-\\w+( |$)'),'$1'+pref+'$2');});}document.documentElement.className=className;}());RLCONF={"wgBreakFrames":false,"wgSeparatorTransformTable":["",""],"wgDigitTransformTable":["",""], "wgDefaultDateFormat":"dmy","wgMonthNames":["","January","February","March","April","May","June","July","August","September","October","November","December"],"wgRequestId":"8a0ad6fcf1ebfadee9f754a8","wgCanonicalNamespace":"","wgCanonicalSpecialPageName":false,"wgNamespaceNumber":0,"wgPageName":"Sorting_algorithms/Selection_sort","wgTitle":"Sorting algorithms/Selection sort","wgCurRevisionId":349291,"wgRevisionId":349291,"wgArticleId":1473,"wgIsArticle":true,"wgIsRedirect":false,"wgAction":"view","wgUserName":null,"wgUserGroups":["*"],"wgCategories":["Pages with syntax highlighting errors","Programming Tasks","Sorting Algorithms","Sorting","11l","360 Assembly","AArch64 Assembly","Action!","ActionScript","Ada","ALGOL 68","AppleScript","ARM Assembly","Arturo","AutoHotkey","AWK","BBC BASIC","BASIC","GWBASIC","IS-BASIC","BCPL","C","C sharp","C++","Clojure","COBOL","Common Lisp","Crystal","D","Dart","Delphi","E","EasyLang","EchoLisp","Eiffel","Elena","Elixir","Erlang","Euphoria","F Sharp", "Factor","Forth","Fortran","FreeBASIC","Gambas","GAP","Go","Haskell","Haxe","Icon","Unicon","Io","J","Java","JavaScript","Jq","Julia","Kotlin","Liberty BASIC","LSE","Lua","Maple","Mathematica","Wolfram Language","MATLAB","Octave","Maxima","MAXScript","N/t/roff","Nanoquery","Nemerle","NetRexx","Nim","OCaml","Oforth","OoRexx","Oz","PARI/GP","Pascal","Perl","Phix","PHP","PicoLisp","PL/I","PowerShell","Prolog","PureBasic","Python","Qi","Quackery","R","Ra","Racket","Raku","REXX","Ring","Ruby","Run BASIC","Rust","Scala","Seed7","Sidef","Standard ML","Stata","Swift","Tcl","Tcllib","TI-83 BASIC","UBasic/4tH","Ursala","VBA","VBScript","Wren","Wren-sort","XPL0","Zkl","GUISS/Omit"],"wgPageViewLanguage":"en","wgPageContentLanguage":"en","wgPageContentModel":"wikitext","wgRelevantPageName":"Sorting_algorithms/Selection_sort","wgRelevantArticleId":1473,"wgIsProbablyEditable":false,"wgRelevantPageIsProbablyEditable":false,"wgRestrictionEdit":[],"wgRestrictionMove":[],"wgNoticeProject":"all", "wgCiteReferencePreviewsActive":true,"wgMFDisplayWikibaseDescriptions":{"search":false,"nearby":false,"watchlist":false,"tagline":false},"wgCheckUserClientHintsHeadersJsApi":["architecture","bitness","brands","fullVersionList","mobile","model","platform","platformVersion"],"wgIsMobile":false,"wgCentralAuthMobileDomain":false,"wgSiteNoticeId":"89.0"};RLSTATE={"ext.globalCssJs.user.styles":"ready","site.styles":"ready","user.styles":"ready","ext.globalCssJs.user":"ready","user":"ready","user.options":"loading","ext.pygments":"ready","skins.vector.search.codex.styles":"ready","skins.vector.styles":"ready","skins.vector.icons":"ready","ext.CookieWarning.styles":"ready","oojs-ui-core.styles":"ready","oojs-ui.styles.indicators":"ready","mediawiki.widgets.styles":"ready","oojs-ui-core.icons":"ready","ext.MobileDetect.nomobile":"ready","ext.dismissableSiteNotice.styles":"ready"};RLPAGEMODULES=["ext.pygments.view","ext.smw.styles","smw.entityexaminer","site","mediawiki.page.ready", "mediawiki.toc","skins.vector.js","ext.centralNotice.geoIP","ext.centralNotice.startUp","ext.checkUser.clientHints","ext.CookieWarning","ext.echo.centralauth","ext.eventLogging","ext.urlShortener.toolbar","ext.toctree","ext.centralauth.centralautologin","ext.purge","ext.smw.purge","ext.dismissableSiteNotice"];</script> <script>(RLQ=window.RLQ||[]).push(function(){mw.loader.impl(function(){return["user.options@12s5i",function($,jQuery,require,module){mw.user.tokens.set({"patrolToken":"+\\","watchToken":"+\\","csrfToken":"+\\"}); }];});});</script> <link rel="stylesheet" href="/w/load.php?lang=en&modules=ext.CookieWarning.styles%7Cext.MobileDetect.nomobile%7Cext.dismissableSiteNotice.styles%7Cext.pygments%7Cmediawiki.widgets.styles%7Coojs-ui-core.icons%2Cstyles%7Coojs-ui.styles.indicators%7Cskins.vector.icons%2Cstyles%7Cskins.vector.search.codex.styles&only=styles&skin=vector-2022"> <script async="" src="/w/load.php?lang=en&modules=startup&only=scripts&raw=1&skin=vector-2022"></script> <style>#mw-indicator-mw-helplink {display:none;}</style> <meta name="ResourceLoaderDynamicStyles" content=""> <link rel="stylesheet" href="/w/load.php?lang=en&modules=site.styles&only=styles&skin=vector-2022"> <meta name="generator" content="MediaWiki 1.43.0-beta"> <meta name="referrer" content="origin"> <meta name="referrer" content="origin-when-cross-origin"> <meta name="robots" content="max-image-preview:standard"> <meta name="format-detection" content="telephone=no"> <meta name="description" content="Task Sort an array (or list) of elements using the Selection sort algorithm. It works as follows: First find the smallest element in the array and exchange it..."> <meta name="twitter:site" content=""> <meta name="twitter:card" content="summary_large_image"> <meta name="viewport" content="width=1120"> <meta property="og:title" content="Sorting algorithms/Selection sort - Rosetta Code"> <meta property="og:type" content="website"> <link rel="preconnect" href="//static.wikitide.net"> <link rel="alternate" type="application/rdf+xml" title="Sorting algorithms/Selection sort" href="/wiki/Special:ExportRDF/Sorting_algorithms/Selection_sort?xmlmime=rdf"> <link rel="search" type="application/opensearchdescription+xml" href="/w/rest.php/v1/search" title="Rosetta Code (en)"> <link rel="EditURI" type="application/rsd+xml" href="https://rosettacode.org/w/api.php?action=rsd"> <link rel="canonical" href="https://rosettacode.org/wiki/Sorting_algorithms/Selection_sort"> <link rel="alternate" type="application/atom+xml" title="Rosetta Code Atom feed" href="/wiki/Special:RecentChanges?feed=atom"> <link rel="dns-prefetch" href="https://meta.miraheze.org" /> <meta property="og:title" content="Sorting algorithms/Selection sort"> <meta property="og:site_name" content="Rosetta Code"> <meta property="og:url" content="https://rosettacode.org/wiki/Sorting_algorithms/Selection_sort"> <meta property="og:description" content="Task Sort an array (or list) of elements using the Selection sort algorithm. It works as follows: First find the smallest element in the array and exchange it..."> <meta property="og:image" content="https://static.miraheze.org/rosettacodewiki/d/d3/RosettaCodeTitle.png"> <meta property="article:modified_time" content="2024-11-25T09:55:02Z"> <meta property="article:published_time" content="2024-11-25T09:55:02Z"> <script type="application/ld+json">{"@context":"http:\/\/schema.org","@type":"Article","name":"Sorting algorithms\/Selection sort - Rosetta Code","headline":"Sorting algorithms\/Selection sort - Rosetta Code","mainEntityOfPage":"Sorting algorithms\/Selection sort","identifier":"https:\/\/rosettacode.org\/wiki\/Sorting_algorithms\/Selection_sort","url":"https:\/\/rosettacode.org\/wiki\/Sorting_algorithms\/Selection_sort","description":"Task Sort an array (or list) of elements using the Selection sort algorithm. It works as follows: First find the smallest element in the array and exchange it...","dateModified":"2024-11-25T09:55:02Z","datePublished":"2024-11-25T09:55:02Z","image":{"@type":"ImageObject","url":"https:\/\/static.miraheze.org\/rosettacodewiki\/d\/d3\/RosettaCodeTitle.png"},"author":{"@type":"Organization","name":"Rosetta Code","url":"https:\/\/rosettacode.org","logo":{"@type":"ImageObject","url":"https:\/\/static.miraheze.org\/rosettacodewiki\/d\/d3\/RosettaCodeTitle.png","caption":"Rosetta Code"}},"publisher":{"@type":"Organization","name":"Rosetta Code","url":"https:\/\/rosettacode.org","logo":{"@type":"ImageObject","url":"https:\/\/static.miraheze.org\/rosettacodewiki\/d\/d3\/RosettaCodeTitle.png","caption":"Rosetta Code"}},"potentialAction":{"@type":"SearchAction","target":"https:\/\/rosettacode.org\/wiki\/Special:Search?search={search_term}","query-input":"required name=search_term"}}</script> <link rel="dns-prefetch" href="//login.miraheze.org"> </head> <body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Sorting_algorithms_Selection_sort rootpage-Sorting_algorithms skin-vector-2022 action-view"><a class="mw-jump-link" href="#bodyContent">Jump to content</a> <div class="vector-header-container"> <header class="vector-header mw-header"> <div class="vector-header-start"> <nav class="vector-main-menu-landmark" aria-label="Site"> <div id="vector-main-menu-dropdown" class="vector-dropdown vector-main-menu-dropdown vector-button-flush-left vector-button-flush-right" > <input type="checkbox" id="vector-main-menu-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-main-menu-dropdown" class="vector-dropdown-checkbox " aria-label="Main menu" > <label id="vector-main-menu-dropdown-label" for="vector-main-menu-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-menu mw-ui-icon-wikimedia-menu"></span> <span class="vector-dropdown-label-text">Main menu</span> </label> <div class="vector-dropdown-content"> <div id="vector-main-menu-unpinned-container" class="vector-unpinned-container"> <div id="vector-main-menu" class="vector-main-menu vector-pinnable-element"> <div class="vector-pinnable-header vector-main-menu-pinnable-header vector-pinnable-header-unpinned" data-feature-name="main-menu-pinned" data-pinnable-element-id="vector-main-menu" data-pinned-container-id="vector-main-menu-pinned-container" data-unpinned-container-id="vector-main-menu-unpinned-container" > <div class="vector-pinnable-header-label">Main menu</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-main-menu.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-main-menu.unpin">hide</button> </div> <div id="p-Social" class="vector-menu mw-portlet mw-portlet-Social" > <div class="vector-menu-heading"> Social </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="n-Discord" class="mw-list-item"><a href="https://discord.gg/JUAQDevqaH" rel="nofollow"><span>Discord</span></a></li><li id="n-Facebook" class="mw-list-item"><a href="https://www.facebook.com/rosettacode" rel="nofollow"><span>Facebook</span></a></li><li id="n-Twitter" class="mw-list-item"><a href="https://twitter.com/rosettacode" rel="nofollow"><span>Twitter</span></a></li> </ul> </div> </div> <div id="p-Explore" class="vector-menu mw-portlet mw-portlet-Explore" > <div class="vector-menu-heading"> Explore </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="n-Languages" class="mw-list-item"><a href="/wiki/Category:Programming_Languages"><span>Languages</span></a></li><li id="n-Tasks" class="mw-list-item"><a href="/wiki/Category:Programming_Tasks"><span>Tasks</span></a></li><li id="n-randompage" class="mw-list-item"><a href="/wiki/Special:Random" title="Load a random page [x]" accesskey="x"><span>Random page</span></a></li> </ul> </div> </div> </div> </div> </div> </div> </nav> <a href="/wiki/Rosetta_Code" class="mw-logo"> <img class="mw-logo-icon" src="https://static.miraheze.org/rosettacodewiki/c/c8/RosettaCodeTitle.ico" alt="" aria-hidden="true" height="50" width="50"> <span class="mw-logo-container skin-invert"> <strong class="mw-logo-wordmark">Rosetta Code</strong> </span> </a> </div> <div class="vector-header-end"> <div id="p-search" role="search" class="vector-search-box-vue vector-search-box-collapses vector-search-box-show-thumbnail vector-search-box-auto-expand-width vector-search-box"> <a href="/wiki/Special:Search" class="cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only search-toggle" title="Search Rosetta Code [f]" accesskey="f"><span class="vector-icon mw-ui-icon-search mw-ui-icon-wikimedia-search"></span> <span>Search</span> </a> <div class="vector-typeahead-search-container"> <div class="cdx-typeahead-search cdx-typeahead-search--show-thumbnail cdx-typeahead-search--auto-expand-width"> <form action="/w/index.php" id="searchform" class="cdx-search-input cdx-search-input--has-end-button"> <div id="simpleSearch" class="cdx-search-input__input-wrapper" data-search-loc="header-moved"> <div class="cdx-text-input cdx-text-input--has-start-icon"> <input class="cdx-text-input__input" type="search" name="search" placeholder="Search Rosetta Code" aria-label="Search Rosetta Code" autocapitalize="sentences" title="Search Rosetta Code [f]" accesskey="f" id="searchInput" > <span class="cdx-text-input__icon cdx-text-input__start-icon"></span> </div> <input type="hidden" name="title" value="Special:Search"> </div> <button class="cdx-button cdx-search-input__end-button">Search</button> </form> </div> </div> </div> <nav class="vector-user-links vector-user-links-wide" aria-label="Personal tools"> <div class="vector-user-links-main"> <div id="p-vector-user-menu-preferences" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <div id="p-vector-user-menu-userpage" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <nav class="vector-appearance-landmark" aria-label="Appearance"> <div id="vector-appearance-dropdown" class="vector-dropdown " title="Change the appearance of the page's font size, width, and color" > <input type="checkbox" id="vector-appearance-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-appearance-dropdown" class="vector-dropdown-checkbox " aria-label="Appearance" > <label id="vector-appearance-dropdown-label" for="vector-appearance-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-appearance mw-ui-icon-wikimedia-appearance"></span> <span class="vector-dropdown-label-text">Appearance</span> </label> <div class="vector-dropdown-content"> <div id="vector-appearance-unpinned-container" class="vector-unpinned-container"> </div> </div> </div> </nav> <div id="p-vector-user-menu-notifications" class="vector-menu mw-portlet emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> <div id="p-vector-user-menu-overflow" class="vector-menu mw-portlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-createaccount-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="/wiki/Special:CreateAccount?returnto=Sorting+algorithms%2FSelection+sort" title="You are encouraged to create an account and log in; however, it is not mandatory" class=""><span>Create account</span></a> </li> <li id="pt-login-2" class="user-links-collapsible-item mw-list-item user-links-collapsible-item"><a data-mw="interface" href="/wiki/Special:UserLogin?returnto=Sorting+algorithms%2FSelection+sort" title="You are encouraged to log in; however, it is not mandatory [o]" accesskey="o" class=""><span>Log in</span></a> </li> </ul> </div> </div> </div> <div id="vector-user-links-dropdown" class="vector-dropdown vector-user-menu vector-button-flush-right vector-user-menu-logged-out" title="More options" > <input type="checkbox" id="vector-user-links-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-user-links-dropdown" class="vector-dropdown-checkbox " aria-label="Personal tools" > <label id="vector-user-links-dropdown-label" for="vector-user-links-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-ellipsis mw-ui-icon-wikimedia-ellipsis"></span> <span class="vector-dropdown-label-text">Personal tools</span> </label> <div class="vector-dropdown-content"> <div id="p-personal" class="vector-menu mw-portlet mw-portlet-personal user-links-collapsible-item" title="User menu" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-createaccount" class="user-links-collapsible-item mw-list-item"><a href="/wiki/Special:CreateAccount?returnto=Sorting+algorithms%2FSelection+sort" title="You are encouraged to create an account and log in; however, it is not mandatory"><span class="vector-icon mw-ui-icon-userAdd mw-ui-icon-wikimedia-userAdd"></span> <span>Create account</span></a></li><li id="pt-login" class="user-links-collapsible-item mw-list-item"><a href="/wiki/Special:UserLogin?returnto=Sorting+algorithms%2FSelection+sort" title="You are encouraged to log in; however, it is not mandatory [o]" accesskey="o"><span class="vector-icon mw-ui-icon-logIn mw-ui-icon-wikimedia-logIn"></span> <span>Log in</span></a></li> </ul> </div> </div> <div id="p-user-menu-anon-editor" class="vector-menu mw-portlet mw-portlet-user-menu-anon-editor" > <div class="vector-menu-heading"> Pages for logged out editors <a href="/wiki/Help:Introduction" aria-label="Learn more about editing"><span>learn more</span></a> </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="pt-anoncontribs" class="mw-list-item"><a href="/wiki/Special:MyContributions" title="A list of edits made from this IP address [y]" accesskey="y"><span>Contributions</span></a></li><li id="pt-anontalk" class="mw-list-item"><a href="/wiki/Special:MyTalk" title="Discussion about edits from this IP address [n]" accesskey="n"><span>Talk</span></a></li> </ul> </div> </div> </div> </div> </nav> </div> </header> </div> <div class="mw-page-container"> <div class="mw-page-container-inner"> <div class="vector-sitenotice-container"> <div id="siteNotice"><div id="mw-dismissablenotice-anonplace"></div><script>(function(){var node=document.getElementById("mw-dismissablenotice-anonplace");if(node){node.outerHTML="\u003Cdiv class=\"mw-dismissable-notice\"\u003E\u003Cdiv class=\"mw-dismissable-notice-close\"\u003E[\u003Ca tabindex=\"0\" role=\"button\"\u003Edismiss\u003C/a\u003E]\u003C/div\u003E\u003Cdiv class=\"mw-dismissable-notice-body\"\u003E\u003C!-- CentralNotice --\u003E\u003Cdiv id=\"localNotice\" data-nosnippet=\"\"\u003E\u003Cdiv class=\"sitenotice\" lang=\"en\" dir=\"ltr\"\u003E\u003Ctable class=\"messagebox\" style=\"text-align: center; background-color: lightblue; clear: both;\"\u003E\n\u003Ctbody\u003E\u003Ctr\u003E\n\u003Ctd\u003E\u003Ci\u003E\u003Ca rel=\"nofollow\" class=\"external text\" href=\"https://discord.gg/JUAQDevqaH\"\u003EJoin our Discord\u003C/a\u003E to discuss Rosetta Code!\u003C/i\u003E\n\u003C/td\u003E\u003C/tr\u003E\u003C/tbody\u003E\u003C/table\u003E\u003C/div\u003E\u003C/div\u003E\u003C/div\u003E\u003C/div\u003E";}}());</script></div> </div> <div class="vector-column-start"> <div class="vector-main-menu-container"> <div id="mw-navigation"> <nav id="mw-panel" class="vector-main-menu-landmark" aria-label="Site"> <div id="vector-main-menu-pinned-container" class="vector-pinned-container"> </div> </nav> </div> </div> <div class="vector-sticky-pinned-container"> <nav id="mw-panel-toc" aria-label="Contents" data-event-name="ui.sidebar-toc" class="mw-table-of-contents-container vector-toc-landmark"> <div id="vector-toc-pinned-container" class="vector-pinned-container"> <div id="vector-toc" class="vector-toc vector-pinnable-element"> <div class="vector-pinnable-header vector-toc-pinnable-header vector-pinnable-header-pinned" data-feature-name="toc-pinned" data-pinnable-element-id="vector-toc" > <h2 class="vector-pinnable-header-label">Contents</h2> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-toc.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-toc.unpin">hide</button> </div> <ul class="vector-toc-contents" id="mw-panel-toc-list"> <li id="toc-mw-content-text" class="vector-toc-list-item vector-toc-level-1"> <a href="#" class="vector-toc-link"> <div class="vector-toc-text">Beginning</div> </a> </li> <li id="toc-11l" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#11l"> <div class="vector-toc-text"> <span class="vector-toc-numb">1</span> <span>11l</span> </div> </a> <ul id="toc-11l-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-360_Assembly" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#360_Assembly"> <div class="vector-toc-text"> <span class="vector-toc-numb">2</span> <span>360 Assembly</span> </div> </a> <ul id="toc-360_Assembly-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-AArch64_Assembly" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#AArch64_Assembly"> <div class="vector-toc-text"> <span class="vector-toc-numb">3</span> <span>AArch64 Assembly</span> </div> </a> <ul id="toc-AArch64_Assembly-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Action!" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Action!"> <div class="vector-toc-text"> <span class="vector-toc-numb">4</span> <span>Action!</span> </div> </a> <ul id="toc-Action!-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-ActionScript" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#ActionScript"> <div class="vector-toc-text"> <span class="vector-toc-numb">5</span> <span>ActionScript</span> </div> </a> <ul id="toc-ActionScript-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Ada" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Ada"> <div class="vector-toc-text"> <span class="vector-toc-numb">6</span> <span>Ada</span> </div> </a> <ul id="toc-Ada-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-ALGOL_68" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#ALGOL_68"> <div class="vector-toc-text"> <span class="vector-toc-numb">7</span> <span>ALGOL 68</span> </div> </a> <ul id="toc-ALGOL_68-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-AppleScript" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#AppleScript"> <div class="vector-toc-text"> <span class="vector-toc-numb">8</span> <span>AppleScript</span> </div> </a> <ul id="toc-AppleScript-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-ARM_Assembly" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#ARM_Assembly"> <div class="vector-toc-text"> <span class="vector-toc-numb">9</span> <span>ARM Assembly</span> </div> </a> <ul id="toc-ARM_Assembly-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Arturo" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Arturo"> <div class="vector-toc-text"> <span class="vector-toc-numb">10</span> <span>Arturo</span> </div> </a> <ul id="toc-Arturo-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-AutoHotkey" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#AutoHotkey"> <div class="vector-toc-text"> <span class="vector-toc-numb">11</span> <span>AutoHotkey</span> </div> </a> <ul id="toc-AutoHotkey-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-AWK" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#AWK"> <div class="vector-toc-text"> <span class="vector-toc-numb">12</span> <span>AWK</span> </div> </a> <ul id="toc-AWK-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-BBC_BASIC" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#BBC_BASIC"> <div class="vector-toc-text"> <span class="vector-toc-numb">13</span> <span>BBC BASIC</span> </div> </a> <ul id="toc-BBC_BASIC-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-BASIC" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#BASIC"> <div class="vector-toc-text"> <span class="vector-toc-numb">14</span> <span>BASIC</span> </div> </a> <button aria-controls="toc-BASIC-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle BASIC subsection</span> </button> <ul id="toc-BASIC-sublist" class="vector-toc-list"> <li id="toc-GWBASIC" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#GWBASIC"> <div class="vector-toc-text"> <span class="vector-toc-numb">14.1</span> <span>GWBASIC</span> </div> </a> <ul id="toc-GWBASIC-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-IS-BASIC" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#IS-BASIC"> <div class="vector-toc-text"> <span class="vector-toc-numb">14.2</span> <span>IS-BASIC</span> </div> </a> <ul id="toc-IS-BASIC-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-BCPL" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#BCPL"> <div class="vector-toc-text"> <span class="vector-toc-numb">15</span> <span>BCPL</span> </div> </a> <ul id="toc-BCPL-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-C" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#C"> <div class="vector-toc-text"> <span class="vector-toc-numb">16</span> <span>C</span> </div> </a> <ul id="toc-C-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-C#" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#C#"> <div class="vector-toc-text"> <span class="vector-toc-numb">17</span> <span>C#</span> </div> </a> <ul id="toc-C#-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-C++" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#C++"> <div class="vector-toc-text"> <span class="vector-toc-numb">18</span> <span>C++</span> </div> </a> <ul id="toc-C++-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Clojure" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Clojure"> <div class="vector-toc-text"> <span class="vector-toc-numb">19</span> <span>Clojure</span> </div> </a> <ul id="toc-Clojure-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-COBOL" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#COBOL"> <div class="vector-toc-text"> <span class="vector-toc-numb">20</span> <span>COBOL</span> </div> </a> <ul id="toc-COBOL-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Common_Lisp" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Common_Lisp"> <div class="vector-toc-text"> <span class="vector-toc-numb">21</span> <span>Common Lisp</span> </div> </a> <ul id="toc-Common_Lisp-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Crystal" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Crystal"> <div class="vector-toc-text"> <span class="vector-toc-numb">22</span> <span>Crystal</span> </div> </a> <ul id="toc-Crystal-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-D" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#D"> <div class="vector-toc-text"> <span class="vector-toc-numb">23</span> <span>D</span> </div> </a> <ul id="toc-D-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Dart" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Dart"> <div class="vector-toc-text"> <span class="vector-toc-numb">24</span> <span>Dart</span> </div> </a> <ul id="toc-Dart-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Delphi" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Delphi"> <div class="vector-toc-text"> <span class="vector-toc-numb">25</span> <span>Delphi</span> </div> </a> <button aria-controls="toc-Delphi-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle Delphi subsection</span> </button> <ul id="toc-Delphi-sublist" class="vector-toc-list"> <li id="toc-Array_sort" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Array_sort"> <div class="vector-toc-text"> <span class="vector-toc-numb">25.1</span> <span>Array sort</span> </div> </a> <ul id="toc-Array_sort-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-String_sort" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#String_sort"> <div class="vector-toc-text"> <span class="vector-toc-numb">25.2</span> <span>String sort</span> </div> </a> <ul id="toc-String_sort-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-E" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#E"> <div class="vector-toc-text"> <span class="vector-toc-numb">26</span> <span>E</span> </div> </a> <ul id="toc-E-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-EasyLang" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#EasyLang"> <div class="vector-toc-text"> <span class="vector-toc-numb">27</span> <span>EasyLang</span> </div> </a> <ul id="toc-EasyLang-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-EchoLisp" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#EchoLisp"> <div class="vector-toc-text"> <span class="vector-toc-numb">28</span> <span>EchoLisp</span> </div> </a> <button aria-controls="toc-EchoLisp-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle EchoLisp subsection</span> </button> <ul id="toc-EchoLisp-sublist" class="vector-toc-list"> <li id="toc-List_sort" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#List_sort"> <div class="vector-toc-text"> <span class="vector-toc-numb">28.1</span> <span>List sort</span> </div> </a> <ul id="toc-List_sort-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Array_sort_2" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Array_sort_2"> <div class="vector-toc-text"> <span class="vector-toc-numb">28.2</span> <span>Array sort</span> </div> </a> <ul id="toc-Array_sort_2-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Eiffel" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Eiffel"> <div class="vector-toc-text"> <span class="vector-toc-numb">29</span> <span>Eiffel</span> </div> </a> <ul id="toc-Eiffel-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Elena" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Elena"> <div class="vector-toc-text"> <span class="vector-toc-numb">30</span> <span>Elena</span> </div> </a> <ul id="toc-Elena-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Elixir" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Elixir"> <div class="vector-toc-text"> <span class="vector-toc-numb">31</span> <span>Elixir</span> </div> </a> <ul id="toc-Elixir-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Erlang" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Erlang"> <div class="vector-toc-text"> <span class="vector-toc-numb">32</span> <span>Erlang</span> </div> </a> <ul id="toc-Erlang-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Euphoria" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Euphoria"> <div class="vector-toc-text"> <span class="vector-toc-numb">33</span> <span>Euphoria</span> </div> </a> <ul id="toc-Euphoria-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-F#" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#F#"> <div class="vector-toc-text"> <span class="vector-toc-numb">34</span> <span>F#</span> </div> </a> <ul id="toc-F#-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Factor" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Factor"> <div class="vector-toc-text"> <span class="vector-toc-numb">35</span> <span>Factor</span> </div> </a> <ul id="toc-Factor-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Forth" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Forth"> <div class="vector-toc-text"> <span class="vector-toc-numb">36</span> <span>Forth</span> </div> </a> <ul id="toc-Forth-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Fortran" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Fortran"> <div class="vector-toc-text"> <span class="vector-toc-numb">37</span> <span>Fortran</span> </div> </a> <ul id="toc-Fortran-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-FreeBASIC" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#FreeBASIC"> <div class="vector-toc-text"> <span class="vector-toc-numb">38</span> <span>FreeBASIC</span> </div> </a> <ul id="toc-FreeBASIC-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Gambas" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Gambas"> <div class="vector-toc-text"> <span class="vector-toc-numb">39</span> <span>Gambas</span> </div> </a> <ul id="toc-Gambas-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-GAP" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#GAP"> <div class="vector-toc-text"> <span class="vector-toc-numb">40</span> <span>GAP</span> </div> </a> <ul id="toc-GAP-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Go" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Go"> <div class="vector-toc-text"> <span class="vector-toc-numb">41</span> <span>Go</span> </div> </a> <ul id="toc-Go-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Haskell" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Haskell"> <div class="vector-toc-text"> <span class="vector-toc-numb">42</span> <span>Haskell</span> </div> </a> <ul id="toc-Haskell-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Haxe" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Haxe"> <div class="vector-toc-text"> <span class="vector-toc-numb">43</span> <span>Haxe</span> </div> </a> <ul id="toc-Haxe-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Icon_and_Unicon" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Icon_and_Unicon"> <div class="vector-toc-text"> <span class="vector-toc-numb">44</span> <span>Icon and Unicon</span> </div> </a> <ul id="toc-Icon_and_Unicon-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Io" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Io"> <div class="vector-toc-text"> <span class="vector-toc-numb">45</span> <span>Io</span> </div> </a> <ul id="toc-Io-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-J" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#J"> <div class="vector-toc-text"> <span class="vector-toc-numb">46</span> <span>J</span> </div> </a> <ul id="toc-J-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Java" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Java"> <div class="vector-toc-text"> <span class="vector-toc-numb">47</span> <span>Java</span> </div> </a> <ul id="toc-Java-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-JavaScript" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#JavaScript"> <div class="vector-toc-text"> <span class="vector-toc-numb">48</span> <span>JavaScript</span> </div> </a> <ul id="toc-JavaScript-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-jq" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#jq"> <div class="vector-toc-text"> <span class="vector-toc-numb">49</span> <span>jq</span> </div> </a> <ul id="toc-jq-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Julia" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Julia"> <div class="vector-toc-text"> <span class="vector-toc-numb">50</span> <span>Julia</span> </div> </a> <ul id="toc-Julia-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Kotlin" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Kotlin"> <div class="vector-toc-text"> <span class="vector-toc-numb">51</span> <span>Kotlin</span> </div> </a> <ul id="toc-Kotlin-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Liberty_BASIC" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Liberty_BASIC"> <div class="vector-toc-text"> <span class="vector-toc-numb">52</span> <span>Liberty BASIC</span> </div> </a> <ul id="toc-Liberty_BASIC-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-LSE" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#LSE"> <div class="vector-toc-text"> <span class="vector-toc-numb">53</span> <span>LSE</span> </div> </a> <ul id="toc-LSE-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Lua" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Lua"> <div class="vector-toc-text"> <span class="vector-toc-numb">54</span> <span>Lua</span> </div> </a> <ul id="toc-Lua-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Maple" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Maple"> <div class="vector-toc-text"> <span class="vector-toc-numb">55</span> <span>Maple</span> </div> </a> <ul id="toc-Maple-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Mathematica_/Wolfram_Language" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Mathematica_/Wolfram_Language"> <div class="vector-toc-text"> <span class="vector-toc-numb">56</span> <span>Mathematica /Wolfram Language</span> </div> </a> <ul id="toc-Mathematica_/Wolfram_Language-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-MATLAB_/_Octave" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#MATLAB_/_Octave"> <div class="vector-toc-text"> <span class="vector-toc-numb">57</span> <span>MATLAB / Octave</span> </div> </a> <ul id="toc-MATLAB_/_Octave-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Maxima" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Maxima"> <div class="vector-toc-text"> <span class="vector-toc-numb">58</span> <span>Maxima</span> </div> </a> <ul id="toc-Maxima-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-MAXScript" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#MAXScript"> <div class="vector-toc-text"> <span class="vector-toc-numb">59</span> <span>MAXScript</span> </div> </a> <ul id="toc-MAXScript-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-N/t/roff" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#N/t/roff"> <div class="vector-toc-text"> <span class="vector-toc-numb">60</span> <span>N/t/roff</span> </div> </a> <button aria-controls="toc-N/t/roff-sublist" class="cdx-button cdx-button--weight-quiet cdx-button--icon-only vector-toc-toggle"> <span class="vector-icon mw-ui-icon-wikimedia-expand"></span> <span>Toggle N/t/roff subsection</span> </button> <ul id="toc-N/t/roff-sublist" class="vector-toc-list"> <li id="toc-Output" class="vector-toc-list-item vector-toc-level-2"> <a class="vector-toc-link" href="#Output"> <div class="vector-toc-text"> <span class="vector-toc-numb">60.1</span> <span>Output</span> </div> </a> <ul id="toc-Output-sublist" class="vector-toc-list"> </ul> </li> </ul> </li> <li id="toc-Nanoquery" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Nanoquery"> <div class="vector-toc-text"> <span class="vector-toc-numb">61</span> <span>Nanoquery</span> </div> </a> <ul id="toc-Nanoquery-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Nemerle" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Nemerle"> <div class="vector-toc-text"> <span class="vector-toc-numb">62</span> <span>Nemerle</span> </div> </a> <ul id="toc-Nemerle-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-NetRexx" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#NetRexx"> <div class="vector-toc-text"> <span class="vector-toc-numb">63</span> <span>NetRexx</span> </div> </a> <ul id="toc-NetRexx-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Nim" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Nim"> <div class="vector-toc-text"> <span class="vector-toc-numb">64</span> <span>Nim</span> </div> </a> <ul id="toc-Nim-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-OCaml" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#OCaml"> <div class="vector-toc-text"> <span class="vector-toc-numb">65</span> <span>OCaml</span> </div> </a> <ul id="toc-OCaml-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Oforth" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Oforth"> <div class="vector-toc-text"> <span class="vector-toc-numb">66</span> <span>Oforth</span> </div> </a> <ul id="toc-Oforth-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-ooRexx" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#ooRexx"> <div class="vector-toc-text"> <span class="vector-toc-numb">67</span> <span>ooRexx</span> </div> </a> <ul id="toc-ooRexx-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Oz" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Oz"> <div class="vector-toc-text"> <span class="vector-toc-numb">68</span> <span>Oz</span> </div> </a> <ul id="toc-Oz-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-PARI/GP" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#PARI/GP"> <div class="vector-toc-text"> <span class="vector-toc-numb">69</span> <span>PARI/GP</span> </div> </a> <ul id="toc-PARI/GP-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Pascal" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Pascal"> <div class="vector-toc-text"> <span class="vector-toc-numb">70</span> <span>Pascal</span> </div> </a> <ul id="toc-Pascal-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Perl" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Perl"> <div class="vector-toc-text"> <span class="vector-toc-numb">71</span> <span>Perl</span> </div> </a> <ul id="toc-Perl-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Phix" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Phix"> <div class="vector-toc-text"> <span class="vector-toc-numb">72</span> <span>Phix</span> </div> </a> <ul id="toc-Phix-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-PHP" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#PHP"> <div class="vector-toc-text"> <span class="vector-toc-numb">73</span> <span>PHP</span> </div> </a> <ul id="toc-PHP-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-PicoLisp" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#PicoLisp"> <div class="vector-toc-text"> <span class="vector-toc-numb">74</span> <span>PicoLisp</span> </div> </a> <ul id="toc-PicoLisp-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-PL/I" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#PL/I"> <div class="vector-toc-text"> <span class="vector-toc-numb">75</span> <span>PL/I</span> </div> </a> <ul id="toc-PL/I-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-PowerShell" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#PowerShell"> <div class="vector-toc-text"> <span class="vector-toc-numb">76</span> <span>PowerShell</span> </div> </a> <ul id="toc-PowerShell-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Prolog" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Prolog"> <div class="vector-toc-text"> <span class="vector-toc-numb">77</span> <span>Prolog</span> </div> </a> <ul id="toc-Prolog-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-PureBasic" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#PureBasic"> <div class="vector-toc-text"> <span class="vector-toc-numb">78</span> <span>PureBasic</span> </div> </a> <ul id="toc-PureBasic-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Python" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Python"> <div class="vector-toc-text"> <span class="vector-toc-numb">79</span> <span>Python</span> </div> </a> <ul id="toc-Python-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Qi" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Qi"> <div class="vector-toc-text"> <span class="vector-toc-numb">80</span> <span>Qi</span> </div> </a> <ul id="toc-Qi-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Quackery" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Quackery"> <div class="vector-toc-text"> <span class="vector-toc-numb">81</span> <span>Quackery</span> </div> </a> <ul id="toc-Quackery-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-R" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#R"> <div class="vector-toc-text"> <span class="vector-toc-numb">82</span> <span>R</span> </div> </a> <ul id="toc-R-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Ra" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Ra"> <div class="vector-toc-text"> <span class="vector-toc-numb">83</span> <span>Ra</span> </div> </a> <ul id="toc-Ra-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Racket" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Racket"> <div class="vector-toc-text"> <span class="vector-toc-numb">84</span> <span>Racket</span> </div> </a> <ul id="toc-Racket-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Raku" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Raku"> <div class="vector-toc-text"> <span class="vector-toc-numb">85</span> <span>Raku</span> </div> </a> <ul id="toc-Raku-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-REXX" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#REXX"> <div class="vector-toc-text"> <span class="vector-toc-numb">86</span> <span>REXX</span> </div> </a> <ul id="toc-REXX-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Ring" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Ring"> <div class="vector-toc-text"> <span class="vector-toc-numb">87</span> <span>Ring</span> </div> </a> <ul id="toc-Ring-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Ruby" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Ruby"> <div class="vector-toc-text"> <span class="vector-toc-numb">88</span> <span>Ruby</span> </div> </a> <ul id="toc-Ruby-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Run_BASIC" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Run_BASIC"> <div class="vector-toc-text"> <span class="vector-toc-numb">89</span> <span>Run BASIC</span> </div> </a> <ul id="toc-Run_BASIC-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Rust" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Rust"> <div class="vector-toc-text"> <span class="vector-toc-numb">90</span> <span>Rust</span> </div> </a> <ul id="toc-Rust-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Scala" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Scala"> <div class="vector-toc-text"> <span class="vector-toc-numb">91</span> <span>Scala</span> </div> </a> <ul id="toc-Scala-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Seed7" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Seed7"> <div class="vector-toc-text"> <span class="vector-toc-numb">92</span> <span>Seed7</span> </div> </a> <ul id="toc-Seed7-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Sidef" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Sidef"> <div class="vector-toc-text"> <span class="vector-toc-numb">93</span> <span>Sidef</span> </div> </a> <ul id="toc-Sidef-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Standard_ML" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Standard_ML"> <div class="vector-toc-text"> <span class="vector-toc-numb">94</span> <span>Standard ML</span> </div> </a> <ul id="toc-Standard_ML-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Stata" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Stata"> <div class="vector-toc-text"> <span class="vector-toc-numb">95</span> <span>Stata</span> </div> </a> <ul id="toc-Stata-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Swift" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Swift"> <div class="vector-toc-text"> <span class="vector-toc-numb">96</span> <span>Swift</span> </div> </a> <ul id="toc-Swift-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Tcl" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Tcl"> <div class="vector-toc-text"> <span class="vector-toc-numb">97</span> <span>Tcl</span> </div> </a> <ul id="toc-Tcl-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-TI-83_BASIC" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#TI-83_BASIC"> <div class="vector-toc-text"> <span class="vector-toc-numb">98</span> <span>TI-83 BASIC</span> </div> </a> <ul id="toc-TI-83_BASIC-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-uBasic/4tH" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#uBasic/4tH"> <div class="vector-toc-text"> <span class="vector-toc-numb">99</span> <span>uBasic/4tH</span> </div> </a> <ul id="toc-uBasic/4tH-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Ursala" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Ursala"> <div class="vector-toc-text"> <span class="vector-toc-numb">100</span> <span>Ursala</span> </div> </a> <ul id="toc-Ursala-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-VBA" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#VBA"> <div class="vector-toc-text"> <span class="vector-toc-numb">101</span> <span>VBA</span> </div> </a> <ul id="toc-VBA-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-VBScript" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#VBScript"> <div class="vector-toc-text"> <span class="vector-toc-numb">102</span> <span>VBScript</span> </div> </a> <ul id="toc-VBScript-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-Wren" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#Wren"> <div class="vector-toc-text"> <span class="vector-toc-numb">103</span> <span>Wren</span> </div> </a> <ul id="toc-Wren-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-XPL0" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#XPL0"> <div class="vector-toc-text"> <span class="vector-toc-numb">104</span> <span>XPL0</span> </div> </a> <ul id="toc-XPL0-sublist" class="vector-toc-list"> </ul> </li> <li id="toc-zkl" class="vector-toc-list-item vector-toc-level-1"> <a class="vector-toc-link" href="#zkl"> <div class="vector-toc-text"> <span class="vector-toc-numb">105</span> <span>zkl</span> </div> </a> <ul id="toc-zkl-sublist" class="vector-toc-list"> </ul> </li> </ul> </div> </div> </nav> </div> </div> <div class="mw-content-container"> <main id="content" class="mw-body"> <header class="mw-body-header vector-page-titlebar"> <nav aria-label="Contents" class="vector-toc-landmark"> <div id="vector-page-titlebar-toc" class="vector-dropdown vector-page-titlebar-toc vector-button-flush-left" > <input type="checkbox" id="vector-page-titlebar-toc-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-page-titlebar-toc" class="vector-dropdown-checkbox " aria-label="Toggle the table of contents" > <label id="vector-page-titlebar-toc-label" for="vector-page-titlebar-toc-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet cdx-button--icon-only " aria-hidden="true" ><span class="vector-icon mw-ui-icon-listBullet mw-ui-icon-wikimedia-listBullet"></span> <span class="vector-dropdown-label-text">Toggle the table of contents</span> </label> <div class="vector-dropdown-content"> <div id="vector-page-titlebar-toc-unpinned-container" class="vector-unpinned-container"> </div> </div> </div> </nav> <h1 id="firstHeading" class="firstHeading mw-first-heading"><span class="mw-page-title-main">Sorting algorithms/Selection sort</span></h1> </header> <div class="vector-page-toolbar"> <div class="vector-page-toolbar-container"> <div id="left-navigation"> <nav aria-label="Namespaces"> <div id="p-associated-pages" class="vector-menu vector-menu-tabs mw-portlet mw-portlet-associated-pages" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-nstab-main" class="selected vector-tab-noicon mw-list-item"><a href="/wiki/Sorting_algorithms/Selection_sort" title="View the content page [c]" accesskey="c"><span>Page</span></a></li><li id="ca-talk" class="vector-tab-noicon mw-list-item"><a href="/wiki/Talk:Sorting_algorithms/Selection_sort" rel="discussion" title="Discussion about the content page [t]" accesskey="t"><span>Discussion</span></a></li> </ul> </div> </div> <div id="vector-variants-dropdown" class="vector-dropdown emptyPortlet" > <input type="checkbox" id="vector-variants-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-variants-dropdown" class="vector-dropdown-checkbox " aria-label="Change language variant" > <label id="vector-variants-dropdown-label" for="vector-variants-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet" aria-hidden="true" ><span class="vector-dropdown-label-text">English</span> </label> <div class="vector-dropdown-content"> <div id="p-variants" class="vector-menu mw-portlet mw-portlet-variants emptyPortlet" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> </ul> </div> </div> </div> </div> </nav> </div> <div id="right-navigation" class="vector-collapsible"> <nav aria-label="Views"> <div id="p-views" class="vector-menu vector-menu-tabs mw-portlet mw-portlet-views" > <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-view" class="selected vector-tab-noicon mw-list-item"><a href="/wiki/Sorting_algorithms/Selection_sort"><span>Read</span></a></li><li id="ca-viewsource" class="vector-tab-noicon mw-list-item"><a href="/wiki/Sorting_algorithms/Selection_sort?action=edit" title="This page is protected. You can view its source [e]" accesskey="e"><span>View source</span></a></li><li id="ca-history" class="vector-tab-noicon mw-list-item"><a href="/wiki/Sorting_algorithms/Selection_sort?action=history" title="Past revisions of this page [h]" accesskey="h"><span>View history</span></a></li> </ul> </div> </div> </nav> <nav class="vector-page-tools-landmark" aria-label="Page tools"> <div id="vector-page-tools-dropdown" class="vector-dropdown vector-page-tools-dropdown" > <input type="checkbox" id="vector-page-tools-dropdown-checkbox" role="button" aria-haspopup="true" data-event-name="ui.dropdown-vector-page-tools-dropdown" class="vector-dropdown-checkbox " aria-label="Tools" > <label id="vector-page-tools-dropdown-label" for="vector-page-tools-dropdown-checkbox" class="vector-dropdown-label cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-quiet" aria-hidden="true" ><span class="vector-dropdown-label-text">Tools</span> </label> <div class="vector-dropdown-content"> <div id="vector-page-tools-unpinned-container" class="vector-unpinned-container"> <div id="vector-page-tools" class="vector-page-tools vector-pinnable-element"> <div class="vector-pinnable-header vector-page-tools-pinnable-header vector-pinnable-header-unpinned" data-feature-name="page-tools-pinned" data-pinnable-element-id="vector-page-tools" data-pinned-container-id="vector-page-tools-pinned-container" data-unpinned-container-id="vector-page-tools-unpinned-container" > <div class="vector-pinnable-header-label">Tools</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-page-tools.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-page-tools.unpin">hide</button> </div> <div id="p-cactions" class="vector-menu mw-portlet mw-portlet-cactions vector-has-collapsible-items" title="More options" > <div class="vector-menu-heading"> Actions </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="ca-more-view" class="selected vector-more-collapsible-item mw-list-item"><a href="/wiki/Sorting_algorithms/Selection_sort"><span>Read</span></a></li><li id="ca-more-viewsource" class="vector-more-collapsible-item mw-list-item"><a href="/wiki/Sorting_algorithms/Selection_sort?action=edit"><span>View source</span></a></li><li id="ca-more-history" class="vector-more-collapsible-item mw-list-item"><a href="/wiki/Sorting_algorithms/Selection_sort?action=history"><span>View history</span></a></li><li id="ca-purge" class="is-disabled mw-list-item"><a href="/wiki/Sorting_algorithms/Selection_sort?action=purge"><span>Refresh</span></a></li> </ul> </div> </div> <div id="p-tb" class="vector-menu mw-portlet mw-portlet-tb" > <div class="vector-menu-heading"> General </div> <div class="vector-menu-content"> <ul class="vector-menu-content-list"> <li id="t-whatlinkshere" class="mw-list-item"><a href="/wiki/Special:WhatLinksHere/Sorting_algorithms/Selection_sort" title="A list of all wiki pages that link here [j]" accesskey="j"><span>What links here</span></a></li><li id="t-recentchangeslinked" class="mw-list-item"><a href="/wiki/Special:RecentChangesLinked/Sorting_algorithms/Selection_sort" rel="nofollow" title="Recent changes in pages linked from this page [k]" accesskey="k"><span>Related changes</span></a></li><li id="t-specialpages" class="mw-list-item"><a href="/wiki/Special:SpecialPages" title="A list of all special pages [q]" accesskey="q"><span>Special pages</span></a></li><li id="t-print" class="mw-list-item"><a href="javascript:print();" rel="alternate" title="Printable version of this page [p]" accesskey="p"><span>Printable version</span></a></li><li id="t-permalink" class="mw-list-item"><a href="/wiki/Sorting_algorithms/Selection_sort?oldid=349291" title="Permanent link to this revision of this page"><span>Permanent link</span></a></li><li id="t-info" class="mw-list-item"><a href="/wiki/Sorting_algorithms/Selection_sort?action=info" title="More information about this page"><span>Page information</span></a></li><li id="t-cite" class="mw-list-item"><a href="/wiki/Special:CiteThisPage?page=Sorting_algorithms%2FSelection_sort&id=349291&wpFormIdentifier=titleform" title="Information on how to cite this page"><span>Cite this page</span></a></li><li id="t-urlshortener" class="mw-list-item"><a href="/wiki/Special:UrlShortener?url=https%3A%2F%2Frosettacode.org%2Fwiki%2FSorting_algorithms%2FSelection_sort"><span>Get shortened URL</span></a></li><li id="t-smwbrowselink" class="mw-list-item"><a href="/wiki/Special:Browse/:Sorting-5Falgorithms-2FSelection-5Fsort" rel="search"><span>Browse properties</span></a></li> </ul> </div> </div> </div> </div> </div> </div> </nav> </div> </div> </div> <div class="vector-column-end"> <div class="vector-sticky-pinned-container"> <nav class="vector-page-tools-landmark" aria-label="Page tools"> <div id="vector-page-tools-pinned-container" class="vector-pinned-container"> </div> </nav> <nav class="vector-appearance-landmark" aria-label="Appearance"> <div id="vector-appearance-pinned-container" class="vector-pinned-container"> <div id="vector-appearance" class="vector-appearance vector-pinnable-element"> <div class="vector-pinnable-header vector-appearance-pinnable-header vector-pinnable-header-pinned" data-feature-name="appearance-pinned" data-pinnable-element-id="vector-appearance" data-pinned-container-id="vector-appearance-pinned-container" data-unpinned-container-id="vector-appearance-unpinned-container" > <div class="vector-pinnable-header-label">Appearance</div> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-pin-button" data-event-name="pinnable-header.vector-appearance.pin">move to sidebar</button> <button class="vector-pinnable-header-toggle-button vector-pinnable-header-unpin-button" data-event-name="pinnable-header.vector-appearance.unpin">hide</button> </div> </div> </div> </nav> </div> </div> <div id="bodyContent" class="vector-body" aria-labelledby="firstHeading" data-mw-ve-target-container> <div class="vector-body-before-content"> <div class="mw-indicators"> <div id="mw-indicator-smw-entity-examiner" class="mw-indicator"><div class="smw-entity-examiner smw-indicator-vertical-bar-loader" data-subject="Sorting_algorithms/Selection_sort#0##" data-dir="ltr" data-uselang="" title="Running an examiner in the background"></div></div> </div> <div id="siteSub" class="noprint">From Rosetta Code</div> </div> <div id="contentSub"><div id="mw-content-subtitle"><div class="subpages">< <bdi dir="ltr"><a href="/wiki/Sorting_algorithms" class="mw-redirect" title="Sorting algorithms">Sorting algorithms</a></bdi></div></div></div> <div id="mw-content-text" class="mw-body-content"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><div class="infobox" style="width: 2in"><figure class="mw-halign-left" typeof="mw:File"><a href="/wiki/Category:Solutions_by_Programming_Task" title="Category:Solutions by Programming Task"><img alt="Task" src="//static.wikitide.net/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png" decoding="async" width="64" height="64" class="mw-file-element" srcset="//static.wikitide.net/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/96px-Rcode-button-task-crushed.png 1.5x, //static.wikitide.net/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/128px-Rcode-button-task-crushed.png 2x" /></a><figcaption></figcaption></figure><b>Sorting algorithms/Selection sort</b><br /> You are encouraged to <a href="/wiki/Rosetta_Code:Solve_a_Task" title="Rosetta Code:Solve a Task">solve this task</a> according to the task description, using any language you may know.</div> <div class="infobox" style="width: 2in"> <p><big><b>Sorting Algorithm</b></big><br /> This is a sorting algorithm.   It may be applied to a set of data in order to sort it.     For comparing various sorts, see <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Compare_sorting_algorithms%27_performance">compare sorts</a>.   For other sorting algorithms,   see <a href="/wiki/Category:Sorting_Algorithms" title="Category:Sorting Algorithms">sorting algorithms</a>,   or: </p> <small><center><b><a href="/wiki/O" title="O">O</a>(<i>n</i> log<i>n</i>) sorts</b><br /> <p><a href="/wiki/Sorting_algorithms/Heapsort" title="Sorting algorithms/Heapsort">Heap sort</a> | <a href="/wiki/Sorting_algorithms/Merge_sort" title="Sorting algorithms/Merge sort">Merge sort</a> | <a href="/wiki/Sorting_algorithms/Patience_sort" title="Sorting algorithms/Patience sort">Patience sort</a> | <a href="/wiki/Sorting_algorithms/Quicksort" title="Sorting algorithms/Quicksort">Quick sort</a> </p><p><b><a href="/wiki/O" title="O">O</a>(<i>n</i> log<sup>2</sup><i>n</i>) sorts</b><br /> <a href="/wiki/Sorting_algorithms/Shell_sort" title="Sorting algorithms/Shell sort">Shell Sort</a> </p><p><b><a href="/wiki/O" title="O">O</a>(<i>n</i><sup>2</sup>) sorts</b><br /> <a href="/wiki/Sorting_algorithms/Bubble_sort" title="Sorting algorithms/Bubble sort">Bubble sort</a> | <a href="/wiki/Sorting_algorithms/Cocktail_sort" title="Sorting algorithms/Cocktail sort">Cocktail sort</a> | <a href="/wiki/Sorting_algorithms/Cocktail_sort_with_shifting_bounds" title="Sorting algorithms/Cocktail sort with shifting bounds">Cocktail sort with shifting bounds</a> | <a href="/wiki/Sorting_algorithms/Comb_sort" title="Sorting algorithms/Comb sort">Comb sort</a> | <a href="/wiki/Sorting_algorithms/Cycle_sort" title="Sorting algorithms/Cycle sort">Cycle sort</a> | <a href="/wiki/Sorting_algorithms/Gnome_sort" title="Sorting algorithms/Gnome sort">Gnome sort</a> | <a href="/wiki/Sorting_algorithms/Insertion_sort" title="Sorting algorithms/Insertion sort">Insertion sort</a> | <a class="mw-selflink selflink">Selection sort</a> | <a href="/wiki/Sorting_algorithms/Strand_sort" title="Sorting algorithms/Strand sort">Strand sort</a> </p><p><b>other sorts</b><br /> <a href="/wiki/Sorting_algorithms/Bead_sort" title="Sorting algorithms/Bead sort">Bead sort</a> | <a href="/wiki/Sorting_algorithms/Bogosort" title="Sorting algorithms/Bogosort">Bogo sort</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Common_sorted_list">Common sorted list</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Sort_an_array_of_composite_structures">Composite structures sort</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Sort_using_a_custom_comparator">Custom comparator sort</a> | <a href="/wiki/Sorting_algorithms/Counting_sort" title="Sorting algorithms/Counting sort">Counting sort</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Sort_disjoint_sublist">Disjoint sublist sort</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/External_sort">External sort</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/JortSort">Jort sort</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Sort_numbers_lexicographically">Lexicographical sort</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Natural_sorting">Natural sorting</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Order_by_pair_comparisons">Order by pair comparisons</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Order_disjoint_list_items">Order disjoint list items</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Order_two_numerical_lists">Order two numerical lists</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Sort_a_list_of_object_identifiers">Object identifier (OID) sort</a> | <a href="/wiki/Sorting_algorithms/Pancake_sort" title="Sorting algorithms/Pancake sort">Pancake sort</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Quickselect_algorithm">Quickselect</a> | <a href="/wiki/Sorting_algorithms/Permutation_sort" title="Sorting algorithms/Permutation sort">Permutation sort</a> | <a href="/wiki/Sorting_algorithms/Radix_sort" title="Sorting algorithms/Radix sort">Radix sort</a> | <a href="/wiki/Ranking_methods" title="Ranking methods">Ranking methods</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Remove_duplicate_elements">Remove duplicate elements</a> | <a href="/wiki/Sorting_algorithms/Sleep_sort" title="Sorting algorithms/Sleep sort">Sleep sort</a> | <a href="/wiki/Sorting_algorithms/Stooge_sort" title="Sorting algorithms/Stooge sort">Stooge sort</a> | [<a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Sort_the_letters_of_string_in_alphabitical_order">Sort letters of a string</a>] | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Sort_three_variables">Three variable sort</a> | <a rel="nofollow" class="external text" href="https://rosettacode.org/wiki/Topological_sort">Topological sort</a> | <a href="/wiki/Sorting_algorithms/Tree_sort_on_a_linked_list" title="Sorting algorithms/Tree sort on a linked list">Tree sort</a> </p> </center></small></div> <dl><dt>Task</dt> <dd></dd></dl> <p>Sort an <a href="/wiki/Array" title="Array">array</a> (or list) of elements using the Selection sort algorithm. </p><p><br /> It works as follows: </p><p>First find the smallest element in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted. </p><p><br /> Its asymptotic complexity is   <big> <a href="/wiki/O" title="O">O</a>(n<sup>2</sup>) </big>   making it inefficient on large arrays. </p><p>Its primary purpose is for when writing data is very expensive (slow) when compared to reading, eg. writing to flash memory or EEPROM. </p><p>No other sorting algorithm has less data movement. </p><p><br /> </p> <dl><dt>References</dt> <dd></dd></dl> <ul><li>  Rosetta Code:   <a href="/wiki/O" title="O">O</a>     (complexity).</li> <li>  Wikipedia:   <a href="https://en.wikipedia.org/wiki/Selection_sort" class="extiw" title="wp:Selection sort">Selection sort</a>.</li> <li>  Wikipedia:   [<a rel="nofollow" class="external text" href="https://en.wikipedia.org/wiki/Big_O_notation">Big O notation</a>].</li></ul> <p><br /><br /> </p> <meta property="mw:PageProp/toc" /> <h2><span class="mw-headline" id="11l"><a href="/wiki/Category:11l" title="Category:11l">11l</a> </span></h2> <div class="examplemeta translation"><b>Translation of</b>: <a href="#Python">Python</a></div> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>F selection_sort(&lst) L(e) lst V mn = min(L.index .< lst.len, key' x -> @lst[x]) (lst[L.index], lst[mn]) = (lst[mn], e) V arr = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0] selection_sort(&arr) print(arr)</pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] </pre> <h2><span class="mw-headline" id="360_Assembly"><a href="/wiki/Category:360_Assembly" title="Category:360 Assembly">360 Assembly</a> </span></h2> <div class="examplemeta translation"><b>Translation of</b>: <a href="#PL/I">PL/I</a></div> <p>The program uses ASM structured macros and two ASSIST macros to keep the code as short as possible. </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>* Selection sort 26/06/2016 SELECSRT CSECT USING SELECSRT,R13 base register B 72(R15) skip savearea DC 17F'0' savearea STM R14,R12,12(R13) prolog ST R13,4(R15) " ST R15,8(R13) " LR R13,R15 " LA RJ,1 j=1 DO WHILE=(C,RJ,LE,N) do j=1 to n LR RK,RJ k=j LR R1,RJ j SLA R1,2 . LA R3,A-4(R1) @a(j) L RT,0(R3) temp=a(j) LA RI,1(RJ) i=j+1 DO WHILE=(C,RI,LE,N) do i=j+1 to n LR R1,RI i SLA R1,2 . L R2,A-4(R1) a(i) IF CR,RT,GT,R2 THEN if temp>a(i) then LR RT,R2 temp=a(i) LR RK,RI k=i ENDIF , end if LA RI,1(RI) i=i+1 ENDDO , end do L R0,0(R3) a(j) LR R1,RK k SLA R1,2 . ST R0,A-4(R1) a(k)=a(j) ST RT,0(R3) a(j)=temp; LA RJ,1(RJ) j=j+1 ENDDO , end do LA R3,PG pgi=0 LA RI,1 i=1 DO WHILE=(C,RI,LE,N) do i=1 to n LR R1,RI i SLA R1,2 . L R2,A-4(R1) a(i) XDECO R2,XDEC edit a(i) MVC 0(4,R3),XDEC+8 output a(i) LA R3,4(R3) pgi=pgi+4 LA RI,1(RI) i=i+1 ENDDO , end do XPRNT PG,L'PG print buffer L R13,4(0,R13) epilog LM R14,R12,12(R13) " XR R15,R15 " BR R14 exit A DC F'4',F'65',F'2',F'-31',F'0',F'99',F'2',F'83',F'782',F'1' DC F'45',F'82',F'69',F'82',F'104',F'58',F'88',F'112',F'89',F'74' N DC A((N-A)/L'A) number of items of a PG DC CL80' ' buffer XDEC DS CL12 temp for xdeco YREGS RI EQU 6 i RJ EQU 7 j RK EQU 8 k RT EQU 9 temp END SELECSRT</pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre> -31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782 </pre> <h2><span class="mw-headline" id="AArch64_Assembly"><a href="/wiki/Category:AArch64_Assembly" title="Category:AArch64 Assembly">AArch64 Assembly</a> </span></h2> <div class="examplemeta workswith"><b>Works with</b>: <a href="/wiki/As?action=edit&redlink=1" class="new" title="As (page does not exist)">as</a> version Raspberry Pi 3B version Buster 64 bits</div> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>/* ARM assembly AARCH64 Raspberry PI 3B */ /* program selectionSort64.s */ /*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeConstantesARM64.inc" /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .asciz "Value  : @ \n" szCarriageReturn: .asciz "\n" .align 4 #TableNumber: .quad 1,3,6,2,5,9,10,8,4,7 TableNumber: .quad 10,9,8,7,6,5,4,3,2,1 .equ NBELEMENTS, (. - TableNumber) / 8 /*********************************/ /* UnInitialized data */ /*********************************/ .bss sZoneConv: .skip 24 /*********************************/ /* code section */ /*********************************/ .text .global main main: // entry of program ldr x0,qAdrTableNumber // address number table mov x1,0 mov x2,NBELEMENTS // number of élements bl selectionSort ldr x0,qAdrTableNumber // address number table bl displayTable ldr x0,qAdrTableNumber // address number table mov x1,NBELEMENTS // number of élements bl isSorted // control sort cmp x0,1 // sorted ? beq 1f ldr x0,qAdrszMessSortNok // no !! error sort bl affichageMess b 100f 1: // yes ldr x0,qAdrszMessSortOk bl affichageMess 100: // standard end of the program mov x0,0 // return code mov x8,EXIT // request to exit program svc 0 // perform the system call qAdrsZoneConv: .quad sZoneConv qAdrszCarriageReturn: .quad szCarriageReturn qAdrsMessResult: .quad sMessResult qAdrTableNumber: .quad TableNumber qAdrszMessSortOk: .quad szMessSortOk qAdrszMessSortNok: .quad szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the number of elements > 0 */ /* x0 return 0 if not sorted 1 if sorted */ isSorted: stp x2,lr,[sp,-16]! // save registers stp x3,x4,[sp,-16]! // save registers mov x2,0 ldr x4,[x0,x2,lsl 3] 1: add x2,x2,1 cmp x2,x1 bge 99f ldr x3,[x0,x2, lsl 3] cmp x3,x4 blt 98f mov x4,x3 b 1b 98: mov x0,0 // not sorted b 100f 99: mov x0,1 // sorted 100: ldp x3,x4,[sp],16 // restaur 2 registers ldp x2,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* selection sort */ /******************************************************************/ /* x0 contains the address of table */ /* x1 contains the first element */ /* x2 contains the number of element */ selectionSort: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers stp x4,x5,[sp,-16]! // save registers stp x6,x7,[sp,-16]! // save registers mov x3,x1 // start index i sub x7,x2,1 // compute n - 1 1: // start loop mov x4,x3 add x5,x3,1 // init index 2 2: ldr x1,[x0,x4,lsl 3] // load value A[mini] ldr x6,[x0,x5,lsl 3] // load value A[j] cmp x6,x1 // compare value csel x4,x5,x4,lt // j -> mini add x5,x5,1 // increment index j cmp x5,x2 // end ? blt 2b // no -> loop cmp x4,x3 // mini <> j ? beq 3f // no ldr x1,[x0,x4,lsl 3] // yes swap A[i] A[mini] ldr x6,[x0,x3,lsl 3] str x1,[x0,x3,lsl 3] str x6,[x0,x4,lsl 3] 3: add x3,x3,1 // increment i cmp x3,x7 // end ? blt 1b // no -> loop 100: ldp x6,x7,[sp],16 // restaur 2 registers ldp x4,x5,[sp],16 // restaur 2 registers ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /******************************************************************/ /* Display table elements */ /******************************************************************/ /* x0 contains the address of table */ displayTable: stp x1,lr,[sp,-16]! // save registers stp x2,x3,[sp,-16]! // save registers mov x2,x0 // table address mov x3,0 1: // loop display table ldr x0,[x2,x3,lsl 3] ldr x1,qAdrsZoneConv bl conversion10 // décimal conversion ldr x0,qAdrsMessResult ldr x1,qAdrsZoneConv bl strInsertAtCharInc // insert result at @ character bl affichageMess // display message add x3,x3,1 cmp x3,NBELEMENTS - 1 ble 1b ldr x0,qAdrszCarriageReturn bl affichageMess 100: ldp x2,x3,[sp],16 // restaur 2 registers ldp x1,lr,[sp],16 // restaur 2 registers ret // return to address lr x30 /********************************************************/ /* File Include fonctions */ /********************************************************/ /* for this file see task include a file in language AArch64 assembly */ .include "../includeARM64.inc"</pre></div> <h2><span id="Action.21"></span><span class="mw-headline" id="Action!"><a href="/wiki/Category:Action!" title="Category:Action!">Action!</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>PROC PrintArray(INT ARRAY a INT size) INT i Put('[) FOR i=0 TO size-1 DO IF i>0 THEN Put(' ) FI PrintI(a(i)) OD Put(']) PutE() RETURN PROC SelectionSort(INT ARRAY a INT size) INT i,j,minpos,tmp FOR i=0 TO size-2 DO minpos=i FOR j=i+1 TO size-1 DO IF a(minpos)>a(j) THEN minpos=j FI OD IF minpos#i THEN tmp=a(i) a(i)=a(minpos) a(minpos)=tmp FI OD RETURN PROC Test(INT ARRAY a INT size) PrintE("Array before sort:") PrintArray(a,size) SelectionSort(a,size) PrintE("Array after sort:") PrintArray(a,size) PutE() RETURN PROC Main() INT ARRAY a(10)=[1 4 65535 0 3 7 4 8 20 65530], b(21)=[10 9 8 7 6 5 4 3 2 1 0 65535 65534 65533 65532 65531 65530 65529 65528 65527 65526], c(8)=[101 102 103 104 105 106 107 108], d(12)=[1 65535 1 65535 1 65535 1 65535 1 65535 1 65535] Test(a,10) Test(b,21) Test(c,8) Test(d,12) RETURN</pre></div> <div> <dl><dt>Output:</dt></dl></div> <p><a rel="nofollow" class="external text" href="https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Selection_sort.png">Screenshot from Atari 8-bit computer</a> </p> <pre>Array before sort: [1 4 -1 0 3 7 4 8 20 -6] Array after sort: [-6 -1 0 1 3 4 4 7 8 20] Array before sort: [10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10] Array after sort: [-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10] Array before sort: [101 102 103 104 105 106 107 108] Array after sort: [101 102 103 104 105 106 107 108] Array before sort: [1 -1 1 -1 1 -1 1 -1 1 -1 1 -1] Array after sort: [-1 -1 -1 -1 -1 -1 1 1 1 1 1 1] </pre> <h2><span class="mw-headline" id="ActionScript"><a href="/wiki/Category:ActionScript" title="Category:ActionScript">ActionScript</a> </span></h2> <div class="mw-highlight mw-highlight-lang-actionscript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">selectionSort</span><span class="p">(</span><span class="nx">input</span><span class="o">:</span><span class="w"> </span><span class="nb">Array</span><span class="p">)</span><span class="o">:</span><span class="nb">Array</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="c1">//find the i'th element</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="k">var</span><span class="w"> </span><span class="nx">i</span><span class="o">:</span><span class="nb">uint</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="o">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">input</span><span class="p">.</span><span class="nx">length</span><span class="o">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="c1">//set minIndex to an arbitrary value</span> <span class="w"> </span><span class="k">var</span><span class="w"> </span><span class="nx">minIndex</span><span class="o">:</span><span class="nb">uint</span><span class="o">=</span><span class="nx">i</span><span class="o">;</span> <span class="w"> </span><span class="c1">//find the smallest number</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="k">var</span><span class="w"> </span><span class="nx">j</span><span class="o">:</span><span class="nb">uint</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">i</span><span class="o">;</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">input</span><span class="p">.</span><span class="nx">length</span><span class="o">;</span><span class="w"> </span><span class="nx">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">input</span><span class="p">[</span><span class="nx">j</span><span class="p">]</span><span class="o"><</span><span class="nx">input</span><span class="p">[</span><span class="nx">minIndex</span><span class="p">])</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">minIndex</span><span class="o">=</span><span class="nx">j</span><span class="o">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="c1">//swap the smallest number into place</span> <span class="w"> </span><span class="k">var</span><span class="w"> </span><span class="nx">tmp</span><span class="o">:</span><span class="nb">Number</span><span class="o">=</span><span class="nx">input</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span> <span class="w"> </span><span class="nx">input</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="o">=</span><span class="nx">input</span><span class="p">[</span><span class="nx">minIndex</span><span class="p">];</span> <span class="w"> </span><span class="nx">input</span><span class="p">[</span><span class="nx">minIndex</span><span class="p">]</span><span class="o">=</span><span class="nx">tmp</span><span class="o">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">input</span><span class="o">;</span> <span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="Ada"><a href="/wiki/Category:Ada" title="Category:Ada">Ada</a> </span></h2> <div class="mw-highlight mw-highlight-lang-ada mw-content-ltr" dir="ltr"><pre><span></span><span class="kn">with</span> <span class="n">Ada.Text_IO</span><span class="p">;</span> <span class="kn">use</span> <span class="n">Ada.Text_IO</span><span class="p">;</span> <span class="kd">procedure</span> <span class="nf">Test_Selection_Sort</span> <span class="kr">is</span> <span class="kd">type</span> <span class="kt">Integer_Array</span> <span class="kr">is</span> <span class="kr">array</span> <span class="p">(</span><span class="kt">Positive</span> <span class="kr">range</span> <span class="p"><>)</span> <span class="kr">of</span> <span class="kt">Integer</span><span class="p">;</span> <span class="kd">procedure</span> <span class="nf">Sort</span> <span class="p">(</span><span class="nv">A</span> <span class="p">: </span><span class="nv">in</span> <span class="nv">out</span> <span class="nv">Integer_Array</span><span class="p">)</span> <span class="kr">is</span> <span class="n">Min</span> <span class="p">:</span> <span class="kt">Positive</span><span class="p">;</span> <span class="n">Temp</span> <span class="p">:</span> <span class="kt">Integer</span><span class="p">;</span> <span class="kr">begin</span> <span class="kr">for</span> <span class="n">I</span> <span class="ow">in</span> <span class="n">A</span><span class="p">'</span><span class="na">First</span><span class="p">..</span><span class="n">A</span><span class="p">'</span><span class="na">Last</span> <span class="o">-</span> <span class="mi">1</span> <span class="kr">loop</span> <span class="n">Min</span> <span class="p">:=</span> <span class="n">I</span><span class="p">;</span> <span class="kr">for</span> <span class="n">J</span> <span class="ow">in</span> <span class="n">I</span> <span class="o">+</span> <span class="mf">1.</span><span class="p">.</span><span class="n">A</span><span class="p">'</span><span class="na">Last</span> <span class="kr">loop</span> <span class="kr">if</span> <span class="n">A</span> <span class="p">(</span><span class="n">Min</span><span class="p">)</span> <span class="o">></span> <span class="n">A</span> <span class="p">(</span><span class="n">J</span><span class="p">)</span> <span class="kr">then</span> <span class="n">Min</span> <span class="p">:=</span> <span class="n">J</span><span class="p">;</span> <span class="kr">end</span> <span class="kr">if</span><span class="p">;</span> <span class="kr">end</span> <span class="kr">loop</span><span class="p">;</span> <span class="kr">if</span> <span class="n">Min</span> <span class="o">/=</span> <span class="n">I</span> <span class="kr">then</span> <span class="n">Temp</span> <span class="p">:=</span> <span class="n">A</span> <span class="p">(</span><span class="n">I</span><span class="p">);</span> <span class="n">A</span> <span class="p">(</span><span class="n">I</span><span class="p">)</span> <span class="p">:=</span> <span class="n">A</span> <span class="p">(</span><span class="n">Min</span><span class="p">);</span> <span class="n">A</span> <span class="p">(</span><span class="n">Min</span><span class="p">)</span> <span class="p">:=</span> <span class="n">Temp</span><span class="p">;</span> <span class="kr">end</span> <span class="kr">if</span><span class="p">;</span> <span class="kr">end</span> <span class="kr">loop</span><span class="p">;</span> <span class="kr">end</span> <span class="nf">Sort</span><span class="p">;</span> <span class="n">A</span> <span class="p">:</span> <span class="n">Integer_Array</span> <span class="p">:=</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="o">-</span><span class="mi">5</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">8</span><span class="p">);</span> <span class="kr">begin</span> <span class="n">Sort</span> <span class="p">(</span><span class="n">A</span><span class="p">);</span> <span class="kr">for</span> <span class="n">I</span> <span class="ow">in</span> <span class="n">A</span><span class="p">'</span><span class="na">Range</span> <span class="kr">loop</span> <span class="n">Put</span> <span class="p">(</span><span class="kt">Integer</span><span class="p">'</span><span class="na">Image</span> <span class="p">(</span><span class="n">A</span> <span class="p">(</span><span class="n">I</span><span class="p">))</span> <span class="o">&</span> <span class="s">" "</span><span class="p">);</span> <span class="kr">end</span> <span class="kr">loop</span><span class="p">;</span> <span class="kr">end</span> <span class="nf">Test_Selection_Sort</span><span class="p">;</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>-5 -2 0 1 3 4 6 7 8 9 </pre> <h2><span class="mw-headline" id="ALGOL_68"><a href="/wiki/Category:ALGOL_68" title="Category:ALGOL 68">ALGOL 68</a> </span></h2> <div class="examplemeta translation"><b>Translation of</b>: <a href="#Ada">Ada</a></div> <div class="examplemeta workswith"><b>Works with</b>: <a href="/wiki/ALGOL_68" class="mw-redirect" title="ALGOL 68">ALGOL 68</a> version Standard - no extensions to language used</div> <div class="examplemeta workswith"><b>Works with</b>: <a href="/wiki/ALGOL_68G" class="mw-redirect" title="ALGOL 68G">ALGOL 68G</a> version Any - tested with release mk15-0.8b.fc9.i386</div> <div class="examplemeta workswith"><b>Works with</b>: <a href="/wiki/ELLA_ALGOL_68" class="mw-redirect" title="ELLA ALGOL 68">ELLA ALGOL 68</a> version Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386</div> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>MODE DATA = REF CHAR; PROC in place selection sort = (REF[]DATA a)VOID: BEGIN INT min; DATA temp; FOR i FROM LWB a TO UPB a DO min := i; FOR j FROM i + 1 TO UPB a DO IF a [min] > a [j] THEN min := j FI OD; IF min /= i THEN temp  := a [i]; a [i]  := a [min]; a [min] := temp FI OD END # in place selection sort #; [32]CHAR data := "big fjords vex quick waltz nymph"; [UPB data]DATA ref data; FOR i TO UPB data DO ref data[i] := data[i] OD; in place selection sort(ref data); FOR i TO UPB ref data DO print(ref data[i]) OD; print(new line); print((data))</pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre> abcdefghiijklmnopqrstuvwxyz big fjords vex quick waltz nymph </pre> <h2><span class="mw-headline" id="AppleScript"><a href="/wiki/Category:AppleScript" title="Category:AppleScript">AppleScript</a> </span></h2> <div class="mw-highlight mw-highlight-lang-applescript mw-content-ltr" dir="ltr"><pre><span></span><span class="k">on</span> <span class="nv">selectionSort</span><span class="p">(</span><span class="nv">theList</span><span class="p">,</span> <span class="nv">l</span><span class="p">,</span> <span class="nv">r</span><span class="p">)</span> <span class="c">-- Sort items l thru r of theList in place.</span> <span class="k">set</span> <span class="nv">listLength</span> <span class="k">to</span> <span class="p">(</span><span class="nb">count</span> <span class="nv">theList</span><span class="p">)</span> <span class="k">if</span> <span class="p">(</span><span class="nv">listLength</span> <span class="o"><</span> <span class="mi">2</span><span class="p">)</span> <span class="k">then</span> <span class="no">return</span> <span class="c">-- Convert negative and/or transposed range indices.</span> <span class="k">if</span> <span class="p">(</span><span class="nv">l</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="k">then</span> <span class="k">set</span> <span class="nv">l</span> <span class="k">to</span> <span class="nv">listLength</span> <span class="o">+</span> <span class="nv">l</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">if</span> <span class="p">(</span><span class="nv">r</span> <span class="o"><</span> <span class="mi">0</span><span class="p">)</span> <span class="k">then</span> <span class="k">set</span> <span class="nv">r</span> <span class="k">to</span> <span class="nv">listLength</span> <span class="o">+</span> <span class="nv">r</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">if</span> <span class="p">(</span><span class="nv">l</span> <span class="o">></span> <span class="nv">r</span><span class="p">)</span> <span class="k">then</span> <span class="k">set</span> <span class="p">{</span><span class="nv">l</span><span class="p">,</span> <span class="nv">r</span><span class="p">}</span> <span class="k">to</span> <span class="p">{</span><span class="nv">r</span><span class="p">,</span> <span class="nv">l</span><span class="p">}</span> <span class="nv">script</span> <span class="nv">o</span> <span class="k">property</span> <span class="nv">lst</span> <span class="p">:</span> <span class="nv">theList</span> <span class="k">end</span> <span class="nv">script</span> <span class="k">repeat</span> <span class="nv">with</span> <span class="nv">i</span> <span class="k">from</span> <span class="nv">l</span> <span class="k">to</span> <span class="p">(</span><span class="nv">r</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="k">set</span> <span class="nv">iVal</span> <span class="k">to</span> <span class="nv">o</span>'s <span class="nv">lst</span>'s <span class="nb">item</span> <span class="nv">i</span> <span class="k">set</span> <span class="nv">minVal</span> <span class="k">to</span> <span class="nv">iVal</span> <span class="k">set</span> <span class="nv">minPos</span> <span class="k">to</span> <span class="nv">i</span> <span class="k">repeat</span> <span class="nv">with</span> <span class="nv">j</span> <span class="k">from</span> <span class="p">(</span><span class="nv">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="k">to</span> <span class="nv">r</span> <span class="k">set</span> <span class="nv">jVal</span> <span class="k">to</span> <span class="nv">o</span>'s <span class="nv">lst</span>'s <span class="nb">item</span> <span class="nv">j</span> <span class="k">if</span> <span class="p">(</span><span class="nv">minVal</span> <span class="o">></span> <span class="nv">jVal</span><span class="p">)</span> <span class="k">then</span> <span class="k">set</span> <span class="nv">minVal</span> <span class="k">to</span> <span class="nv">jVal</span> <span class="k">set</span> <span class="nv">minPos</span> <span class="k">to</span> <span class="nv">j</span> <span class="k">end</span> <span class="k">if</span> <span class="k">end</span> <span class="k">repeat</span> <span class="k">set</span> <span class="nv">o</span>'s <span class="nv">lst</span>'s <span class="nb">item</span> <span class="nv">minPos</span> <span class="k">to</span> <span class="nv">iVal</span> <span class="k">set</span> <span class="nv">o</span>'s <span class="nv">lst</span>'s <span class="nb">item</span> <span class="nv">i</span> <span class="k">to</span> <span class="nv">minVal</span> <span class="k">end</span> <span class="k">repeat</span> <span class="no">return</span> <span class="c">-- nothing.</span> <span class="k">end</span> <span class="nv">selectionSort</span> <span class="k">property</span> <span class="nv">sort</span> <span class="p">:</span> <span class="nv">selectionSort</span> <span class="k">on</span> <span class="nv">demo</span><span class="p">()</span> <span class="k">set</span> <span class="nv">theList</span> <span class="k">to</span> <span class="p">{</span><span class="mi">988</span><span class="p">,</span> <span class="mi">906</span><span class="p">,</span> <span class="mi">151</span><span class="p">,</span> <span class="mi">71</span><span class="p">,</span> <span class="mi">712</span><span class="p">,</span> <span class="mi">177</span><span class="p">,</span> <span class="mi">945</span><span class="p">,</span> <span class="mi">558</span><span class="p">,</span> <span class="mi">31</span><span class="p">,</span> <span class="mi">627</span><span class="p">}</span> <span class="nv">sort</span><span class="p">(</span><span class="nv">theList</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="no">return</span> <span class="nv">theList</span> <span class="k">end</span> <span class="nv">demo</span> <span class="nv">demo</span><span class="p">()</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <div class="mw-highlight mw-highlight-lang-applescript mw-content-ltr" dir="ltr"><pre><span></span><span class="p">{</span><span class="mi">31</span><span class="p">,</span> <span class="mi">71</span><span class="p">,</span> <span class="mi">151</span><span class="p">,</span> <span class="mi">177</span><span class="p">,</span> <span class="mi">558</span><span class="p">,</span> <span class="mi">627</span><span class="p">,</span> <span class="mi">712</span><span class="p">,</span> <span class="mi">906</span><span class="p">,</span> <span class="mi">945</span><span class="p">,</span> <span class="mi">988</span><span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="ARM_Assembly"><a href="/wiki/Category:ARM_Assembly" title="Category:ARM Assembly">ARM Assembly</a> </span></h2> <div class="examplemeta workswith"><b>Works with</b>: <a href="/wiki/As?action=edit&redlink=1" class="new" title="As (page does not exist)">as</a> version Raspberry Pi</div> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>/* ARM assembly Raspberry PI */ /* program selectionSort.s */ /************************************/ /* Constantes */ /************************************/ .equ STDOUT, 1 @ Linux output console .equ EXIT, 1 @ Linux syscall .equ WRITE, 4 @ Linux syscall /*********************************/ /* Initialized data */ /*********************************/ .data szMessSortOk: .asciz "Table sorted.\n" szMessSortNok: .asciz "Table not sorted !!!!!.\n" sMessResult: .ascii "Value  : " sMessValeur: .fill 11, 1, ' ' @ size => 11 szCarriageReturn: .asciz "\n" .align 4 iGraine: .int 123456 .equ NBELEMENTS, 10 #TableNumber: .int 1,3,6,2,5,9,10,8,4,7 TableNumber: .int 10,9,8,7,6,5,4,3,2,1 /*********************************/ /* UnInitialized data */ /*********************************/ .bss /*********************************/ /* code section */ /*********************************/ .text .global main main: @ entry of program 1: ldr r0,iAdrTableNumber @ address number table mov r1,#0 mov r2,#NBELEMENTS @ number of élements bl selectionSort ldr r0,iAdrTableNumber @ address number table bl displayTable ldr r0,iAdrTableNumber @ address number table mov r1,#NBELEMENTS @ number of élements bl isSorted @ control sort cmp r0,#1 @ sorted ? beq 2f ldr r0,iAdrszMessSortNok @ no !! error sort bl affichageMess b 100f 2: @ yes ldr r0,iAdrszMessSortOk bl affichageMess 100: @ standard end of the program mov r0, #0 @ return code mov r7, #EXIT @ request to exit program svc #0 @ perform the system call iAdrsMessValeur: .int sMessValeur iAdrszCarriageReturn: .int szCarriageReturn iAdrsMessResult: .int sMessResult iAdrTableNumber: .int TableNumber iAdrszMessSortOk: .int szMessSortOk iAdrszMessSortNok: .int szMessSortNok /******************************************************************/ /* control sorted table */ /******************************************************************/ /* r0 contains the address of table */ /* r1 contains the number of elements > 0 */ /* r0 return 0 if not sorted 1 if sorted */ isSorted: push {r2-r4,lr} @ save registers mov r2,#0 ldr r4,[r0,r2,lsl #2] 1: add r2,#1 cmp r2,r1 movge r0,#1 bge 100f ldr r3,[r0,r2, lsl #2] cmp r3,r4 movlt r0,#0 blt 100f mov r4,r3 b 1b 100: pop {r2-r4,lr} bx lr @ return /******************************************************************/ /* selection sort */ /******************************************************************/ /* r0 contains the address of table */ /* r1 contains the first element */ /* r2 contains the number of element */ selectionSort: push {r1-r7,lr} @ save registers mov r3,r1 @ start index i sub r7,r2,#1 @ compute n - 1 1: @ start loop mov r4,r3 add r5,r3,#1 @ init index 2 2: ldr r1,[r0,r4,lsl #2] @ load value A[mini] ldr r6,[r0,r5,lsl #2] @ load value A[j] cmp r6,r1 @ compare value movlt r4,r5 @ j -> mini add r5,#1 @ increment index j cmp r5,r2 @ end ? blt 2b @ no -> loop cmp r4,r3 @ mini <> j ? beq 3f @ no ldr r1,[r0,r4,lsl #2] @ yes swap A[i] A[mini] ldr r6,[r0,r3,lsl #2] str r1,[r0,r3,lsl #2] str r6,[r0,r4,lsl #2] 3: add r3,#1 @ increment i cmp r3,r7 @ end ? blt 1b @ no -> loop 100: pop {r1-r7,lr} bx lr @ return /******************************************************************/ /* Display table elements */ /******************************************************************/ /* r0 contains the address of table */ displayTable: push {r0-r3,lr} @ save registers mov r2,r0 @ table address mov r3,#0 1: @ loop display table ldr r0,[r2,r3,lsl #2] ldr r1,iAdrsMessValeur @ display value bl conversion10 @ call function ldr r0,iAdrsMessResult bl affichageMess @ display message add r3,#1 cmp r3,#NBELEMENTS - 1 ble 1b ldr r0,iAdrszCarriageReturn bl affichageMess 100: pop {r0-r3,lr} bx lr /******************************************************************/ /* display text with size calculation */ /******************************************************************/ /* r0 contains the address of the message */ affichageMess: push {r0,r1,r2,r7,lr} @ save registres mov r2,#0 @ counter length 1: @ loop length calculation ldrb r1,[r0,r2] @ read octet start position + index cmp r1,#0 @ if 0 its over addne r2,r2,#1 @ else add 1 in the length bne 1b @ and loop @ so here r2 contains the length of the message mov r1,r0 @ address message in r1 mov r0,#STDOUT @ code to write to the standard output Linux mov r7, #WRITE @ code call system "write" svc #0 @ call systeme pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */ bx lr @ return /******************************************************************/ /* Converting a register to a decimal unsigned */ /******************************************************************/ /* r0 contains value and r1 address area */ /* r0 return size of result (no zero final in area) */ /* area size => 11 bytes */ .equ LGZONECAL, 10 conversion10: push {r1-r4,lr} @ save registers mov r3,r1 mov r2,#LGZONECAL 1: @ start loop bl divisionpar10U @ unsigned r0 <- dividende. quotient ->r0 reste -> r1 add r1,#48 @ digit strb r1,[r3,r2] @ store digit on area cmp r0,#0 @ stop if quotient = 0 subne r2,#1 @ else previous position bne 1b @ and loop @ and move digit from left of area mov r4,#0 2: ldrb r1,[r3,r2] strb r1,[r3,r4] add r2,#1 add r4,#1 cmp r2,#LGZONECAL ble 2b @ and move spaces in end on area mov r0,r4 @ result length mov r1,#' ' @ space 3: strb r1,[r3,r4] @ store space in area add r4,#1 @ next position cmp r4,#LGZONECAL ble 3b @ loop if r4 <= area size 100: pop {r1-r4,lr} @ restaur registres bx lr @return /***************************************************/ /* division par 10 unsigned */ /***************************************************/ /* r0 dividende */ /* r0 quotient */ /* r1 remainder */ divisionpar10U: push {r2,r3,r4, lr} mov r4,r0 @ save value //mov r3,#0xCCCD @ r3 <- magic_number lower raspberry 3 //movt r3,#0xCCCC @ r3 <- magic_number higter raspberry 3 ldr r3,iMagicNumber @ r3 <- magic_number raspberry 1 2 umull r1, r2, r3, r0 @ r1<- Lower32Bits(r1*r0) r2<- Upper32Bits(r1*r0) mov r0, r2, LSR #3 @ r2 <- r2 >> shift 3 add r2,r0,r0, lsl #2 @ r2 <- r0 * 5 sub r1,r4,r2, lsl #1 @ r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) pop {r2,r3,r4,lr} bx lr @ leave function iMagicNumber: .int 0xCCCCCCCD</pre></div> <h2><span class="mw-headline" id="Arturo"><a href="/wiki/Category:Arturo" title="Category:Arturo">Arturo</a> </span></h2> <div class="mw-highlight mw-highlight-lang-rebol mw-content-ltr" dir="ltr"><pre><span></span><span class="gu">selectionSort:</span> <span class="kn">function</span> <span class="gs">[</span><span class="nv">items</span><span class="gs">][</span> <span class="gu">sorted:</span> <span class="nv">new</span> <span class="gs">[]</span> <span class="gu">tmp:</span> <span class="nv">new</span> <span class="nv">items</span> <span class="nb">while</span> <span class="gs">[</span><span class="k">not?</span> <span class="k">empty?</span> <span class="nv">tmp</span><span class="gs">][</span> <span class="gu">minIndex:</span> <span class="nv">index</span> <span class="nv">tmp</span> <span class="nf">min</span> <span class="nv">tmp</span> <span class="vi">'sorted</span> <span class="nv">++</span> <span class="nv">tmp\</span><span class="gs">[</span><span class="nv">minIndex</span><span class="gs">]</span> <span class="nf">remove</span> <span class="vi">'tmp</span> <span class="nv">.index</span> <span class="nv">minIndex</span> <span class="gs">]</span> <span class="ne">return</span> <span class="nv">sorted</span> <span class="gs">]</span> <span class="nb">print</span> <span class="nv">selectionSort</span> <span class="gs">[</span><span class="m">3</span> <span class="m">1</span> <span class="m">2</span> <span class="m">8</span> <span class="m">5</span> <span class="m">7</span> <span class="m">9</span> <span class="m">4</span> <span class="m">6</span><span class="gs">]</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>1 2 3 4 5 6 7 8 9</pre> <h2><span class="mw-headline" id="AutoHotkey"><a href="/wiki/Category:AutoHotkey" title="Category:AutoHotkey">AutoHotkey</a> </span></h2> <p>ahk forum: <a rel="nofollow" class="external text" href="http://www.autohotkey.com/forum/topic44657-105.html">discussion</a> </p> <div class="mw-highlight mw-highlight-lang-autohotkey mw-content-ltr" dir="ltr"><pre><span></span><span class="nb">MsgBox</span> <span class="o">%</span> <span class="n">SelecSort</span><span class="p">(</span><span class="s">""</span><span class="p">)</span> <span class="nb">MsgBox</span> <span class="o">%</span> <span class="n">SelecSort</span><span class="p">(</span><span class="s">"xxx"</span><span class="p">)</span> <span class="nb">MsgBox</span> <span class="o">%</span> <span class="n">SelecSort</span><span class="p">(</span><span class="s">"3,2,1"</span><span class="p">)</span> <span class="nb">MsgBox</span> <span class="o">%</span> <span class="n">SelecSort</span><span class="p">(</span><span class="s">"dog,000000,xx,cat,pile,abcde,1,cat,zz,xx,z"</span><span class="p">)</span> <span class="n">SelecSort</span><span class="p">(</span><span class="n">var</span><span class="p">)</span> <span class="p">{</span><span class="c1">  ; SORT COMMA SEPARATED LIST</span> <span class="nb">StringSplit</span> <span class="n">a</span><span class="p">,</span> <span class="n">var</span><span class="p">,</span> <span class="se">`,</span><span class="c1">  ; make array, size = a0</span> <span class="nb">Loop</span> <span class="o">%</span> <span class="n">a0</span><span class="o">-</span><span class="mi">1</span> <span class="p">{</span> <span class="n">i</span> <span class="o">:=</span> <span class="nv">A_Index</span><span class="p">,</span> <span class="n">mn</span> <span class="o">:=</span> <span class="n">a</span><span class="nv">%i%</span><span class="p">,</span> <span class="n">j</span> <span class="o">:=</span> <span class="n">m</span> <span class="o">:=</span> <span class="n">i</span> <span class="nb">Loop</span> <span class="o">%</span> <span class="n">a0</span><span class="o">-</span><span class="n">i</span> <span class="p">{</span><span class="c1">  ; find minimum</span> <span class="n">j</span><span class="o">++</span> <span class="n">If</span> <span class="p">(</span><span class="n">a</span><span class="nv">%j%</span> <span class="o"><</span> <span class="n">mn</span><span class="p">)</span> <span class="n">mn</span> <span class="o">:=</span> <span class="n">a</span><span class="nv">%j%</span><span class="p">,</span> <span class="n">m</span> <span class="o">:=</span> <span class="n">j</span> <span class="p">}</span> <span class="n">t</span> <span class="o">:=</span> <span class="n">a</span><span class="nv">%i%</span><span class="p">,</span> <span class="n">a</span><span class="nv">%i%</span> <span class="o">:=</span> <span class="n">a</span><span class="nv">%m%</span><span class="p">,</span> <span class="n">a</span><span class="nv">%m%</span> <span class="o">:=</span> <span class="n">t</span><span class="c1"> ; swap first with minimum</span> <span class="p">}</span> <span class="nb">Loop</span> <span class="o">%</span> <span class="n">a0</span><span class="c1">  ; construct string from sorted array</span> <span class="n">sorted</span> <span class="o">.=</span> <span class="s">","</span> <span class="o">.</span> <span class="n">a</span><span class="nv">%A_Index%</span> <span class="nb">Return</span> <span class="nf">SubStr</span><span class="p">(</span><span class="n">sorted</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span><span class="c1">  ; drop leading comma</span> <span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="AWK"><a href="/wiki/Category:AWK" title="Category:AWK">AWK</a> </span></h2> <div class="mw-highlight mw-highlight-lang-awk mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span> <span class="nx">getminindex</span><span class="p">(</span><span class="nx">gl</span><span class="p">,</span> <span class="nx">gi</span><span class="p">,</span> <span class="nx">gs</span><span class="p">)</span> <span class="p">{</span> <span class="nx">min</span> <span class="o">=</span> <span class="nx">gl</span><span class="p">[</span><span class="nx">gi</span><span class="p">]</span> <span class="nx">gm</span> <span class="o">=</span> <span class="nx">gi</span> <span class="k">for</span><span class="p">(</span><span class="nx">gj</span><span class="o">=</span><span class="nx">gi</span><span class="p">;</span> <span class="nx">gj</span> <span class="o"><=</span> <span class="nx">gs</span><span class="p">;</span> <span class="nx">gj</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span> <span class="nx">gl</span><span class="p">[</span><span class="nx">gj</span><span class="p">]</span> <span class="o"><</span> <span class="nx">min</span> <span class="p">)</span> <span class="p">{</span> <span class="nx">min</span> <span class="o">=</span> <span class="nx">gl</span><span class="p">[</span><span class="nx">gj</span><span class="p">]</span> <span class="nx">gm</span> <span class="o">=</span> <span class="nx">gj</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="nx">gm</span> <span class="p">}</span> <span class="p">{</span> <span class="nx">line</span><span class="p">[</span><span class="nb">NR</span><span class="p">]</span> <span class="o">=</span> <span class="o">$</span><span class="mi">0</span> <span class="p">}</span> <span class="nb">END</span> <span class="p">{</span> <span class="c1"># sort it with selection sort</span> <span class="k">for</span><span class="p">(</span><span class="nx">i</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><=</span> <span class="nb">NR</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="nx">mi</span> <span class="o">=</span> <span class="nx">getminindex</span><span class="p">(</span><span class="nx">line</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nb">NR</span><span class="p">)</span> <span class="nx">t</span> <span class="o">=</span> <span class="nx">line</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="nx">line</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">line</span><span class="p">[</span><span class="nx">mi</span><span class="p">];</span> <span class="nx">line</span><span class="p">[</span><span class="nx">mi</span><span class="p">]</span> <span class="o">=</span> <span class="nx">t</span> <span class="p">}</span> <span class="c1">#print it</span> <span class="k">for</span><span class="p">(</span><span class="nx">i</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o"><=</span> <span class="nb">NR</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="kr">print</span> <span class="nx">line</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="BBC_BASIC"><a href="/wiki/Category:BBC_BASIC" title="Category:BBC BASIC">BBC BASIC</a> </span></h2> <div class="mw-highlight mw-highlight-lang-bbcbasic mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">DEF</span><span class="w"> </span><span class="kd">PROC</span><span class="nf">_SelectionSort</span><span class="o">(</span><span class="nv">Size%</span><span class="o">)</span> <span class="k">FOR</span><span class="w"> </span><span class="nv">I%</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">TO</span><span class="w"> </span><span class="nv">Size%</span><span class="mi">-1</span> <span class="w"> </span><span class="nv">lowest%</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">I%</span> <span class="w"> </span><span class="k">FOR</span><span class="w"> </span><span class="nv">J%</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">(</span><span class="nv">I%</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="o">)</span><span class="w"> </span><span class="k">TO</span><span class="w"> </span><span class="nv">Size%</span> <span class="w"> </span><span class="k">IF</span><span class="w"> </span><span class="nv">data%</span><span class="o">(</span><span class="nv">J%</span><span class="o">)</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nv">data%</span><span class="o">(</span><span class="nv">lowest%</span><span class="o">)</span><span class="w"> </span><span class="nv">lowest%</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">J%</span> <span class="w"> </span><span class="k">NEXT</span><span class="w"> </span><span class="nv">J%</span> <span class="w"> </span><span class="k">IF</span><span class="w"> </span><span class="nv">I%</span><span class="o"><></span><span class="nv">lowest%</span><span class="w"> </span><span class="k">SWAP</span><span class="w"> </span><span class="nv">data%</span><span class="o">(</span><span class="nv">I%</span><span class="o">),</span><span class="nv">data%</span><span class="o">(</span><span class="nv">lowest%</span><span class="o">)</span> <span class="k">NEXT</span><span class="w"> </span><span class="nv">I%</span> <span class="k">ENDPROC</span> </pre></div> <h2><span class="mw-headline" id="BASIC"><a href="/wiki/Category:BASIC" title="Category:BASIC">BASIC</a> </span></h2> <h3><span class="mw-headline" id="GWBASIC"><a href="/wiki/Category:GWBASIC?action=edit&redlink=1" class="new" title="Category:GWBASIC (page does not exist)">GWBASIC</a> </span></h3> <p>Works with: QBASIC, QuickBASIC, VB-DOS </p> <div class="mw-highlight mw-highlight-lang-basic mw-content-ltr" dir="ltr"><pre><span></span><span class="nl">10</span><span class="w"> </span><span class="c1">'SAVE"SELSORT",A</span> <span class="nl">20</span><span class="w"> </span><span class="c1">' Selection Sort Algorithm</span> <span class="nl">30</span><span class="w"> </span><span class="c1">'</span> <span class="nl">40</span><span class="w"> </span><span class="c1">' VAR</span> <span class="nl">50</span><span class="w"> </span><span class="kr">DEFINT</span><span class="w"> </span><span class="vg">A</span><span class="o">-</span><span class="vg">Z</span> <span class="nl">60</span><span class="w"> </span><span class="kr">OPTION BASE</span><span class="w"> </span><span class="il">1</span> <span class="nl">70</span><span class="w"> </span><span class="vg">I</span><span class="o">=</span><span class="il">0</span><span class="o">:</span><span class="w"> </span><span class="vg">J</span><span class="o">=</span><span class="il">0</span><span class="o">:</span><span class="w"> </span><span class="vg">IMINV</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">0</span><span class="o">:</span><span class="w"> </span><span class="vg">IMAX</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">0</span><span class="o">:</span><span class="w"> </span><span class="vg">TP!</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">0</span><span class="o">:</span><span class="w"> </span><span class="vg">TL!</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">0</span> <span class="nl">80</span><span class="w"> </span><span class="c1">'</span> <span class="nl">90</span><span class="w"> </span><span class="kr">CLS</span> <span class="nl">100</span><span class="w"> </span><span class="kr">PRINT</span><span class="w"> </span><span class="s2">"This program does the Selection Sort Algorithm"</span> <span class="nl">110</span><span class="w"> </span><span class="kr">INPUT</span><span class="w"> </span><span class="s2">"Number of elements to sort (Max=1000, Enter=10)"</span><span class="p">;</span><span class="vg">IMAX</span> <span class="nl">120</span><span class="w"> </span><span class="kr">IF</span><span class="w"> </span><span class="vg">IMAX</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">0</span><span class="w"> </span><span class="kr">THEN</span><span class="w"> </span><span class="vg">IMAX</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">10</span> <span class="nl">130</span><span class="w"> </span><span class="kr">IF</span><span class="w"> </span><span class="vg">IMAX</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="il">1000</span><span class="w"> </span><span class="kr">THEN</span><span class="w"> </span><span class="vg">IMAX</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">1000</span> <span class="nl">140</span><span class="w"> </span><span class="kd">DIM</span><span class="w"> </span><span class="vg">N</span><span class="p">(</span><span class="vg">IMAX</span><span class="p">)</span> <span class="nl">150</span><span class="w"> </span><span class="c1">' Creates and shows the unsorted list</span> <span class="nl">160</span><span class="w"> </span><span class="kr">RANDOMIZE</span><span class="w"> </span><span class="kr">TIMER</span> <span class="nl">170</span><span class="w"> </span><span class="kr">FOR</span><span class="w"> </span><span class="vg">I</span><span class="o">=</span><span class="il">1</span><span class="w"> </span><span class="k">TO</span><span class="w"> </span><span class="nl">IMAX:</span><span class="w"> </span><span class="vg">N</span><span class="p">(</span><span class="vg">I</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nl">I:</span><span class="w"> </span><span class="kr">NEXT</span><span class="w"> </span><span class="vg">I</span> <span class="nl">180</span><span class="w"> </span><span class="kr">FOR</span><span class="w"> </span><span class="vg">I</span><span class="o">=</span><span class="il">1</span><span class="w"> </span><span class="k">TO</span><span class="w"> </span><span class="vg">IMAX</span> <span class="nl">190</span><span class="w"> </span><span class="vg">J</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kr">INT</span><span class="p">(</span><span class="kr">RND</span><span class="o">*</span><span class="vg">IMAX</span><span class="p">)</span><span class="o">+</span><span class="il">1</span> <span class="nl">200</span><span class="w"> </span><span class="kr">SWAP</span><span class="w"> </span><span class="vg">N</span><span class="p">(</span><span class="vg">I</span><span class="p">),</span><span class="w"> </span><span class="vg">N</span><span class="p">(</span><span class="vg">J</span><span class="p">)</span> <span class="nl">210</span><span class="w"> </span><span class="kr">NEXT</span><span class="w"> </span><span class="vg">I</span> <span class="nl">220</span><span class="w"> </span><span class="kr">PRINT</span><span class="o">:</span><span class="w"> </span><span class="kr">PRINT</span><span class="w"> </span><span class="s2">"Unsorted list:"</span><span class="p">;</span> <span class="nl">230</span><span class="w"> </span><span class="kr">FOR</span><span class="w"> </span><span class="vg">I</span><span class="o">=</span><span class="il">1</span><span class="w"> </span><span class="k">TO</span><span class="w"> </span><span class="nl">IMAX:</span><span class="w"> </span><span class="kr">PRINT</span><span class="w"> </span><span class="vg">N</span><span class="p">(</span><span class="vg">I</span><span class="p">);</span><span class="o">:</span><span class="w"> </span><span class="kr">NEXT</span><span class="w"> </span><span class="vg">I</span> <span class="nl">240</span><span class="w"> </span><span class="kr">PRINT</span><span class="o">:</span><span class="w"> </span><span class="kr">PRINT</span> <span class="nl">250</span><span class="w"> </span><span class="c1">' Sorts the list through the Selection Sort Algorithm and shows the results</span> <span class="nl">260</span><span class="w"> </span><span class="vg">TL!</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kr">TIMER</span> <span class="nl">270</span><span class="w"> </span><span class="kr">PRINT</span><span class="w"> </span><span class="s2">"Sorting"</span><span class="p">;</span><span class="w"> </span><span class="vg">IMAX</span><span class="p">;</span><span class="w"> </span><span class="s2">"numbers"</span><span class="p">;</span> <span class="nl">280</span><span class="w"> </span><span class="kr">COLOR</span><span class="w"> </span><span class="il">7</span><span class="o">+</span><span class="il">16</span><span class="o">:</span><span class="w"> </span><span class="vg">X</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kr">POS</span><span class="p">(</span><span class="il">0</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="kr">PRINT</span><span class="s2">"..."</span><span class="p">;</span><span class="o">:</span><span class="w"> </span><span class="kr">COLOR</span><span class="w"> </span><span class="il">7</span> <span class="nl">290</span><span class="w"> </span><span class="vg">ITP</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">0</span> <span class="nl">300</span><span class="w"> </span><span class="kr">FOR</span><span class="w"> </span><span class="vg">I</span><span class="o">=</span><span class="il">1</span><span class="w"> </span><span class="k">TO</span><span class="w"> </span><span class="vg">IMAX</span><span class="il">-1</span> <span class="nl">310</span><span class="w"> </span><span class="vg">IMINV</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">I</span> <span class="nl">320</span><span class="w"> </span><span class="kr">FOR</span><span class="w"> </span><span class="vg">J</span><span class="o">=</span><span class="vg">I</span><span class="o">+</span><span class="il">1</span><span class="w"> </span><span class="k">TO</span><span class="w"> </span><span class="vg">IMAX</span> <span class="nl">330</span><span class="w"> </span><span class="kr">IF</span><span class="w"> </span><span class="vg">N</span><span class="p">(</span><span class="vg">IMINV</span><span class="p">)</span><span class="o">></span><span class="vg">N</span><span class="p">(</span><span class="vg">J</span><span class="p">)</span><span class="w"> </span><span class="kr">THEN</span><span class="w"> </span><span class="vg">IMINV</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">J</span> <span class="nl">340</span><span class="w"> </span><span class="kr">NEXT</span><span class="w"> </span><span class="vg">J</span> <span class="nl">350</span><span class="w"> </span><span class="kr">IF</span><span class="w"> </span><span class="vg">IMINV</span><span class="o">></span><span class="vg">I</span><span class="w"> </span><span class="kr">THEN</span><span class="w"> </span><span class="kr">SWAP</span><span class="w"> </span><span class="vg">N</span><span class="p">(</span><span class="vg">IMINV</span><span class="p">),</span><span class="w"> </span><span class="vg">N</span><span class="p">(</span><span class="vg">I</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="vg">TP!</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">TP!</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="il">1</span> <span class="nl">360</span><span class="w"> </span><span class="kr">NEXT</span><span class="w"> </span><span class="vg">I</span> <span class="nl">370</span><span class="w"> </span><span class="kr">LOCATE</span><span class="w"> </span><span class="p">,</span><span class="nl">X:</span><span class="w"> </span><span class="kr">PRINT</span><span class="w"> </span><span class="s2">". Done!"</span> <span class="nl">380</span><span class="w"> </span><span class="kr">PRINT</span><span class="o">:</span><span class="w"> </span><span class="kr">PRINT</span><span class="w"> </span><span class="s2">"Sorted list:"</span><span class="p">;</span> <span class="nl">390</span><span class="w"> </span><span class="kr">FOR</span><span class="w"> </span><span class="vg">I</span><span class="o">=</span><span class="il">1</span><span class="w"> </span><span class="k">TO</span><span class="w"> </span><span class="nl">IMAX:</span><span class="w"> </span><span class="kr">PRINT</span><span class="w"> </span><span class="vg">N</span><span class="p">(</span><span class="vg">I</span><span class="p">);</span><span class="o">:</span><span class="w"> </span><span class="kr">NEXT</span><span class="w"> </span><span class="vg">I</span> <span class="nl">400</span><span class="w"> </span><span class="c1">' Final results</span> <span class="nl">410</span><span class="w"> </span><span class="kr">PRINT</span><span class="o">:</span><span class="w"> </span><span class="kr">PRINT</span><span class="o">:</span><span class="w"> </span><span class="kr">PRINT</span><span class="w"> </span><span class="s2">"Numbers sorted:"</span><span class="p">;</span><span class="w"> </span><span class="vg">IMAX</span> <span class="nl">420</span><span class="w"> </span><span class="kr">PRINT</span><span class="w"> </span><span class="s2">"Total permutations done:"</span><span class="p">;</span><span class="vg">TP!</span> <span class="nl">430</span><span class="w"> </span><span class="kr">PRINT</span><span class="w"> </span><span class="s2">"Time lapse:"</span><span class="p">;</span><span class="w"> </span><span class="kr">TIMER</span><span class="o">-</span><span class="vg">TL!</span><span class="p">;</span><span class="w"> </span><span class="s2">"seconds."</span> <span class="nl">440</span><span class="w"> </span><span class="kr">PRINT</span> <span class="nl">450</span><span class="w"> </span><span class="kr">PRINT</span><span class="w"> </span><span class="s2">"End of program"</span> <span class="nl">460</span><span class="w"> </span><span class="kr">END</span> </pre></div> <h3><span class="mw-headline" id="IS-BASIC"><a href="/wiki/Category:IS-BASIC" title="Category:IS-BASIC">IS-BASIC</a> </span></h3> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>100 PROGRAM "SelecSrt.bas" 110 RANDOMIZE 120 NUMERIC ARRAY(-5 TO 14) 130 CALL INIT(ARRAY) 140 CALL WRITE(ARRAY) 150 CALL SELECTIONSORT(ARRAY) 160 CALL WRITE(ARRAY) 170 DEF INIT(REF A) 180 FOR I=LBOUND(A) TO UBOUND(A) 190 LET A(I)=RND(98)+1 200 NEXT 210 END DEF 220 DEF WRITE(REF A) 230 FOR I=LBOUND(A) TO UBOUND(A) 240 PRINT A(I); 250 NEXT 260 PRINT 270 END DEF 280 DEF SELECTIONSORT(REF A) 290 FOR I=LBOUND(A) TO UBOUND(A)-1 300 LET MN=A(I):LET INDEX=I 310 FOR J=I+1 TO UBOUND(A) 320 IF MN>A(J) THEN LET MN=A(J):LET INDEX=J 330 NEXT 340 LET A(INDEX)=A(I):LET A(I)=MN 350 NEXT 360 END DEF</pre></div> <h2><span class="mw-headline" id="BCPL"><a href="/wiki/Category:BCPL" title="Category:BCPL">BCPL</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>get "libhdr" let selectionsort(A, len) be if len > 1 $( let minloc = A and t = ? for i=0 to len-1 if !minloc > A!i do minloc := A+i t := !A !A := !minloc !minloc := t selectionsort(A+1, len-1) $) let writearray(A, len) be for i=0 to len-1 do writed(A!i, 6) let start() be $( let array = table 52, -5, -20, 199, 65, -3, 190, 25, 9999, -5342 let length = 10 writes("Input: ") ; writearray(array, length) ; wrch('*N') selectionsort(array, length) writes("Output: ") ; writearray(array, length) ; wrch('*N') $)</pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>Input: 52 -5 -20 199 65 -3 190 25 9999 -5342 Output: -5342 -20 -5 -3 25 52 65 190 199 9999</pre> <h2><span class="mw-headline" id="C"><a href="/wiki/Category:C" title="Category:C">C</a> </span></h2> <div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf"><stdio.h></span> <span class="kt">void</span><span class="w"> </span><span class="nf">selection_sort</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="o">*</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="p">,</span><span class="w"> </span><span class="n">m</span><span class="p">,</span><span class="w"> </span><span class="n">t</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">m</span><span class="p">])</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">m</span><span class="p">];</span> <span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">m</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">t</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> <span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">65</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">-31</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">99</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">83</span><span class="p">,</span><span class="w"> </span><span class="mi">782</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">};</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">sizeof</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="k">sizeof</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d%s"</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="s">" "</span><span class="p">);</span> <span class="w"> </span><span class="n">selection_sort</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">n</span><span class="p">);</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"%d%s"</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">],</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="s">" "</span><span class="p">);</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>4 65 2 -31 0 99 2 83 782 1 -31 0 1 2 2 4 65 83 99 782 </pre> <h2><span id="C.23"></span><span class="mw-headline" id="C#"><a href="/wiki/Category:C_sharp" title="Category:C sharp">C#</a> </span></h2> <p>This is a generic implementation that works with any type that implements the IComparable interface </p> <div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">SelectionSort</span><span class="o"><</span><span class="n">T</span><span class="o">></span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">IComparable</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="n">T</span><span class="p">[]</span><span class="w"> </span><span class="nf">Sort</span><span class="p">(</span><span class="n">T</span><span class="p">[]</span><span class="w"> </span><span class="n">list</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">k</span><span class="p">;</span> <span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="n">temp</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">list</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="o">=</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">list</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">list</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">CompareTo</span><span class="p">(</span><span class="n">list</span><span class="p">[</span><span class="n">k</span><span class="p">])</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="m">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="n">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">list</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="w"> </span><span class="n">list</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">list</span><span class="p">[</span><span class="n">k</span><span class="p">];</span> <span class="w"> </span><span class="n">list</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">temp</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">list</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> </pre></div> <p>Example of usage: </p> <div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span></span><span class="n">String</span><span class="p">[]</span><span class="w"> </span><span class="n">str</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="s">"this"</span><span class="p">,</span><span class="w"> </span><span class="s">"is"</span><span class="p">,</span><span class="w"> </span><span class="s">"a"</span><span class="p">,</span><span class="w"> </span><span class="s">"test"</span><span class="p">,</span><span class="w"> </span><span class="s">"of"</span><span class="p">,</span><span class="w"> </span><span class="s">"generic"</span><span class="p">,</span><span class="w"> </span><span class="s">"selection"</span><span class="p">,</span><span class="w"> </span><span class="s">"sort"</span><span class="w"> </span><span class="p">};</span> <span class="n">SelectionSort</span><span class="o"><</span><span class="n">String</span><span class="o">></span><span class="w"> </span><span class="n">mySort</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">SelectionSort</span><span class="o"><</span><span class="kt">string</span><span class="o">></span><span class="p">();</span> <span class="n">String</span><span class="p">[]</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mySort</span><span class="p">.</span><span class="n">Sort</span><span class="p">(</span><span class="n">str</span><span class="p">);</span> <span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">result</span><span class="p">.</span><span class="n">Length</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">result</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span> <span class="p">}</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>a generic is of selection sort test this</pre> <h2><span id="C.2B.2B"></span><span class="mw-headline" id="C++"><a href="/wiki/Category:C%2B%2B" title="Category:C++">C++</a> </span></h2> <p>Uses C++11. Compile with </p> <pre>g++ -std=c++11 selection.cpp </pre> <div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf"><algorithm></span> <span class="cp">#include</span><span class="w"> </span><span class="cpf"><iterator></span> <span class="cp">#include</span><span class="w"> </span><span class="cpf"><iostream></span> <span class="k">template</span><span class="o"><</span><span class="k">typename</span><span class="w"> </span><span class="nc">ForwardIterator</span><span class="o">></span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="n">selection_sort</span><span class="p">(</span><span class="n">ForwardIterator</span><span class="w"> </span><span class="n">begin</span><span class="p">,</span> <span class="w"> </span><span class="n">ForwardIterator</span><span class="w"> </span><span class="n">end</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="k">auto</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">begin</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">end</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">iter_swap</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">min_element</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">end</span><span class="p">));</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> <span class="kt">int</span><span class="w"> </span><span class="n">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="mi">100</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">56</span><span class="p">,</span><span class="w"> </span><span class="mi">200</span><span class="p">,</span><span class="w"> </span><span class="mi">-52</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">99</span><span class="p">,</span><span class="w"> </span><span class="mi">33</span><span class="p">,</span><span class="w"> </span><span class="mi">177</span><span class="p">,</span><span class="w"> </span><span class="mi">-199</span><span class="p">};</span> <span class="w"> </span><span class="n">selection_sort</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">begin</span><span class="p">(</span><span class="n">a</span><span class="p">),</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">end</span><span class="p">(</span><span class="n">a</span><span class="p">));</span> <span class="w"> </span><span class="n">copy</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">begin</span><span class="p">(</span><span class="n">a</span><span class="p">),</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">end</span><span class="p">(</span><span class="n">a</span><span class="p">),</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">ostream_iterator</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="p">,</span><span class="w"> </span><span class="s">" "</span><span class="p">));</span> <span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="s">"</span><span class="se">\n</span><span class="s">"</span><span class="p">;</span> <span class="p">}</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>-199 -52 2 3 33 56 99 100 177 200 </pre> <h2><span class="mw-headline" id="Clojure"><a href="/wiki/Category:Clojure" title="Category:Clojure">Clojure</a> </span></h2> <p>This is an implementation that mutates a Java arraylist in place. </p> <div class="mw-highlight mw-highlight-lang-lisp mw-content-ltr" dir="ltr"><pre><span></span><span class="p">(</span><span class="nb">import</span><span class="w"> </span><span class="ss">'java.util.ArrayList</span><span class="p">)</span> <span class="p">(</span><span class="nv">defn</span><span class="w"> </span><span class="nv">arr-swap!</span><span class="w"> </span><span class="nv">[#^ArrayList</span><span class="w"> </span><span class="nv">arr</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="nv">j]</span> <span class="w"> </span><span class="p">(</span><span class="k">let</span><span class="w"> </span><span class="nv">[t</span><span class="w"> </span><span class="p">(</span><span class="o">.</span><span class="nb">get</span><span class="w"> </span><span class="nv">arr</span><span class="w"> </span><span class="nv">i</span><span class="p">)</span><span class="nv">]</span> <span class="w"> </span><span class="p">(</span><span class="nv">doto</span><span class="w"> </span><span class="nv">arr</span> <span class="w"> </span><span class="p">(</span><span class="o">.</span><span class="nb">set</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="p">(</span><span class="o">.</span><span class="nb">get</span><span class="w"> </span><span class="nv">arr</span><span class="w"> </span><span class="nv">j</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="o">.</span><span class="nb">set</span><span class="w"> </span><span class="nv">j</span><span class="w"> </span><span class="no">t</span><span class="p">))))</span> <span class="p">(</span><span class="nv">defn</span><span class="w"> </span><span class="nv">sel-sort!</span> <span class="w"> </span><span class="p">(</span><span class="nv">[arr]</span><span class="w"> </span><span class="p">(</span><span class="nv">sel-sort!</span><span class="w"> </span><span class="nv">compare</span><span class="w"> </span><span class="nv">arr</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="nv">[cmp</span><span class="w"> </span><span class="err">#</span><span class="nv">^ArrayList</span><span class="w"> </span><span class="nv">arr]</span> <span class="w"> </span><span class="p">(</span><span class="k">let</span><span class="w"> </span><span class="nv">[n</span><span class="w"> </span><span class="p">(</span><span class="o">.</span><span class="nv">size</span><span class="w"> </span><span class="nv">arr</span><span class="p">)</span><span class="nv">]</span> <span class="w"> </span><span class="p">(</span><span class="nv">letfn</span><span class="w"> </span><span class="nv">[</span><span class="p">(</span><span class="nv">move-min!</span> <span class="w"> </span><span class="nv">[start-i]</span> <span class="w"> </span><span class="p">(</span><span class="nb">loop</span><span class="w"> </span><span class="nv">[i</span><span class="w"> </span><span class="nv">start-i]</span> <span class="w"> </span><span class="p">(</span><span class="nb">when</span><span class="w"> </span><span class="p">(</span><span class="nb"><</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="nv">n</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nb">when</span><span class="w"> </span><span class="p">(</span><span class="nb"><</span><span class="w"> </span><span class="p">(</span><span class="nv">cmp</span><span class="w"> </span><span class="p">(</span><span class="o">.</span><span class="nb">get</span><span class="w"> </span><span class="nv">arr</span><span class="w"> </span><span class="nv">i</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="o">.</span><span class="nb">get</span><span class="w"> </span><span class="nv">arr</span><span class="w"> </span><span class="nv">start-i</span><span class="p">))</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nv">arr-swap!</span><span class="w"> </span><span class="nv">arr</span><span class="w"> </span><span class="nv">start-i</span><span class="w"> </span><span class="nv">i</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="nv">recur</span><span class="w"> </span><span class="p">(</span><span class="nv">inc</span><span class="w"> </span><span class="nv">i</span><span class="p">)))))</span><span class="nv">]</span> <span class="w"> </span><span class="p">(</span><span class="nv">doseq</span><span class="w"> </span><span class="nv">[start-i</span><span class="w"> </span><span class="p">(</span><span class="nv">range</span><span class="w"> </span><span class="p">(</span><span class="nv">dec</span><span class="w"> </span><span class="nv">n</span><span class="p">))</span><span class="nv">]</span> <span class="w"> </span><span class="p">(</span><span class="nv">move-min!</span><span class="w"> </span><span class="nv">start-i</span><span class="p">))</span> <span class="w"> </span><span class="nv">arr</span><span class="p">))))</span> </pre></div> <h2><span class="mw-headline" id="COBOL"><a href="/wiki/Category:COBOL" title="Category:COBOL">COBOL</a> </span></h2> <div class="mw-highlight mw-highlight-lang-cobol mw-content-ltr" dir="ltr"><pre><span></span><span class="c"> </span><span class="w"> </span><span class="kr">PERFORM</span><span class="w"> </span><span class="nv">E-SELECTION</span><span class="w"> </span><span class="kp">VARYING</span><span class="w"> </span><span class="nv">WB-IX-1</span><span class="w"> </span><span class="kp">FROM</span><span class="w"> </span><span class="mi">1 </span><span class="kp">BY</span><span class="w"> </span><span class="mi">1</span> <span class="mi"> </span><span class="kp">UNTIL</span><span class="w"> </span><span class="nv">WB-IX-1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">WC-SIZE</span><span class="p">.</span> <span class="p">...</span> <span class="c"> </span><span class="w"> </span><span class="nv">E-SELECTION</span><span class="w"> </span><span class="kr">SECTION</span><span class="p">.</span> <span class="c"> </span><span class="w"> </span><span class="nv">E-000</span><span class="p">.</span> <span class="c"> </span><span class="w"> </span><span class="kr">SET</span><span class="w"> </span><span class="nv">WC-LOWEST</span><span class="w"> </span><span class="kp">TO</span><span class="w"> </span><span class="nv">WB-IX-1</span><span class="p">.</span> <span class="c"> </span><span class="w"> </span><span class="kr">ADD</span><span class="w"> </span><span class="mi">1 </span><span class="nv">WC-LOWEST</span><span class="w"> </span><span class="kp">GIVING</span><span class="w"> </span><span class="nv">WC-START</span> <span class="c"> </span><span class="w"> </span><span class="kr">PERFORM</span><span class="w"> </span><span class="nv">F-PASS</span><span class="w"> </span><span class="kp">VARYING</span><span class="w"> </span><span class="nv">WB-IX-2</span><span class="w"> </span><span class="kp">FROM</span><span class="w"> </span><span class="nv">WC-START</span><span class="w"> </span><span class="kp">BY</span><span class="w"> </span><span class="mi">1</span> <span class="mi"> </span><span class="kp">UNTIL</span><span class="w"> </span><span class="nv">WB-IX-2</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="nv">WC-SIZE</span><span class="p">.</span> <span class="c"> </span><span class="w"> </span><span class="kr">IF</span><span class="w"> </span><span class="nv">WB-IX-1</span><span class="w"> </span><span class="ow">NOT </span><span class="o">=</span><span class="w"> </span><span class="nv">WC-LOWEST</span> <span class="c"> </span><span class="w"> </span><span class="kr">MOVE</span><span class="w"> </span><span class="nv">WB-ENTRY</span><span class="p">(</span><span class="nv">WC-LOWEST</span><span class="p">)</span><span class="w"> </span><span class="kp">TO</span><span class="w"> </span><span class="nv">WC-TEMP</span> <span class="c"> </span><span class="w"> </span><span class="kr">MOVE</span><span class="w"> </span><span class="nv">WB-ENTRY</span><span class="p">(</span><span class="nv">WB-IX-1</span><span class="p">)</span><span class="w"> </span><span class="kp">TO</span><span class="w"> </span><span class="nv">WB-ENTRY</span><span class="p">(</span><span class="nv">WC-LOWEST</span><span class="p">)</span> <span class="c"> </span><span class="w"> </span><span class="kr">MOVE</span><span class="w"> </span><span class="nv">WC-TEMP</span><span class="w"> </span><span class="kp">TO</span><span class="w"> </span><span class="nv">WB-ENTRY</span><span class="p">(</span><span class="nv">WB-IX-1</span><span class="p">).</span> <span class="c"> </span><span class="w"> </span><span class="nv">E-999</span><span class="p">.</span> <span class="c"> </span><span class="w"> </span><span class="kr">EXIT</span><span class="p">.</span> <span class="c"> </span><span class="w"> </span><span class="nv">F-PASS</span><span class="w"> </span><span class="kr">SECTION</span><span class="p">.</span> <span class="c"> </span><span class="w"> </span><span class="nv">F-000</span><span class="p">.</span> <span class="c"> </span><span class="w"> </span><span class="kr">IF</span><span class="w"> </span><span class="nv">WB-ENTRY</span><span class="p">(</span><span class="nv">WB-IX-2</span><span class="p">)</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nv">WB-ENTRY</span><span class="p">(</span><span class="nv">WC-LOWEST</span><span class="p">)</span> <span class="c"> </span><span class="w"> </span><span class="kr">SET</span><span class="w"> </span><span class="nv">WC-LOWEST</span><span class="w"> </span><span class="kp">TO</span><span class="w"> </span><span class="nv">WB-IX-2</span><span class="p">.</span> <span class="c"> </span><span class="w"> </span><span class="nv">F-999</span><span class="p">.</span> <span class="c"> </span><span class="w"> </span><span class="kr">EXIT</span><span class="p">.</span> </pre></div> <h2><span class="mw-headline" id="Common_Lisp"><a href="/wiki/Category:Common_Lisp" title="Category:Common Lisp">Common Lisp</a> </span></h2> <div class="mw-highlight mw-highlight-lang-lisp mw-content-ltr" dir="ltr"><pre><span></span><span class="p">(</span><span class="nb">defun</span><span class="w"> </span><span class="nv">selection-sort-vector</span><span class="w"> </span><span class="p">(</span><span class="nc">array</span><span class="w"> </span><span class="nv">predicate</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nb">do</span><span class="w"> </span><span class="p">((</span><span class="nb">length</span><span class="w"> </span><span class="p">(</span><span class="nb">length</span><span class="w"> </span><span class="nc">array</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="nv">i</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">(</span><span class="nb">1+</span><span class="w"> </span><span class="nv">i</span><span class="p">)))</span> <span class="w"> </span><span class="p">((</span><span class="nb">eql</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="nb">length</span><span class="p">)</span><span class="w"> </span><span class="nc">array</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nb">do</span><span class="w"> </span><span class="p">((</span><span class="nv">mindex</span><span class="w"> </span><span class="nv">i</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nb">min</span><span class="w"> </span><span class="p">(</span><span class="nb">aref</span><span class="w"> </span><span class="nc">array</span><span class="w"> </span><span class="nv">i</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="nv">j</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="p">(</span><span class="nb">1+</span><span class="w"> </span><span class="nv">j</span><span class="p">)))</span> <span class="w"> </span><span class="p">((</span><span class="nb">eql</span><span class="w"> </span><span class="nv">j</span><span class="w"> </span><span class="nb">length</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nb">rotatef</span><span class="w"> </span><span class="p">(</span><span class="nb">aref</span><span class="w"> </span><span class="nc">array</span><span class="w"> </span><span class="nv">i</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nb">aref</span><span class="w"> </span><span class="nc">array</span><span class="w"> </span><span class="nv">mindex</span><span class="p">)))</span> <span class="w"> </span><span class="p">(</span><span class="nb">when</span><span class="w"> </span><span class="p">(</span><span class="nb">funcall</span><span class="w"> </span><span class="nv">predicate</span><span class="w"> </span><span class="p">(</span><span class="nb">aref</span><span class="w"> </span><span class="nc">array</span><span class="w"> </span><span class="nv">j</span><span class="p">)</span><span class="w"> </span><span class="nb">min</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nb">setf</span><span class="w"> </span><span class="nb">min</span><span class="w"> </span><span class="p">(</span><span class="nb">aref</span><span class="w"> </span><span class="nc">array</span><span class="w"> </span><span class="nv">j</span><span class="p">)</span> <span class="w"> </span><span class="nv">mindex</span><span class="w"> </span><span class="nv">j</span><span class="p">)))))</span> <span class="p">(</span><span class="nb">defun</span><span class="w"> </span><span class="nv">selection-sort-list</span><span class="w"> </span><span class="p">(</span><span class="nb">list</span><span class="w"> </span><span class="nv">predicate</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="k">flet</span><span class="w"> </span><span class="p">((</span><span class="nv">min-first</span><span class="w"> </span><span class="p">(</span><span class="nb">list</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nb">do</span><span class="w"> </span><span class="p">((</span><span class="nv">before-min</span><span class="w"> </span><span class="no">nil</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nb">min</span><span class="w"> </span><span class="p">(</span><span class="nb">first</span><span class="w"> </span><span class="nb">list</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="nv">prev</span><span class="w"> </span><span class="nb">list</span><span class="w"> </span><span class="p">(</span><span class="nb">rest</span><span class="w"> </span><span class="nv">prev</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="nv">curr</span><span class="w"> </span><span class="p">(</span><span class="nb">rest</span><span class="w"> </span><span class="nb">list</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nb">rest</span><span class="w"> </span><span class="nv">curr</span><span class="p">)))</span> <span class="w"> </span><span class="p">((</span><span class="nb">endp</span><span class="w"> </span><span class="nv">curr</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nb">null</span><span class="w"> </span><span class="nv">before-min</span><span class="p">)</span><span class="w"> </span><span class="nb">list</span> <span class="w"> </span><span class="p">(</span><span class="k">let</span><span class="w"> </span><span class="p">((</span><span class="nb">min</span><span class="w"> </span><span class="p">(</span><span class="nb">cdr</span><span class="w"> </span><span class="nv">before-min</span><span class="p">)))</span> <span class="w"> </span><span class="p">(</span><span class="nb">rplacd</span><span class="w"> </span><span class="nv">before-min</span><span class="w"> </span><span class="p">(</span><span class="nb">cdr</span><span class="w"> </span><span class="nb">min</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="nb">rplacd</span><span class="w"> </span><span class="nb">min</span><span class="w"> </span><span class="nb">list</span><span class="p">)</span> <span class="w"> </span><span class="nb">min</span><span class="p">)))</span> <span class="w"> </span><span class="p">(</span><span class="nb">when</span><span class="w"> </span><span class="p">(</span><span class="nb">funcall</span><span class="w"> </span><span class="nv">predicate</span><span class="w"> </span><span class="p">(</span><span class="nb">first</span><span class="w"> </span><span class="nv">curr</span><span class="p">)</span><span class="w"> </span><span class="nb">min</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nb">setf</span><span class="w"> </span><span class="nv">before-min</span><span class="w"> </span><span class="nv">prev</span> <span class="w"> </span><span class="nb">min</span><span class="w"> </span><span class="p">(</span><span class="nb">first</span><span class="w"> </span><span class="nv">curr</span><span class="p">))))))</span> <span class="w"> </span><span class="p">(</span><span class="k">let</span><span class="w"> </span><span class="p">((</span><span class="nv">result</span><span class="w"> </span><span class="p">(</span><span class="nv">min-first</span><span class="w"> </span><span class="nb">list</span><span class="p">)))</span> <span class="w"> </span><span class="p">(</span><span class="nb">do</span><span class="w"> </span><span class="p">((</span><span class="nv">head</span><span class="w"> </span><span class="nv">result</span><span class="w"> </span><span class="p">(</span><span class="nb">rest</span><span class="w"> </span><span class="nv">head</span><span class="p">)))</span> <span class="w"> </span><span class="p">((</span><span class="nb">endp</span><span class="w"> </span><span class="p">(</span><span class="nb">rest</span><span class="w"> </span><span class="nv">head</span><span class="p">))</span><span class="w"> </span><span class="nv">result</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nb">rplacd</span><span class="w"> </span><span class="nv">head</span><span class="w"> </span><span class="p">(</span><span class="nv">min-first</span><span class="w"> </span><span class="p">(</span><span class="nb">rest</span><span class="w"> </span><span class="nv">head</span><span class="p">)))))))</span> <span class="p">(</span><span class="nb">defun</span><span class="w"> </span><span class="nv">selection-sort</span><span class="w"> </span><span class="p">(</span><span class="nc">sequence</span><span class="w"> </span><span class="nv">predicate</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nb">etypecase</span><span class="w"> </span><span class="nc">sequence</span> <span class="w"> </span><span class="p">(</span><span class="nb">list</span><span class="w"> </span><span class="p">(</span><span class="nv">selection-sort-list</span><span class="w"> </span><span class="nc">sequence</span><span class="w"> </span><span class="nv">predicate</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="nb">vector</span><span class="w"> </span><span class="p">(</span><span class="nv">selection-sort-vector</span><span class="w"> </span><span class="nc">sequence</span><span class="w"> </span><span class="nv">predicate</span><span class="p">))))</span> </pre></div> <p>Example use: </p> <pre>> (selection-sort (list 8 7 4 3 2 0 9 1 5 6) '<) (0 1 2 3 4 5 6 7 8 9) > (selection-sort (vector 8 7 4 3 2 0 9 1 5 6) '>) #(9 8 7 6 5 4 3 2 1 0)</pre> <h2><span class="mw-headline" id="Crystal"><a href="/wiki/Category:Crystal" title="Category:Crystal">Crystal</a> </span></h2> <p>This sorts the array in-place. </p> <div class="mw-highlight mw-highlight-lang-crystal mw-content-ltr" dir="ltr"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">selectionSort</span><span class="p">(</span><span class="n">array</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">Array</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="o">...</span><span class="n">array</span><span class="o">.</span><span class="n">size</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">each</span><span class="w"> </span><span class="k">do</span><span class="w"> </span><span class="o">|</span><span class="n">i</span><span class="o">|</span> <span class="w"> </span><span class="n">nextMinIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span> <span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="o">...</span><span class="n">array</span><span class="o">.</span><span class="n">size</span><span class="p">)</span><span class="o">.</span><span class="n">each</span><span class="w"> </span><span class="k">do</span><span class="w"> </span><span class="o">|</span><span class="n">j</span><span class="o">|</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">array</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">array</span><span class="o">[</span><span class="n">nextMinIndex</span><span class="o">]</span> <span class="w"> </span><span class="n">nextMinIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="k">end</span><span class="w"> </span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">nextMinIndex</span> <span class="w"> </span><span class="n">array</span><span class="o">.</span><span class="n">swap</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">nextMinIndex</span><span class="p">)</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="k">end</span> <span class="k">end</span> </pre></div> <h2><span class="mw-headline" id="D"><a href="/wiki/Category:D" title="Category:D">D</a> </span></h2> <p>The actual function is very short. </p> <div class="mw-highlight mw-highlight-lang-d mw-content-ltr" dir="ltr"><pre><span></span><span class="k">import</span><span class="w"> </span><span class="n">std</span><span class="p">.</span><span class="n">stdio</span><span class="p">,</span><span class="w"> </span><span class="n">std</span><span class="p">.</span><span class="n">algorithm</span><span class="p">,</span><span class="w"> </span><span class="n">std</span><span class="p">.</span><span class="n">array</span><span class="p">,</span><span class="w"> </span><span class="n">std</span><span class="p">.</span><span class="n">traits</span><span class="p">;</span> <span class="k">enum</span><span class="w"> </span><span class="n">AreSortableArrayItems</span><span class="p">(</span><span class="n">T</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">isMutable</span><span class="p">!</span><span class="n">T</span><span class="w"> </span><span class="p">&&</span> <span class="w"> </span><span class="k">__traits</span><span class="p">(</span><span class="n">compiles</span><span class="p">,</span><span class="w"> </span><span class="n">T</span><span class="p">.</span><span class="n">init</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="n">T</span><span class="p">.</span><span class="n">init</span><span class="p">)</span><span class="w"> </span><span class="p">&&</span> <span class="w"> </span><span class="p">!</span><span class="n">isNarrowString</span><span class="p">!(</span><span class="n">T</span><span class="p">[]);</span> <span class="kt">void</span><span class="w"> </span><span class="n">selectionSort</span><span class="p">(</span><span class="n">T</span><span class="p">)(</span><span class="n">T</span><span class="p">[]</span><span class="w"> </span><span class="n">data</span><span class="p">)</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">AreSortableArrayItems</span><span class="p">!</span><span class="n">T</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">foreach</span><span class="w"> </span><span class="p">(</span><span class="k">immutable</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="k">ref</span><span class="w"> </span><span class="n">d</span><span class="p">;</span><span class="w"> </span><span class="n">data</span><span class="p">)</span> <span class="w"> </span><span class="n">data</span><span class="p">.</span><span class="n">drop</span><span class="p">(</span><span class="n">i</span><span class="p">).</span><span class="n">minPos</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">swap</span><span class="p">(</span><span class="n">d</span><span class="p">);</span> <span class="p">}</span><span class="w"> </span><span class="k">unittest</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kt">int</span><span class="p">[]</span><span class="w"> </span><span class="n">a0</span><span class="p">;</span> <span class="w"> </span><span class="n">a0</span><span class="p">.</span><span class="n">selectionSort</span><span class="p">;</span> <span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">a1</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="p">];</span> <span class="w"> </span><span class="n">a1</span><span class="p">.</span><span class="n">selectionSort</span><span class="p">;</span> <span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">a1</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="p">]);</span> <span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">a2</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="s">"a"</span><span class="p">,</span><span class="w"> </span><span class="s">"b"</span><span class="p">];</span> <span class="w"> </span><span class="n">a2</span><span class="p">.</span><span class="n">selectionSort</span><span class="p">;</span> <span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">a2</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="p">[</span><span class="s">"a"</span><span class="p">,</span><span class="w"> </span><span class="s">"b"</span><span class="p">]);</span> <span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">a3</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="s">"b"</span><span class="p">,</span><span class="w"> </span><span class="s">"a"</span><span class="p">];</span> <span class="w"> </span><span class="n">a3</span><span class="p">.</span><span class="n">selectionSort</span><span class="p">;</span> <span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">a3</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="p">[</span><span class="s">"a"</span><span class="p">,</span><span class="w"> </span><span class="s">"b"</span><span class="p">]);</span> <span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">a4</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="sc">'a'</span><span class="p">,</span><span class="w"> </span><span class="sc">'b'</span><span class="p">];</span> <span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="k">assert</span><span class="p">(!</span><span class="k">__traits</span><span class="p">(</span><span class="n">compiles</span><span class="p">,</span><span class="w"> </span><span class="n">a4</span><span class="p">.</span><span class="n">selectionSort</span><span class="p">));</span> <span class="w"> </span><span class="kt">dchar</span><span class="p">[]</span><span class="w"> </span><span class="n">a5</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="sc">'b'</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">];</span> <span class="w"> </span><span class="n">a5</span><span class="p">.</span><span class="n">selectionSort</span><span class="p">;</span> <span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">a5</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="s">"ab"d</span><span class="p">);</span> <span class="w"> </span><span class="k">import</span><span class="w"> </span><span class="n">std</span><span class="p">.</span><span class="n">typecons</span><span class="p">;</span> <span class="w"> </span><span class="k">alias</span><span class="w"> </span><span class="n">Nullable</span><span class="p">!</span><span class="kt">int</span><span class="w"> </span><span class="n">N</span><span class="p">;</span> <span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">a6</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="n">N</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span><span class="w"> </span><span class="n">N</span><span class="p">(</span><span class="mi">1</span><span class="p">)];</span> <span class="w"> </span><span class="n">a6</span><span class="p">.</span><span class="n">selectionSort</span><span class="p">;</span><span class="w"> </span><span class="c1">// Not nothrow.</span> <span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">a6</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="p">[</span><span class="n">N</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span><span class="w"> </span><span class="n">N</span><span class="p">(</span><span class="mi">2</span><span class="p">)]);</span> <span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">a7</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="mf">1.0</span><span class="p">+</span><span class="mi">0</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="mf">2.0</span><span class="p">+</span><span class="mi">0</span><span class="n">i</span><span class="p">];</span><span class="w"> </span><span class="c1">// To be deprecated.</span> <span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="k">assert</span><span class="p">(!</span><span class="k">__traits</span><span class="p">(</span><span class="n">compiles</span><span class="p">,</span><span class="w"> </span><span class="n">a7</span><span class="p">.</span><span class="n">selectionSort</span><span class="p">));</span> <span class="w"> </span><span class="k">import</span><span class="w"> </span><span class="n">std</span><span class="p">.</span><span class="n">complex</span><span class="p">;</span> <span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">a8</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="n">complex</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span><span class="w"> </span><span class="n">complex</span><span class="p">(</span><span class="mi">2</span><span class="p">)];</span> <span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="k">assert</span><span class="p">(!</span><span class="k">__traits</span><span class="p">(</span><span class="n">compiles</span><span class="p">,</span><span class="w"> </span><span class="n">a8</span><span class="p">.</span><span class="n">selectionSort</span><span class="p">));</span> <span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="n">F</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">x</span><span class="p">;</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">opCmp</span><span class="p">(</span><span class="n">F</span><span class="w"> </span><span class="n">f</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// Not pure.</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="n">f</span><span class="p">.</span><span class="n">x</span><span class="w"> </span><span class="p">?</span><span class="w"> </span><span class="p">-</span><span class="mi">1</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="p">></span><span class="w"> </span><span class="n">f</span><span class="p">.</span><span class="n">x</span><span class="w"> </span><span class="p">?</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">a9</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="n">F</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span><span class="w"> </span><span class="n">F</span><span class="p">(</span><span class="mi">1</span><span class="p">)];</span> <span class="w"> </span><span class="n">a9</span><span class="p">.</span><span class="n">selectionSort</span><span class="p">;</span> <span class="w"> </span><span class="k">assert</span><span class="p">(</span><span class="n">a9</span><span class="w"> </span><span class="p">==</span><span class="w"> </span><span class="p">[</span><span class="n">F</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span><span class="w"> </span><span class="n">F</span><span class="p">(</span><span class="mi">2</span><span class="p">)]);</span> <span class="p">}</span> <span class="kt">void</span><span class="w"> </span><span class="n">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">auto</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">];</span> <span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">selectionSort</span><span class="p">;</span> <span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">writeln</span><span class="p">;</span> <span class="p">}</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>[1, 1, 2, 2, 3, 3, 3, 4, 5, 5, 5, 6, 7, 8, 9, 9, 9]</pre> <h2><span class="mw-headline" id="Dart"><a href="/wiki/Category:Dart" title="Category:Dart">Dart</a> </span></h2> <div class="examplemeta translation"><b>Translation of</b>: <a href="#_Java"> Java</a></div> <div class="mw-highlight mw-highlight-lang-dart mw-content-ltr" dir="ltr"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="n">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">selectionSort</span><span class="p">([</span><span class="m">1100</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">56</span><span class="p">,</span><span class="w"> </span><span class="m">200</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="m">52</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">99</span><span class="p">,</span><span class="w"> </span><span class="m">33</span><span class="p">,</span><span class="w"> </span><span class="m">177</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="m">199</span><span class="p">]);</span> <span class="w"> </span><span class="n">print</span><span class="p">(</span><span class="s1">'</span><span class="si">$</span><span class="n">a</span><span class="s1">'</span><span class="p">);</span> <span class="p">}</span> <span class="n">selectionSort</span><span class="p">(</span><span class="n">List</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="w"> </span><span class="n">array</span><span class="p">){</span> <span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">currentPlace</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span><span class="n">currentPlace</span><span class="o"><</span><span class="n">array</span><span class="p">.</span><span class="n">length</span><span class="o">-</span><span class="m">1</span><span class="p">;</span><span class="n">currentPlace</span><span class="o">++</span><span class="p">){</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">smallest</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">4294967296</span><span class="p">;</span><span class="w"> </span><span class="c1">//maxInt</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">smallestAt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">currentPlace</span><span class="o">+</span><span class="m">1</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">check</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">currentPlace</span><span class="p">;</span><span class="w"> </span><span class="n">check</span><span class="o"><</span><span class="n">array</span><span class="p">.</span><span class="n">length</span><span class="p">;</span><span class="n">check</span><span class="o">++</span><span class="p">){</span> <span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="n">array</span><span class="p">[</span><span class="n">check</span><span class="p">]</span><span class="o"><</span><span class="n">smallest</span><span class="p">){</span> <span class="w"> </span><span class="n">smallestAt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">check</span><span class="p">;</span> <span class="w"> </span><span class="n">smallest</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">check</span><span class="p">];</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">currentPlace</span><span class="p">];</span> <span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">currentPlace</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">smallestAt</span><span class="p">];</span> <span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">smallestAt</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">temp</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">array</span><span class="p">;</span> <span class="p">}</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre> unsorted array: [1100, 2, 56, 200, -52, 3, 99, 33, 177, -199] a sorted: [-199, -52, 2, 3, 33, 56, 99, 177, 200, 1100] </pre> <h2><span class="mw-headline" id="Delphi"><a href="/wiki/Category:Delphi" title="Category:Delphi">Delphi</a> </span></h2> <h3><span class="mw-headline" id="Array_sort">Array sort</span></h3> <p>Dynamic array is a 0-based array of variable length </p><p>Static array is an arbitrary-based array of fixed length </p> <div class="mw-highlight mw-highlight-lang-delphi mw-content-ltr" dir="ltr"><pre><span></span><span class="k">program</span><span class="w"> </span><span class="n">TestSelectionSort</span><span class="o">;</span> <span class="cm">{$APPTYPE CONSOLE}</span> <span class="cm">{.$DEFINE DYNARRAY}</span><span class="w"> </span><span class="c1">// remove '.' to compile with dynamic array</span> <span class="k">type</span> <span class="w"> </span><span class="n">TItem</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">Integer</span><span class="o">;</span><span class="w"> </span><span class="c1">// declare ordinal type for array item</span> <span class="cm">{$IFDEF DYNARRAY}</span> <span class="w"> </span><span class="n">TArray</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">TItem</span><span class="o">;</span><span class="w"> </span><span class="c1">// dynamic array</span> <span class="cm">{$ELSE}</span> <span class="w"> </span><span class="n">TArray</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">array</span><span class="p">[</span><span class="mi">0</span><span class="o">..</span><span class="mi">15</span><span class="p">]</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="n">TItem</span><span class="o">;</span><span class="w"> </span><span class="c1">// static array</span> <span class="cm">{$ENDIF}</span> <span class="k">procedure</span><span class="w"> </span><span class="nf">SelectionSort</span><span class="p">(</span><span class="k">var</span><span class="w"> </span><span class="n">A</span><span class="o">:</span><span class="w"> </span><span class="n">TArray</span><span class="p">)</span><span class="o">;</span> <span class="k">var</span> <span class="w"> </span><span class="n">Item</span><span class="o">:</span><span class="w"> </span><span class="n">TItem</span><span class="o">;</span> <span class="w"> </span><span class="n">I</span><span class="o">,</span><span class="w"> </span><span class="n">J</span><span class="o">,</span><span class="w"> </span><span class="n">M</span><span class="o">:</span><span class="w"> </span><span class="kt">Integer</span><span class="o">;</span> <span class="k">begin</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">I</span><span class="o">:=</span><span class="w"> </span><span class="nb">Low</span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="nb">High</span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">do</span><span class="w"> </span><span class="k">begin</span> <span class="w"> </span><span class="n">M</span><span class="o">:=</span><span class="w"> </span><span class="n">I</span><span class="o">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">J</span><span class="o">:=</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="nb">High</span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="k">do</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">A</span><span class="p">[</span><span class="n">J</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">A</span><span class="p">[</span><span class="n">M</span><span class="p">]</span><span class="w"> </span><span class="k">then</span><span class="w"> </span><span class="n">M</span><span class="o">:=</span><span class="w"> </span><span class="n">J</span><span class="o">;</span> <span class="w"> </span><span class="n">Item</span><span class="o">:=</span><span class="w"> </span><span class="n">A</span><span class="p">[</span><span class="n">M</span><span class="p">]</span><span class="o">;</span> <span class="w"> </span><span class="n">A</span><span class="p">[</span><span class="n">M</span><span class="p">]</span><span class="o">:=</span><span class="w"> </span><span class="n">A</span><span class="p">[</span><span class="n">I</span><span class="p">]</span><span class="o">;</span> <span class="w"> </span><span class="n">A</span><span class="p">[</span><span class="n">I</span><span class="p">]</span><span class="o">:=</span><span class="w"> </span><span class="n">Item</span><span class="o">;</span> <span class="w"> </span><span class="k">end</span><span class="o">;</span> <span class="k">end</span><span class="o">;</span> <span class="k">var</span> <span class="w"> </span><span class="n">A</span><span class="o">:</span><span class="w"> </span><span class="n">TArray</span><span class="o">;</span> <span class="w"> </span><span class="n">I</span><span class="o">:</span><span class="w"> </span><span class="kt">Integer</span><span class="o">;</span> <span class="k">begin</span> <span class="cm">{$IFDEF DYNARRAY}</span> <span class="w"> </span><span class="nb">SetLength</span><span class="p">(</span><span class="n">A</span><span class="o">,</span><span class="w"> </span><span class="mi">16</span><span class="p">)</span><span class="o">;</span> <span class="cm">{$ENDIF}</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">I</span><span class="o">:=</span><span class="w"> </span><span class="nb">Low</span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="nb">High</span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="k">do</span> <span class="w"> </span><span class="n">A</span><span class="p">[</span><span class="n">I</span><span class="p">]</span><span class="o">:=</span><span class="w"> </span><span class="nb">Random</span><span class="p">(</span><span class="mi">100</span><span class="p">)</span><span class="o">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">I</span><span class="o">:=</span><span class="w"> </span><span class="nb">Low</span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="nb">High</span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="k">do</span> <span class="w"> </span><span class="nb">Write</span><span class="p">(</span><span class="n">A</span><span class="p">[</span><span class="n">I</span><span class="p">]</span><span class="o">:</span><span class="mi">3</span><span class="p">)</span><span class="o">;</span> <span class="w"> </span><span class="nb">Writeln</span><span class="o">;</span> <span class="w"> </span><span class="n">SelectionSort</span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="o">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">I</span><span class="o">:=</span><span class="w"> </span><span class="nb">Low</span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="nb">High</span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="w"> </span><span class="k">do</span> <span class="w"> </span><span class="nb">Write</span><span class="p">(</span><span class="n">A</span><span class="p">[</span><span class="n">I</span><span class="p">]</span><span class="o">:</span><span class="mi">3</span><span class="p">)</span><span class="o">;</span> <span class="w"> </span><span class="nb">Writeln</span><span class="o">;</span> <span class="w"> </span><span class="nb">Readln</span><span class="o">;</span> <span class="k">end</span><span class="o">.</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre> 0 3 86 20 27 67 31 16 37 42 8 47 7 84 5 29 0 3 5 7 8 16 20 27 29 31 37 42 47 67 84 86 </pre> <h3><span class="mw-headline" id="String_sort">String sort</span></h3> <p>// string is 1-based variable-length array of Char </p> <div class="mw-highlight mw-highlight-lang-delphi mw-content-ltr" dir="ltr"><pre><span></span><span class="k">procedure</span><span class="w"> </span><span class="nf">SelectionSort</span><span class="p">(</span><span class="k">var</span><span class="w"> </span><span class="n">S</span><span class="o">:</span><span class="w"> </span><span class="k">string</span><span class="p">)</span><span class="o">;</span> <span class="k">var</span> <span class="w"> </span><span class="n">Lowest</span><span class="o">:</span><span class="w"> </span><span class="kt">Char</span><span class="o">;</span> <span class="w"> </span><span class="n">I</span><span class="o">,</span><span class="w"> </span><span class="n">J</span><span class="o">,</span><span class="w"> </span><span class="n">M</span><span class="o">,</span><span class="w"> </span><span class="n">L</span><span class="o">:</span><span class="w"> </span><span class="kt">Integer</span><span class="o">;</span> <span class="k">begin</span> <span class="w"> </span><span class="n">L</span><span class="o">:=</span><span class="w"> </span><span class="nb">Length</span><span class="p">(</span><span class="n">S</span><span class="p">)</span><span class="o">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">I</span><span class="o">:=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">L</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">do</span><span class="w"> </span><span class="k">begin</span> <span class="w"> </span><span class="n">M</span><span class="o">:=</span><span class="w"> </span><span class="n">I</span><span class="o">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">J</span><span class="o">:=</span><span class="w"> </span><span class="n">I</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">to</span><span class="w"> </span><span class="n">L</span><span class="w"> </span><span class="k">do</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">S</span><span class="p">[</span><span class="n">J</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">S</span><span class="p">[</span><span class="n">M</span><span class="p">]</span><span class="w"> </span><span class="k">then</span><span class="w"> </span><span class="n">M</span><span class="o">:=</span><span class="w"> </span><span class="n">J</span><span class="o">;</span> <span class="w"> </span><span class="n">Lowest</span><span class="o">:=</span><span class="w"> </span><span class="n">S</span><span class="p">[</span><span class="n">M</span><span class="p">]</span><span class="o">;</span> <span class="w"> </span><span class="n">S</span><span class="p">[</span><span class="n">M</span><span class="p">]</span><span class="o">:=</span><span class="w"> </span><span class="n">S</span><span class="p">[</span><span class="n">I</span><span class="p">]</span><span class="o">;</span> <span class="w"> </span><span class="n">S</span><span class="p">[</span><span class="n">I</span><span class="p">]</span><span class="o">:=</span><span class="w"> </span><span class="n">Lowest</span><span class="o">;</span> <span class="w"> </span><span class="k">end</span><span class="o">;</span> <span class="k">end</span><span class="o">;</span> </pre></div> <pre>// in : S = 'the quick brown fox jumps over the lazy dog' // out: S = ' abcdeeefghhijklmnoooopqrrsttuuvwxyz' </pre> <h2><span class="mw-headline" id="E"><a href="/wiki/Category:E" title="Category:E">E</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>def selectionSort := { def cswap(c, a, b) { def t := c[a] c[a]  := c[b] c[b]  := t println(c) } def indexOfMin(array, first, last) { var min := array[first] var mini := first for i in (first+1)..last { if (array[i] < min) { min := array[i] mini := i } } return mini } /** Selection sort (in-place). */ def selectionSort(array) { def last := (array.size()-1) for i in 0..(last - 1) { cswap(array, i, indexOfMin(array, i + 1, last)) } } }</pre></div> <h2><span class="mw-headline" id="EasyLang"><a href="/wiki/Category:EasyLang" title="Category:EasyLang">EasyLang</a> </span></h2> <div class="mw-highlight mw-highlight-lang-text mw-content-ltr" dir="ltr"><pre><span></span>proc sort . d[] . for i = 1 to len d[] - 1 for j = i + 1 to len d[] if d[j] < d[i] swap d[j] d[i] . . . . data[] = [ 29 4 72 44 55 26 27 77 92 5 ] sort data[] print data[] </pre></div> <h2><span class="mw-headline" id="EchoLisp"><a href="/wiki/Category:EchoLisp" title="Category:EchoLisp">EchoLisp</a> </span></h2> <h3><span class="mw-headline" id="List_sort">List sort</span></h3> <div class="mw-highlight mw-highlight-lang-scheme mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">;; recursive version (adapted from Racket)</span> <span class="p">(</span><span class="nf">lib</span><span class="w"> </span><span class="ss">'list</span><span class="p">)</span><span class="w"> </span><span class="c1">;; list-delete</span> <span class="p">(</span><span class="k">define</span><span class="w"> </span><span class="p">(</span><span class="nf">sel-sort</span><span class="w"> </span><span class="nv">xs</span><span class="w"> </span><span class="p">(</span><span class="nf">x0</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="k">cond</span><span class="w"> </span> <span class="w"> </span><span class="p">[(</span><span class="nb">null?</span><span class="w"> </span><span class="nv">xs</span><span class="p">)</span><span class="w"> </span><span class="nv">null</span><span class="p">]</span> <span class="w"> </span><span class="p">[</span><span class="k">else</span><span class="w"> </span><span class="p">(</span><span class="k">set!</span><span class="w"> </span><span class="nv">x0</span><span class="w"> </span><span class="p">(</span><span class="nb">apply</span><span class="w"> </span><span class="nb">min</span><span class="w"> </span><span class="nv">xs</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="nb">cons</span><span class="w"> </span><span class="nv">x0</span><span class="w"> </span><span class="p">(</span><span class="nf">sel-sort</span><span class="w"> </span><span class="p">(</span><span class="nf">list-delete</span><span class="w"> </span><span class="nv">xs</span><span class="w"> </span><span class="nv">x0</span><span class="p">)))]))</span> <span class="p">(</span><span class="nf">sel-sort</span><span class="w"> </span><span class="p">(</span><span class="nf">shuffle</span><span class="w"> </span><span class="p">(</span><span class="nb">iota</span><span class="w"> </span><span class="mi">13</span><span class="p">)))</span> <span class="w"> </span><span class="err">→</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="mi">6</span><span class="w"> </span><span class="mi">7</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="mi">9</span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="mi">11</span><span class="w"> </span><span class="mi">12</span><span class="p">)</span> <span class="w"> </span> <span class="c1">;; straightforward and more efficient implementation using list-swap!</span> <span class="p">(</span><span class="k">define</span><span class="w"> </span><span class="p">(</span><span class="nf">sel-sort</span><span class="w"> </span><span class="nb">list</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="nf">maplist</span><span class="w"> </span><span class="p">(</span><span class="k">lambda</span><span class="p">(</span><span class="w"> </span><span class="nv">L</span><span class="p">)</span><span class="w"> </span> <span class="w"> </span><span class="p">(</span><span class="nb">first</span><span class="w"> </span><span class="p">(</span><span class="nf">list-swap!</span><span class="w"> </span><span class="nv">L</span><span class="w"> </span><span class="p">(</span><span class="nb">first</span><span class="w"> </span><span class="nv">L</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nb">apply</span><span class="w"> </span><span class="nb">min</span><span class="w"> </span><span class="nv">L</span><span class="w"> </span><span class="p">))))</span><span class="w"> </span><span class="nb">list</span><span class="p">))</span> <span class="w"> </span> <span class="p">(</span><span class="nf">sel-sort</span><span class="w"> </span><span class="p">(</span><span class="nf">shuffle</span><span class="w"> </span><span class="p">(</span><span class="nb">iota</span><span class="w"> </span><span class="mi">13</span><span class="p">)))</span> <span class="w"> </span><span class="err">→</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="mi">6</span><span class="w"> </span><span class="mi">7</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="mi">9</span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="mi">11</span><span class="w"> </span><span class="mi">12</span><span class="p">)</span> </pre></div> <h3><span class="mw-headline" id="Array_sort_2">Array sort</span></h3> <div class="mw-highlight mw-highlight-lang-scheme mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">;; sort an array in place</span> <span class="p">(</span><span class="k">define</span><span class="w"> </span><span class="p">(</span><span class="nf">sel-sort</span><span class="w"> </span><span class="nv">a</span><span class="w"> </span><span class="p">(</span><span class="nf">amin</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nf">imin</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="k">define</span><span class="w"> </span><span class="nv">ilast</span><span class="w"> </span><span class="p">(</span><span class="nb">1-</span><span class="w"> </span><span class="p">(</span><span class="nb">vector-length</span><span class="w"> </span><span class="nv">a</span><span class="p">)))</span> <span class="w"> </span><span class="p">(</span><span class="nf">for</span><span class="w"> </span><span class="p">[(</span><span class="nf">i</span><span class="w"> </span><span class="nv">ilast</span><span class="p">)]</span> <span class="w"> </span><span class="p">(</span><span class="k">set!</span><span class="w"> </span><span class="nv">amin</span><span class="w"> </span><span class="p">[</span><span class="nv">a</span><span class="w"> </span><span class="p">(</span><span class="nf">setv!</span><span class="w"> </span><span class="nv">imin</span><span class="w"> </span><span class="nv">i</span><span class="p">)])</span><span class="w"> </span><span class="c1">;; imin := i , amin := a[imin]</span> <span class="w"> </span><span class="p">(</span><span class="nf">for</span><span class="w"> </span><span class="p">[(</span><span class="nf">j</span><span class="w"> </span><span class="p">(</span><span class="nf">in-range</span><span class="w"> </span><span class="p">(</span><span class="nb">1+</span><span class="w"> </span><span class="nv">i</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nb">1+</span><span class="w"> </span><span class="nv">ilast</span><span class="p">)))]</span> <span class="w"> </span><span class="p">(</span><span class="k">when</span><span class="w"> </span><span class="p">(</span><span class="nb"><</span><span class="w"> </span><span class="p">[</span><span class="nv">a</span><span class="w"> </span><span class="nv">j</span><span class="p">]</span><span class="w"> </span><span class="nv">amin</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="k">set!</span><span class="w"> </span><span class="nv">amin</span><span class="w"> </span><span class="p">[</span><span class="nv">a</span><span class="w"> </span><span class="p">(</span><span class="nf">setv!</span><span class="w"> </span><span class="nv">imin</span><span class="w"> </span><span class="nv">j</span><span class="p">)])))</span> <span class="w"> </span><span class="p">(</span><span class="nf">vector-swap!</span><span class="w"> </span><span class="nv">a</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="nv">imin</span><span class="p">))</span> <span class="w"> </span><span class="nv">a</span><span class="w"> </span><span class="p">)</span> <span class="w"> </span> <span class="p">(</span><span class="k">define</span><span class="w"> </span><span class="nv">a</span><span class="w"> </span><span class="o">#</span><span class="p">(</span><span class="mi">9</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">6</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="mi">4</span><span class="p">))</span> <span class="p">(</span><span class="nf">sel-sort</span><span class="w"> </span><span class="nv">a</span><span class="p">)</span> <span class="w"> </span><span class="err">→</span><span class="w"> </span><span class="o">#</span><span class="p">(</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="mi">6</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="mi">9</span><span class="p">)</span> </pre></div> <h2><span class="mw-headline" id="Eiffel"><a href="/wiki/Category:Eiffel" title="Category:Eiffel">Eiffel</a> </span></h2> <div class="mw-highlight mw-highlight-lang-eiffel mw-content-ltr" dir="ltr"><pre><span></span><span class="kr">class</span> <span class="w"> </span><span class="nc">SELECTION_SORT</span><span class="w"> </span><span class="o">[</span><span class="nc">G</span><span class="w"> </span><span class="o">-></span><span class="w"> </span><span class="nc">COMPARABLE</span><span class="o">]</span> <span class="kr">feature</span><span class="w"> </span><span class="p">{</span><span class="kr">NONE</span><span class="p">}</span> <span class="w"> </span><span class="n">index_of_min</span><span class="w"> </span><span class="p">(</span><span class="n">ar</span><span class="p">:</span><span class="w"> </span><span class="nc">ARRAY</span><span class="w"> </span><span class="o">[</span><span class="nc">G</span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="n">lower</span><span class="p">:</span><span class="w"> </span><span class="nc">INTEGER</span><span class="p">):</span><span class="w"> </span><span class="nc">INTEGER</span> <span class="w"> </span><span class="c1">--Index of smallest element in 'ar' in the range of lower and the max index.</span> <span class="w"> </span><span class="kr">require</span> <span class="w"> </span><span class="n">lower_positiv</span><span class="p">:</span><span class="w"> </span><span class="n">lower</span><span class="w"> </span><span class="o">>=</span><span class="w"> </span><span class="mi">1</span> <span class="w"> </span><span class="n">lower_in_range</span><span class="p">:</span><span class="w"> </span><span class="n">lower</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">ar</span><span class="p">.</span><span class="n">count</span> <span class="w"> </span><span class="n">ar_not_void</span><span class="p">:</span><span class="w"> </span><span class="n">ar</span><span class="w"> </span><span class="o">/=</span><span class="w"> </span><span class="kc">Void</span> <span class="w"> </span><span class="kr">local</span> <span class="w"> </span><span class="n">i</span><span class="p">:</span><span class="w"> </span><span class="nc">INTEGER</span> <span class="w"> </span><span class="n">min</span><span class="p">:</span><span class="w"> </span><span class="nc">G</span> <span class="w"> </span><span class="kr">do</span> <span class="w"> </span><span class="kr">from</span> <span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">lower</span> <span class="w"> </span><span class="n">min</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">ar</span><span class="p">.</span><span class="n">item</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="w"> </span><span class="kc">Result</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">i</span> <span class="w"> </span><span class="kr">until</span> <span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">ar</span><span class="p">.</span><span class="n">count</span> <span class="w"> </span><span class="kr">loop</span> <span class="w"> </span><span class="kr">if</span><span class="w"> </span><span class="n">ar</span><span class="p">.</span><span class="n">item</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">min</span><span class="w"> </span><span class="kr">then</span> <span class="w"> </span><span class="n">min</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">ar</span><span class="p">.</span><span class="n">item</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span> <span class="w"> </span><span class="kc">Result</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="kr">ensure</span> <span class="w"> </span><span class="n">result_is_set</span><span class="p">:</span><span class="w"> </span><span class="kc">Result</span><span class="w"> </span><span class="o">/=</span><span class="w"> </span><span class="kc">Void</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="n">sort</span><span class="w"> </span><span class="p">(</span><span class="n">ar</span><span class="p">:</span><span class="w"> </span><span class="nc">ARRAY</span><span class="w"> </span><span class="o">[</span><span class="nc">G</span><span class="o">]</span><span class="p">):</span><span class="w"> </span><span class="nc">ARRAY</span><span class="w"> </span><span class="o">[</span><span class="nc">G</span><span class="o">]</span> <span class="w"> </span><span class="c1">-- sort array ar with selectionsort</span> <span class="w"> </span><span class="kr">require</span> <span class="w"> </span><span class="n">ar_not_void</span><span class="p">:</span><span class="w"> </span><span class="n">ar</span><span class="w"> </span><span class="o">/=</span><span class="w"> </span><span class="kc">Void</span> <span class="w"> </span><span class="kr">local</span> <span class="w"> </span><span class="n">min_index</span><span class="p">:</span><span class="w"> </span><span class="nc">INTEGER</span> <span class="w"> </span><span class="n">ith</span><span class="p">:</span><span class="w"> </span><span class="nc">G</span> <span class="w"> </span><span class="kr">do</span> <span class="w"> </span><span class="kr">create</span><span class="w"> </span><span class="kc">Result</span><span class="p">.</span><span class="n">make_empty</span> <span class="w"> </span><span class="kc">Result</span><span class="p">.</span><span class="n">deep_copy</span><span class="w"> </span><span class="p">(</span><span class="n">ar</span><span class="p">)</span> <span class="w"> </span><span class="kr">across</span> <span class="w"> </span><span class="kc">Result</span><span class="w"> </span><span class="kr">as</span><span class="w"> </span><span class="n">ic</span> <span class="w"> </span><span class="kr">loop</span> <span class="w"> </span><span class="n">min_index</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">index_of_min</span><span class="w"> </span><span class="p">(</span><span class="kc">Result</span><span class="p">,</span><span class="w"> </span><span class="n">ic</span><span class="p">.</span><span class="n">cursor_index</span><span class="p">)</span> <span class="w"> </span><span class="n">ith</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="kc">Result</span><span class="w"> </span><span class="o">[</span><span class="n">ic</span><span class="p">.</span><span class="n">cursor_index</span><span class="o">]</span> <span class="w"> </span><span class="kc">Result</span><span class="w"> </span><span class="o">[</span><span class="n">ic</span><span class="p">.</span><span class="n">cursor_index</span><span class="o">]</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="kc">Result</span><span class="w"> </span><span class="o">[</span><span class="n">min_index</span><span class="o">]</span> <span class="w"> </span><span class="kc">Result</span><span class="w"> </span><span class="o">[</span><span class="n">min_index</span><span class="o">]</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">ith</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="kr">ensure</span> <span class="w"> </span><span class="n">Result_is_set</span><span class="p">:</span><span class="w"> </span><span class="kc">Result</span><span class="w"> </span><span class="o">/=</span><span class="w"> </span><span class="kc">Void</span> <span class="w"> </span><span class="n">Result_sorted</span><span class="p">:</span><span class="w"> </span><span class="n">is_sorted</span><span class="w"> </span><span class="p">(</span><span class="kc">Result</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">True</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="n">is_sorted</span><span class="w"> </span><span class="p">(</span><span class="n">ar</span><span class="p">:</span><span class="w"> </span><span class="nc">ARRAY</span><span class="w"> </span><span class="o">[</span><span class="nc">G</span><span class="o">]</span><span class="p">):</span><span class="w"> </span><span class="nc">BOOLEAN</span> <span class="w"> </span><span class="c1">--- Is 'ar' sorted in ascending order?</span> <span class="w"> </span><span class="kr">require</span> <span class="w"> </span><span class="n">ar_not_empty</span><span class="p">:</span><span class="w"> </span><span class="n">ar</span><span class="p">.</span><span class="n">is_empty</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">False</span> <span class="w"> </span><span class="kr">local</span> <span class="w"> </span><span class="n">i</span><span class="p">:</span><span class="w"> </span><span class="nc">INTEGER</span> <span class="w"> </span><span class="kr">do</span> <span class="w"> </span><span class="kc">Result</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="kc">True</span> <span class="w"> </span><span class="kr">from</span> <span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">ar</span><span class="p">.</span><span class="n">lower</span> <span class="w"> </span><span class="kr">until</span> <span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ar</span><span class="p">.</span><span class="n">upper</span> <span class="w"> </span><span class="kr">loop</span> <span class="w"> </span><span class="kr">if</span><span class="w"> </span><span class="n">ar</span><span class="w"> </span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">ar</span><span class="w"> </span><span class="o">[</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="o">]</span><span class="w"> </span><span class="kr">then</span> <span class="w"> </span><span class="kc">Result</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="kc">False</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="kr">end</span> <span class="kr">feature</span> <span class="w"> </span><span class="n">selectionsort</span><span class="w"> </span><span class="p">(</span><span class="n">ar</span><span class="p">:</span><span class="w"> </span><span class="nc">ARRAY</span><span class="w"> </span><span class="o">[</span><span class="nc">G</span><span class="o">]</span><span class="p">):</span><span class="w"> </span><span class="nc">ARRAY</span><span class="w"> </span><span class="o">[</span><span class="nc">G</span><span class="o">]</span> <span class="w"> </span><span class="kr">do</span> <span class="w"> </span><span class="kc">Result</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">sort</span><span class="w"> </span><span class="p">(</span><span class="n">ar</span><span class="p">)</span> <span class="w"> </span><span class="kr">end</span> <span class="kr">end</span> </pre></div> <p>Test: </p> <div class="mw-highlight mw-highlight-lang-eiffel mw-content-ltr" dir="ltr"><pre><span></span><span class="kr">class</span> <span class="w"> </span><span class="nc">APPLICATION</span> <span class="kr">create</span> <span class="w"> </span><span class="n">make</span> <span class="kr">feature</span> <span class="w"> </span><span class="n">make</span> <span class="w"> </span><span class="kr">do</span> <span class="w"> </span><span class="n">test</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="o"><<</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">27</span><span class="p">,</span><span class="w"> </span><span class="mi">32</span><span class="p">,</span><span class="w"> </span><span class="mi">99</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="o">>></span> <span class="w"> </span><span class="n">io</span><span class="p">.</span><span class="n">put_string</span><span class="w"> </span><span class="p">(</span><span class="s">"Unsorted: "</span><span class="p">)</span> <span class="w"> </span><span class="kr">across</span> <span class="w"> </span><span class="n">test</span><span class="w"> </span><span class="kr">as</span><span class="w"> </span><span class="n">ic</span> <span class="w"> </span><span class="kr">loop</span> <span class="w"> </span><span class="n">io</span><span class="p">.</span><span class="n">put_string</span><span class="w"> </span><span class="p">(</span><span class="n">ic</span><span class="p">.</span><span class="n">item</span><span class="p">.</span><span class="n">out</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">" "</span><span class="p">)</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="kr">create</span><span class="w"> </span><span class="n">selectionsort</span> <span class="w"> </span><span class="n">io</span><span class="p">.</span><span class="n">put_string</span><span class="w"> </span><span class="p">(</span><span class="s">"%NSorted: "</span><span class="p">)</span> <span class="w"> </span><span class="n">test</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">selectionsort</span><span class="p">.</span><span class="n">selectionsort</span><span class="w"> </span><span class="p">(</span><span class="n">test</span><span class="p">)</span> <span class="w"> </span><span class="kr">across</span> <span class="w"> </span><span class="n">test</span><span class="w"> </span><span class="kr">as</span><span class="w"> </span><span class="n">ar</span> <span class="w"> </span><span class="kr">loop</span> <span class="w"> </span><span class="n">io</span><span class="p">.</span><span class="n">put_string</span><span class="w"> </span><span class="p">(</span><span class="n">ar</span><span class="p">.</span><span class="n">item</span><span class="p">.</span><span class="n">out</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s">" "</span><span class="p">)</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="n">test</span><span class="p">:</span><span class="w"> </span><span class="nc">ARRAY</span><span class="w"> </span><span class="o">[</span><span class="nc">INTEGER</span><span class="o">]</span> <span class="w"> </span><span class="n">selectionsort</span><span class="p">:</span><span class="w"> </span><span class="nc">SELECTION_SORT</span><span class="w"> </span><span class="o">[</span><span class="nc">INTEGER</span><span class="o">]</span> <span class="kr">end</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>Unsorted: 1 27 32 99 1 -7 3 5 7 Sorted: -7 1 1 3 5 7 27 32 99 </pre> <h2><span class="mw-headline" id="Elena"><a href="/wiki/Category:Elena" title="Category:Elena">Elena</a> </span></h2> <p>ELENA 6.x : </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>import extensions; import system'routines; extension op { selectionSort() { var copy := self.clone(); for(int i := 0; i < copy.Length; i += 1) { int k := i; for(int j := i + 1; j < copy.Length; j += 1) { if (copy[j] < copy[k]) { k := j } }; copy.exchange(i,k) }; ^ copy } } public program() { var list := new string[]{"this", "is", "a", "test", "of", "generic", "selection", "sort"}; console.printLine("before:",list.asEnumerable()); console.printLine("after:",list.selectionSort().asEnumerable()) }</pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>before:this,is,a,test,of,generic,selection,sort after:a,generic,is,of,selection,sort,test,this </pre> <h2><span class="mw-headline" id="Elixir"><a href="/wiki/Category:Elixir" title="Category:Elixir">Elixir</a> </span></h2> <div class="mw-highlight mw-highlight-lang-elixir mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">defmodule</span><span class="w"> </span><span class="nc">Sort</span><span class="w"> </span><span class="k">do</span> <span class="w"> </span><span class="kd">def</span><span class="w"> </span><span class="n">selection_sort</span><span class="p">(</span><span class="n">list</span><span class="p">)</span><span class="w"> </span><span class="ow">when</span><span class="w"> </span><span class="n">is_list</span><span class="p">(</span><span class="n">list</span><span class="p">),</span><span class="w"> </span><span class="ss">do</span><span class="p">:</span><span class="w"> </span><span class="n">selection_sort</span><span class="p">(</span><span class="n">list</span><span class="p">,</span><span class="w"> </span><span class="p">[])</span> <span class="w"> </span> <span class="w"> </span><span class="kd">defp</span><span class="w"> </span><span class="n">selection_sort</span><span class="p">([],</span><span class="w"> </span><span class="n">sorted</span><span class="p">),</span><span class="w"> </span><span class="ss">do</span><span class="p">:</span><span class="w"> </span><span class="n">sorted</span> <span class="w"> </span><span class="kd">defp</span><span class="w"> </span><span class="n">selection_sort</span><span class="p">(</span><span class="n">list</span><span class="p">,</span><span class="w"> </span><span class="n">sorted</span><span class="p">)</span><span class="w"> </span><span class="k">do</span> <span class="w"> </span><span class="n">max</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Enum</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">list</span><span class="p">)</span> <span class="w"> </span><span class="n">selection_sort</span><span class="p">(</span><span class="nc">List</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">list</span><span class="p">,</span><span class="w"> </span><span class="n">max</span><span class="p">),</span><span class="w"> </span><span class="p">[</span><span class="n">max</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">sorted</span><span class="p">])</span> <span class="w"> </span><span class="k">end</span> <span class="k">end</span> </pre></div> <p>Example: </p> <pre>iex(10)> Sort.selection_sort([5,3,9,4,1,6,8,2,7]) [1, 2, 3, 4, 5, 6, 7, 8, 9] </pre> <h2><span class="mw-headline" id="Erlang"><a href="/wiki/Category:Erlang" title="Category:Erlang">Erlang</a> </span></h2> <div class="mw-highlight mw-highlight-lang-erlang mw-content-ltr" dir="ltr"><pre><span></span><span class="p">-</span><span class="ni">module</span><span class="p">(</span><span class="n">solution</span><span class="p">).</span> <span class="p">-</span><span class="ni">import</span><span class="p">(</span><span class="n">lists</span><span class="p">,[</span><span class="n">delete</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span><span class="n">max</span><span class="o">/</span><span class="mi">1</span><span class="p">]).</span> <span class="p">-</span><span class="ni">compile</span><span class="p">(</span><span class="n">export_all</span><span class="p">).</span> <span class="nf">selection_sort</span><span class="p">([],</span><span class="nv">Sort</span><span class="p">)</span><span class="o">-></span><span class="w"> </span><span class="nv">Sort</span><span class="p">;</span> <span class="nf">selection_sort</span><span class="p">(</span><span class="nv">Ar</span><span class="p">,</span><span class="nv">Sort</span><span class="p">)</span><span class="o">-></span> <span class="w"> </span><span class="nv">M</span><span class="o">=</span><span class="n">max</span><span class="p">(</span><span class="nv">Ar</span><span class="p">),</span> <span class="w"> </span><span class="nv">Ad</span><span class="o">=</span><span class="n">delete</span><span class="p">(</span><span class="nv">M</span><span class="p">,</span><span class="nv">Ar</span><span class="p">),</span> <span class="w"> </span><span class="n">selection_sort</span><span class="p">(</span><span class="nv">Ad</span><span class="p">,[</span><span class="nv">M</span><span class="p">|</span><span class="nv">Sort</span><span class="p">]).</span> <span class="nf">print_array</span><span class="p">([])</span><span class="o">-></span><span class="n">ok</span><span class="p">;</span> <span class="nf">print_array</span><span class="p">([</span><span class="nv">H</span><span class="p">|</span><span class="nv">T</span><span class="p">])</span><span class="o">-></span> <span class="w"> </span><span class="nn">io</span><span class="p">:</span><span class="nf">format</span><span class="p">(</span><span class="s">"</span><span class="si">~p~n</span><span class="s">"</span><span class="p">,[</span><span class="nv">H</span><span class="p">]),</span> <span class="w"> </span><span class="n">print_array</span><span class="p">(</span><span class="nv">T</span><span class="p">).</span><span class="w"> </span> <span class="nf">main</span><span class="p">()</span><span class="o">-></span> <span class="w"> </span><span class="nv">Ans</span><span class="o">=</span><span class="n">selection_sort</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">10</span><span class="p">],[]),</span> <span class="w"> </span><span class="n">print_array</span><span class="p">(</span><span class="nv">Ans</span><span class="p">).</span> </pre></div> <h2><span class="mw-headline" id="Euphoria"><a href="/wiki/Category:Euphoria" title="Category:Euphoria">Euphoria</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>function selection_sort(sequence s) object tmp integer m for i = 1 to length(s) do m = i for j = i+1 to length(s) do if compare(s[j],s[m]) < 0 then m = j end if end for tmp = s[i] s[i] = s[m] s[m] = tmp end for return s end function include misc.e constant s = {4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1} puts(1,"Before: ") pretty_print(1,s,{2}) puts(1,"\nAfter: ") pretty_print(1,selection_sort(s),{2})</pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>Before: { 4, 15, "delta", 2, -31, 0, "alfa", 19, "gamma", 2, 13, "beta", 782, 1 } After: { -31, 0, 1, 2, 2, 4, 13, 15, 19, 782, "alfa", "beta", "delta", "gamma" }</pre> <h2><span id="F.23"></span><span class="mw-headline" id="F#"><a href="/wiki/Category:F_Sharp" title="Category:F Sharp">F#</a> </span></h2> <div class="mw-highlight mw-highlight-lang-fsharp mw-content-ltr" dir="ltr"><pre><span></span><span class="k">let</span><span class="w"> </span><span class="nv">rec</span><span class="w"> </span><span class="n">ssort</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">function</span> <span class="w"> </span><span class="bp">[]</span><span class="w"> </span><span class="o">-></span><span class="w"> </span><span class="bp">[]</span> <span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">x</span><span class="o">::</span><span class="n">xs</span><span class="w"> </span><span class="o">-></span><span class="w"> </span> <span class="w"> </span><span class="k">let</span><span class="w"> </span><span class="nv">min</span><span class="o">,</span><span class="w"> </span><span class="n">rest</span><span class="w"> </span><span class="o">=</span> <span class="w"> </span><span class="nn">List</span><span class="p">.</span><span class="n">fold</span><span class="w"> </span><span class="o">(</span><span class="k">fun</span><span class="w"> </span><span class="o">(</span><span class="n">min</span><span class="o">,</span><span class="n">acc</span><span class="o">)</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">-></span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">x</span><span class="o"><</span><span class="n">min</span><span class="w"> </span><span class="k">then</span><span class="w"> </span><span class="o">(</span><span class="n">x</span><span class="o">,</span><span class="w"> </span><span class="n">min</span><span class="o">::</span><span class="n">acc</span><span class="o">)</span> <span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="o">(</span><span class="n">min</span><span class="o">,</span><span class="w"> </span><span class="n">x</span><span class="o">::</span><span class="n">acc</span><span class="o">))</span> <span class="w"> </span><span class="o">(</span><span class="n">x</span><span class="o">,</span><span class="w"> </span><span class="bp">[]</span><span class="o">)</span><span class="w"> </span><span class="n">xs</span> <span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">min</span><span class="o">::</span><span class="n">ssort</span><span class="w"> </span><span class="n">rest</span> </pre></div> <h2><span class="mw-headline" id="Factor"><a href="/wiki/Category:Factor" title="Category:Factor">Factor</a> </span></h2> <div class="mw-highlight mw-highlight-lang-factor mw-content-ltr" dir="ltr"><pre><span></span><span class="kn">USING:</span><span class="w"> </span><span class="nn">kernel</span><span class="w"> </span><span class="nn">math</span><span class="w"> </span><span class="nn">sequences</span><span class="w"> </span><span class="nn">sequences.extras</span><span class="w"> </span><span class="k">;</span> <span class="k">:</span><span class="w"> </span><span class="nf">select</span><span class="w"> </span><span class="nf">(</span><span class="w"> </span><span class="nv">m</span><span class="w"> </span><span class="nv">n</span><span class="w"> </span><span class="nv">seq</span><span class="w"> </span><span class="nf">--</span><span class="w"> </span><span class="nf">)</span> <span class="w"> </span>[<span class="w"> </span><span class="nb">dup</span><span class="w"> </span>]<span class="w"> </span><span class="nb">2dip</span><span class="w"> </span>[<span class="w"> </span><span class="nb"><slice></span><span class="w"> </span>[<span class="w"> </span>]<span class="w"> </span>infimum-by*<span class="w"> </span><span class="nb">drop</span><span class="w"> </span><span class="nb">over</span><span class="w"> </span><span class="nb">+</span><span class="w"> </span>] <span class="w"> </span>[<span class="w"> </span><span class="nb">exchange</span><span class="w"> </span>]<span class="w"> </span><span class="nb">bi</span><span class="w"> </span><span class="k">;</span> <span class="k">:</span><span class="w"> </span><span class="nf">selection-sort!</span><span class="w"> </span><span class="nf">(</span><span class="w"> </span><span class="nv">seq</span><span class="w"> </span><span class="nf">--</span><span class="w"> </span><span class="nv">seq'</span><span class="w"> </span><span class="nf">)</span> <span class="w"> </span>[<span class="w"> </span>]<span class="w"> </span>[<span class="w"> </span><span class="nb">length</span><span class="w"> </span><span class="nb">dup</span><span class="w"> </span>]<span class="w"> </span>[<span class="w"> </span>]<span class="w"> </span><span class="nb">tri</span><span class="w"> </span>[<span class="w"> </span>select<span class="w"> </span>]<span class="w"> </span><span class="nb">2curry</span><span class="w"> </span><span class="nb">each-integer</span><span class="w"> </span><span class="k">;</span> </pre></div> <p>Example use </p> <div class="mw-highlight mw-highlight-lang-factor mw-content-ltr" dir="ltr"><pre><span></span><span class="kn">IN:</span><span class="w"> </span><span class="nn">scratchpad</span><span class="w"> </span>{<span class="w"> </span><span class="m">5 -6 3 9 -2 4 -1 -6 5 -5 </span>}<span class="w"> </span>selection-sort! ---<span class="w"> </span>Data<span class="w"> </span>stack: {<span class="w"> </span><span class="m">-6 -6 -5 -2 -1 3 4 5 5 9 </span>} </pre></div> <h2><span class="mw-headline" id="Forth"><a href="/wiki/Category:Forth" title="Category:Forth">Forth</a> </span></h2> <div class="mw-highlight mw-highlight-lang-forth mw-content-ltr" dir="ltr"><pre><span></span><span class="k">defer</span><span class="w"> </span><span class="nf">less?</span><span class="w"> </span><span class="nf">'</span><span class="w"> </span><span class="k"><</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="nf">less?</span> <span class="kn">:</span><span class="w"> </span><span class="nc">least</span><span class="w"> </span><span class="c1">( start end -- least )</span> <span class="w"> </span><span class="k">over</span><span class="w"> </span><span class="k">cell+</span><span class="w"> </span><span class="k">do</span> <span class="w"> </span><span class="k">i</span><span class="w"> </span><span class="k">@</span><span class="w"> </span><span class="k">over</span><span class="w"> </span><span class="k">@</span><span class="w"> </span><span class="nf">less?</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="k">drop</span><span class="w"> </span><span class="k">i</span><span class="w"> </span><span class="k">then</span> <span class="w"> </span><span class="nf">cell</span><span class="w"> </span><span class="k">+loop</span><span class="w"> </span><span class="k">;</span> <span class="kn">:</span><span class="w"> </span><span class="nc">selection</span><span class="w"> </span><span class="c1">( array len -- )</span> <span class="w"> </span><span class="k">cells</span><span class="w"> </span><span class="k">over</span><span class="w"> </span><span class="k">+</span><span class="w"> </span><span class="k">tuck</span><span class="w"> </span><span class="c1">( end start end )</span> <span class="w"> </span><span class="nf">cell-</span><span class="w"> </span><span class="k">swap</span><span class="w"> </span><span class="k">do</span><span class="w"> </span><span class="c1">( end )</span> <span class="w"> </span><span class="k">i</span><span class="w"> </span><span class="k">over</span><span class="w"> </span><span class="nf">least</span><span class="w"> </span><span class="c1">( end least )</span> <span class="w"> </span><span class="k">i</span><span class="w"> </span><span class="k">@</span><span class="w"> </span><span class="k">over</span><span class="w"> </span><span class="k">@</span><span class="w"> </span><span class="k">i</span><span class="w"> </span><span class="k">!</span><span class="w"> </span><span class="k">swap</span><span class="w"> </span><span class="k">!</span> <span class="w"> </span><span class="nf">cell</span><span class="w"> </span><span class="k">+loop</span><span class="w"> </span><span class="k">drop</span><span class="w"> </span><span class="k">;</span> <span class="k">create</span><span class="w"> </span><span class="nf">array</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="k">,</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">,</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="k">,</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="k">,</span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="k">,</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="k">,</span><span class="w"> </span><span class="mi">7</span><span class="w"> </span><span class="k">,</span><span class="w"> </span><span class="mi">9</span><span class="w"> </span><span class="k">,</span><span class="w"> </span><span class="mi">6</span><span class="w"> </span><span class="k">,</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="k">,</span> <span class="w"> </span> <span class="nf">array</span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="nf">selection</span> <span class="nf">array</span><span class="w"> </span><span class="mi">10</span><span class="w"> </span><span class="k">cells</span><span class="w"> </span><span class="k">dump</span> </pre></div> <h2><span class="mw-headline" id="Fortran"><a href="/wiki/Category:Fortran" title="Category:Fortran">Fortran</a> </span></h2> <div class="examplemeta workswith"><b>Works with</b>: <a href="/wiki/Fortran" class="mw-redirect" title="Fortran">Fortran</a> version 95 and later</div> <div class="mw-highlight mw-highlight-lang-fortran mw-content-ltr" dir="ltr"><pre><span></span><span class="k">PROGRAM </span><span class="n">SELECTION</span> <span class="w"> </span><span class="k">IMPLICIT NONE</span> <span class="k"> </span><span class="kt">INTEGER</span><span class="w"> </span><span class="kd">::</span><span class="w"> </span><span class="n">intArray</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="o">/</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="w"> </span><span class="o">/</span><span class="p">)</span> <span class="w"> </span> <span class="w"> </span><span class="k">WRITE</span><span class="p">(</span><span class="o">*</span><span class="p">,</span><span class="s2">"(A,10I5)"</span><span class="p">)</span><span class="w"> </span><span class="s2">"Unsorted array:"</span><span class="p">,</span><span class="w"> </span><span class="n">intArray</span> <span class="w"> </span><span class="k">CALL </span><span class="n">Selection_sort</span><span class="p">(</span><span class="n">intArray</span><span class="p">)</span> <span class="w"> </span><span class="k">WRITE</span><span class="p">(</span><span class="o">*</span><span class="p">,</span><span class="s2">"(A,10I5)"</span><span class="p">)</span><span class="w"> </span><span class="s2">"Sorted array  :"</span><span class="p">,</span><span class="w"> </span><span class="n">intArray</span> <span class="w"> </span> <span class="k">CONTAINS</span> <span class="k"> </span> <span class="k"> SUBROUTINE </span><span class="n">Selection_sort</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="w"> </span><span class="kt">INTEGER</span><span class="p">,</span><span class="w"> </span><span class="k">INTENT</span><span class="p">(</span><span class="n">IN</span><span class="w"> </span><span class="n">OUT</span><span class="p">)</span><span class="w"> </span><span class="kd">::</span><span class="w"> </span><span class="n">a</span><span class="p">(:)</span> <span class="w"> </span><span class="kt">INTEGER</span><span class="w"> </span><span class="kd">::</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">minIndex</span><span class="p">,</span><span class="w"> </span><span class="n">temp</span> <span class="w"> </span><span class="k">DO </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">SIZE</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span> <span class="w"> </span><span class="n">minIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">MINLOC</span><span class="p">(</span><span class="n">a</span><span class="p">(</span><span class="n">i</span><span class="p">:),</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span> <span class="w"> </span><span class="k">IF</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">minIndex</span><span class="p">))</span><span class="w"> </span><span class="k">THEN</span> <span class="k"> </span><span class="n">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">minIndex</span><span class="p">)</span> <span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">minIndex</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">temp</span> <span class="w"> </span><span class="k">END IF</span> <span class="k"> END DO</span> <span class="k"> END SUBROUTINE </span><span class="n">Selection_sort</span> <span class="k">END PROGRAM </span><span class="n">SELECTION</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>Unsorted array: 4 9 3 -2 0 7 -5 1 6 8 Sorted array  : -5 -2 0 1 3 4 6 7 8 9 </pre> <h2><span class="mw-headline" id="FreeBASIC"><a href="/wiki/Category:FreeBASIC" title="Category:FreeBASIC">FreeBASIC</a> </span></h2> <div class="mw-highlight mw-highlight-lang-basic mw-content-ltr" dir="ltr"><pre><span></span><span class="c1">' version 03-12-2016</span> <span class="c1">' compile with: fbc -s console</span> <span class="c1">' for boundry checks on array's compile with: fbc -s console -exx</span> <span class="vg">Sub</span><span class="w"> </span><span class="vg">selectionsort</span><span class="p">(</span><span class="vg">arr</span><span class="p">()</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Long</span><span class="p">)</span> <span class="w"> </span><span class="c1">' sort from lower bound to the highter bound</span> <span class="w"> </span><span class="c1">' array's can have subscript range from -2147483648 to +2147483647</span> <span class="w"> </span><span class="vg">Dim</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Long</span><span class="w"> </span><span class="vg">i</span><span class="p">,</span><span class="w"> </span><span class="vg">j</span><span class="p">,</span><span class="w"> </span><span class="vg">x</span> <span class="w"> </span><span class="vg">Dim</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Long</span><span class="w"> </span><span class="vg">lb</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">LBound</span><span class="p">(</span><span class="vg">arr</span><span class="p">)</span> <span class="w"> </span><span class="vg">Dim</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Long</span><span class="w"> </span><span class="vg">ub</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">UBound</span><span class="p">(</span><span class="vg">arr</span><span class="p">)</span> <span class="w"> </span><span class="vg">For</span><span class="w"> </span><span class="vg">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">lb</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="vg">ub</span><span class="w"> </span><span class="il">-1</span> <span class="w"> </span><span class="vg">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">i</span> <span class="w"> </span><span class="vg">For</span><span class="w"> </span><span class="vg">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">i</span><span class="w"> </span><span class="o">+</span><span class="il">1</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="vg">ub</span> <span class="w"> </span><span class="vg">If</span><span class="w"> </span><span class="vg">arr</span><span class="p">(</span><span class="vg">j</span><span class="p">)</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="vg">arr</span><span class="p">(</span><span class="vg">x</span><span class="p">)</span><span class="w"> </span><span class="vg">Then</span><span class="w"> </span><span class="vg">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">j</span> <span class="w"> </span><span class="vg">Next</span> <span class="w"> </span><span class="vg">If</span><span class="w"> </span><span class="vg">x</span><span class="w"> </span><span class="o"><></span><span class="w"> </span><span class="vg">i</span><span class="w"> </span><span class="vg">Then</span> <span class="w"> </span><span class="vg">Swap</span><span class="w"> </span><span class="vg">arr</span><span class="p">(</span><span class="vg">i</span><span class="p">),</span><span class="w"> </span><span class="vg">arr</span><span class="p">(</span><span class="vg">x</span><span class="p">)</span> <span class="w"> </span><span class="vg">End</span><span class="w"> </span><span class="vg">If</span> <span class="w"> </span><span class="vg">Next</span> <span class="vg">End</span><span class="w"> </span><span class="vg">Sub</span> <span class="c1">' ------=< MAIN >=------</span> <span class="vg">Dim</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Long</span><span class="w"> </span><span class="vg">i</span><span class="p">,</span><span class="w"> </span><span class="vg">array</span><span class="p">(</span><span class="il">-7</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="il">7</span><span class="p">)</span> <span class="vg">Dim</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Long</span><span class="w"> </span><span class="vg">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">LBound</span><span class="p">(</span><span class="vg">array</span><span class="p">),</span><span class="w"> </span><span class="vg">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">UBound</span><span class="p">(</span><span class="vg">array</span><span class="p">)</span> <span class="vg">Randomize</span><span class="w"> </span><span class="vg">Timer</span> <span class="vg">For</span><span class="w"> </span><span class="vg">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">a</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="vg">b</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="vg">array</span><span class="p">(</span><span class="vg">i</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">i</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="vg">Next</span> <span class="vg">For</span><span class="w"> </span><span class="vg">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">a</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="vg">b</span><span class="w"> </span><span class="c1">' little shuffle</span> <span class="w"> </span><span class="vg">Swap</span><span class="w"> </span><span class="vg">array</span><span class="p">(</span><span class="vg">i</span><span class="p">),</span><span class="w"> </span><span class="vg">array</span><span class="p">(</span><span class="vg">Int</span><span class="p">(</span><span class="vg">Rnd</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="vg">b</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="vg">a</span><span class="w"> </span><span class="o">+</span><span class="il">1</span><span class="p">))</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="vg">a</span><span class="p">)</span> <span class="vg">Next</span> <span class="vg">Print</span><span class="w"> </span><span class="s2">"unsort "</span><span class="p">;</span> <span class="vg">For</span><span class="w"> </span><span class="vg">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">a</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="vg">b</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="vg">Print</span><span class="w"> </span><span class="vg">Using</span><span class="w"> </span><span class="s2">"####"</span><span class="p">;</span><span class="w"> </span><span class="vg">array</span><span class="p">(</span><span class="vg">i</span><span class="p">);</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="vg">Next</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="vg">Print</span> <span class="vg">selectionsort</span><span class="p">(</span><span class="vg">array</span><span class="p">())</span><span class="w"> </span><span class="c1">' sort the array</span> <span class="vg">Print</span><span class="w"> </span><span class="s2">" sort "</span><span class="p">;</span> <span class="vg">For</span><span class="w"> </span><span class="vg">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">a</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="vg">b</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="vg">Print</span><span class="w"> </span><span class="vg">Using</span><span class="w"> </span><span class="s2">"####"</span><span class="p">;</span><span class="w"> </span><span class="vg">array</span><span class="p">(</span><span class="vg">i</span><span class="p">);</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="vg">Next</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="vg">Print</span> <span class="c1">' empty keyboard buffer</span> <span class="vg">While</span><span class="w"> </span><span class="vg">InKey</span><span class="w"> </span><span class="o"><></span><span class="w"> </span><span class="s2">""</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="vg">Wend</span> <span class="vg">Print</span><span class="w"> </span><span class="o">:</span><span class="w"> </span><span class="vg">Print</span><span class="w"> </span><span class="s2">"hit any key to end program"</span> <span class="vg">Sleep</span> <span class="vg">End</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>unsort 1 -7 -5 -4 6 5 -3 4 2 0 3 -6 -2 7 -1 sort -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre> <h2><span class="mw-headline" id="Gambas"><a href="/wiki/Category:Gambas" title="Category:Gambas">Gambas</a> </span></h2> <div class="mw-highlight mw-highlight-lang-basic mw-content-ltr" dir="ltr"><pre><span></span><span class="vg">siLow</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Short</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">-99</span><span class="w"> </span><span class="c1">'Set the lowest value number to create</span> <span class="vg">siHigh</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Short</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">99</span><span class="w"> </span><span class="c1">'Set the highest value number to create</span> <span class="vg">siQty</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Short</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">20</span><span class="w"> </span><span class="c1">'Set the quantity of numbers to create</span> <span class="vg">Public</span><span class="w"> </span><span class="vg">Sub</span><span class="w"> </span><span class="vg">Main</span><span class="p">()</span> <span class="vg">Dim</span><span class="w"> </span><span class="vg">siToSort</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Short</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">CreateNumbersToSort</span><span class="p">()</span> <span class="vg">Dim</span><span class="w"> </span><span class="vg">siPos</span><span class="p">,</span><span class="w"> </span><span class="vg">siLow</span><span class="p">,</span><span class="w"> </span><span class="vg">siChar</span><span class="p">,</span><span class="w"> </span><span class="vg">siCount</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Short</span> <span class="vg">PrintOut</span><span class="p">(</span><span class="s2">"To sort: "</span><span class="p">,</span><span class="w"> </span><span class="vg">siToSort</span><span class="p">)</span> <span class="vg">For</span><span class="w"> </span><span class="vg">siCount</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">0</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="vg">siToSort</span><span class="o">.</span><span class="vg">Max</span> <span class="w"> </span><span class="vg">siChar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">siCount</span> <span class="w"> </span><span class="vg">For</span><span class="w"> </span><span class="vg">siPos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">siCount</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="il">1</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="vg">siToSort</span><span class="o">.</span><span class="vg">Max</span> <span class="w"> </span><span class="vg">If</span><span class="w"> </span><span class="vg">siToSort</span><span class="p">[</span><span class="vg">siChar</span><span class="p">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="vg">siToSort</span><span class="p">[</span><span class="vg">siPos</span><span class="p">]</span><span class="w"> </span><span class="vg">Then</span><span class="w"> </span><span class="vg">siChar</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">siPos</span> <span class="w"> </span><span class="vg">Next</span> <span class="w"> </span><span class="vg">siLow</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">siToSort</span><span class="p">[</span><span class="vg">siChar</span><span class="p">]</span> <span class="w"> </span><span class="vg">siToSort</span><span class="o">.</span><span class="vg">Delete</span><span class="p">(</span><span class="vg">siChar</span><span class="p">,</span><span class="w"> </span><span class="il">1</span><span class="p">)</span> <span class="w"> </span><span class="vg">siToSort</span><span class="o">.</span><span class="vg">Add</span><span class="p">(</span><span class="vg">siLow</span><span class="p">,</span><span class="w"> </span><span class="vg">siCount</span><span class="p">)</span> <span class="vg">Next</span> <span class="vg">PrintOut</span><span class="p">(</span><span class="s2">" Sorted: "</span><span class="p">,</span><span class="w"> </span><span class="vg">siToSort</span><span class="p">)</span> <span class="vg">End</span> <span class="c1">'---------------------------------------------------------</span> <span class="vg">Public</span><span class="w"> </span><span class="vg">Sub</span><span class="w"> </span><span class="vg">PrintOut</span><span class="p">(</span><span class="vg">sText</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">String</span><span class="p">,</span><span class="w"> </span><span class="vg">siToSort</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">String</span><span class="p">[])</span> <span class="vg">Dim</span><span class="w"> </span><span class="vg">siCount</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Short</span> <span class="vg">Print</span><span class="w"> </span><span class="vg">sText</span><span class="p">;</span> <span class="vg">For</span><span class="w"> </span><span class="vg">siCount</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">0</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="vg">siToSort</span><span class="o">.</span><span class="vg">Max</span> <span class="w"> </span><span class="vg">Print</span><span class="w"> </span><span class="vg">siToSort</span><span class="p">[</span><span class="vg">siCount</span><span class="p">];</span> <span class="w"> </span><span class="vg">If</span><span class="w"> </span><span class="vg">siCount</span><span class="w"> </span><span class="o"><></span><span class="w"> </span><span class="vg">siToSort</span><span class="o">.</span><span class="vg">max</span><span class="w"> </span><span class="vg">Then</span><span class="w"> </span><span class="vg">Print</span><span class="w"> </span><span class="s2">", "</span><span class="p">;</span> <span class="vg">Next</span> <span class="vg">Print</span> <span class="vg">End</span> <span class="c1">'---------------------------------------------------------</span> <span class="vg">Public</span><span class="w"> </span><span class="vg">Sub</span><span class="w"> </span><span class="vg">CreateNumbersToSort</span><span class="p">()</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Short</span><span class="p">[]</span> <span class="vg">Dim</span><span class="w"> </span><span class="vg">siCount</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">Short</span> <span class="vg">Dim</span><span class="w"> </span><span class="vg">siList</span><span class="w"> </span><span class="vg">As</span><span class="w"> </span><span class="vg">New</span><span class="w"> </span><span class="vg">Short</span><span class="p">[]</span> <span class="vg">For</span><span class="w"> </span><span class="vg">siCount</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">0</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="vg">siQty</span> <span class="w"> </span><span class="vg">siList</span><span class="o">.</span><span class="vg">Add</span><span class="p">(</span><span class="vg">Rand</span><span class="p">(</span><span class="vg">siLow</span><span class="p">,</span><span class="w"> </span><span class="vg">siHigh</span><span class="p">))</span> <span class="vg">Next</span> <span class="vg">Return</span><span class="w"> </span><span class="vg">siList</span> <span class="vg">End</span> </pre></div> <p>Output: </p> <pre>To sort: -11, -64, -20, -84, 94, -60, -82, -82, 37, -30, -75, 73, 19, -97, 81, -26, 55, 8, -15, -31, 36 Sorted: -97, -84, -82, -82, -75, -64, -60, -31, -30, -26, -20, -15, -11, 8, 19, 36, 37, 55, 73, 81, 94 </pre> <h2><span class="mw-headline" id="GAP"><a href="/wiki/Category:GAP" title="Category:GAP">GAP</a> </span></h2> <div class="mw-highlight mw-highlight-lang-gap mw-content-ltr" dir="ltr"><pre><span></span><span class="nv">SelectionSort</span> <span class="o">:=</span> <span class="k">function</span><span class="p">(</span><span class="nv">v</span><span class="p">)</span> <span class="k">local</span> <span class="nv">i</span><span class="o">,</span> <span class="nv">j</span><span class="o">,</span> <span class="nv">k</span><span class="o">,</span> <span class="nv">n</span><span class="o">,</span> <span class="nv">m</span><span class="o">;</span> <span class="nv">n</span> <span class="o">:=</span> <span class="nv">Size</span><span class="p">(</span><span class="nv">v</span><span class="p">)</span><span class="o">;</span> <span class="k">for</span> <span class="nv">i</span> <span class="ow">in</span> <span class="p">[</span><span class="nv">1</span> <span class="o">..</span> <span class="nv">n</span><span class="p">]</span> <span class="k">do</span> <span class="nv">k</span> <span class="o">:=</span> <span class="nv">i</span><span class="o">;</span> <span class="nv">m</span> <span class="o">:=</span> <span class="nv">v</span><span class="p">[</span><span class="nv">i</span><span class="p">]</span><span class="o">;</span> <span class="k">for</span> <span class="nv">j</span> <span class="ow">in</span> <span class="p">[</span><span class="nv">i</span> <span class="o">+</span> <span class="nv">1</span> <span class="o">..</span> <span class="nv">n</span><span class="p">]</span> <span class="k">do</span> <span class="k">if</span> <span class="nv">v</span><span class="p">[</span><span class="nv">j</span><span class="p">]</span> <span class="o"><</span> <span class="nv">m</span> <span class="k">then</span> <span class="nv">k</span> <span class="o">:=</span> <span class="nv">j</span><span class="o">;</span> <span class="nv">m</span> <span class="o">:=</span> <span class="nv">v</span><span class="p">[</span><span class="nv">j</span><span class="p">]</span><span class="o">;</span> <span class="k">fi</span><span class="o">;</span> <span class="k">od</span><span class="o">;</span> <span class="nv">v</span><span class="p">[</span><span class="nv">k</span><span class="p">]</span> <span class="o">:=</span> <span class="nv">v</span><span class="p">[</span><span class="nv">i</span><span class="p">]</span><span class="o">;</span> <span class="nv">v</span><span class="p">[</span><span class="nv">i</span><span class="p">]</span> <span class="o">:=</span> <span class="nv">m</span><span class="o">;</span> <span class="k">od</span><span class="o">;</span> <span class="k">end</span><span class="o">;</span> <span class="nv">v</span> <span class="o">:=</span> <span class="nv">List</span><span class="p">([</span><span class="nv">1</span> <span class="o">..</span> <span class="nv">100</span><span class="p">]</span><span class="o">,</span> <span class="nv">n</span> <span class="o">-></span> <span class="nv">Random</span><span class="p">([</span><span class="nv">1</span> <span class="o">..</span> <span class="nv">100</span><span class="p">]))</span><span class="o">;</span> <span class="nv">SelectionSort</span><span class="p">(</span><span class="nv">v</span><span class="p">)</span><span class="o">;</span> <span class="nv">v</span><span class="o">;</span> </pre></div> <h2><span class="mw-headline" id="Go"><a href="/wiki/Category:Go" title="Category:Go">Go</a> </span></h2> <div class="mw-highlight mw-highlight-lang-go mw-content-ltr" dir="ltr"><pre><span></span><span class="kn">package</span><span class="w"> </span><span class="nx">main</span> <span class="kn">import</span><span class="w"> </span><span class="s">"fmt"</span> <span class="kd">var</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="mi">170</span><span class="p">,</span><span class="w"> </span><span class="mi">45</span><span class="p">,</span><span class="w"> </span><span class="mi">75</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">90</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">802</span><span class="p">,</span><span class="w"> </span><span class="mi">24</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">66</span><span class="p">}</span> <span class="kd">func</span><span class="w"> </span><span class="nx">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"before:"</span><span class="p">,</span><span class="w"> </span><span class="nx">a</span><span class="p">)</span> <span class="w"> </span><span class="nx">selectionSort</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span> <span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"after: "</span><span class="p">,</span><span class="w"> </span><span class="nx">a</span><span class="p">)</span> <span class="p">}</span> <span class="kd">func</span><span class="w"> </span><span class="nx">selectionSort</span><span class="p">(</span><span class="nx">a</span><span class="w"> </span><span class="p">[]</span><span class="kt">int</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">last</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="nx">last</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">aMin</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">a</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="w"> </span><span class="nx">iMin</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">i</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="nb">len</span><span class="p">(</span><span class="nx">a</span><span class="p">);</span><span class="w"> </span><span class="nx">j</span><span class="o">++</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nx">a</span><span class="p">[</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="nx">aMin</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">aMin</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">a</span><span class="p">[</span><span class="nx">j</span><span class="p">]</span> <span class="w"> </span><span class="nx">iMin</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">j</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="nx">a</span><span class="p">[</span><span class="nx">i</span><span class="p">],</span><span class="w"> </span><span class="nx">a</span><span class="p">[</span><span class="nx">iMin</span><span class="p">]</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">aMin</span><span class="p">,</span><span class="w"> </span><span class="nx">a</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> </pre></div> <p>More generic version that sorts anything that implements <code>sort.Interface</code>: </p> <div class="mw-highlight mw-highlight-lang-go mw-content-ltr" dir="ltr"><pre><span></span><span class="kn">package</span><span class="w"> </span><span class="nx">main</span> <span class="kn">import</span><span class="w"> </span><span class="p">(</span> <span class="w"> </span><span class="s">"sort"</span> <span class="w"> </span><span class="s">"fmt"</span> <span class="p">)</span> <span class="kd">var</span><span class="w"> </span><span class="nx">a</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="mi">170</span><span class="p">,</span><span class="w"> </span><span class="mi">45</span><span class="p">,</span><span class="w"> </span><span class="mi">75</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">90</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">802</span><span class="p">,</span><span class="w"> </span><span class="mi">24</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">66</span><span class="p">}</span> <span class="kd">func</span><span class="w"> </span><span class="nx">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"before:"</span><span class="p">,</span><span class="w"> </span><span class="nx">a</span><span class="p">)</span> <span class="w"> </span><span class="nx">selectionSort</span><span class="p">(</span><span class="nx">sort</span><span class="p">.</span><span class="nx">IntSlice</span><span class="p">(</span><span class="nx">a</span><span class="p">))</span> <span class="w"> </span><span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="s">"after: "</span><span class="p">,</span><span class="w"> </span><span class="nx">a</span><span class="p">)</span> <span class="p">}</span> <span class="kd">func</span><span class="w"> </span><span class="nx">selectionSort</span><span class="p">(</span><span class="nx">a</span><span class="w"> </span><span class="nx">sort</span><span class="p">.</span><span class="nx">Interface</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">last</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">a</span><span class="p">.</span><span class="nx">Len</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="nx">last</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">iMin</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">i</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="nx">a</span><span class="p">.</span><span class="nx">Len</span><span class="p">();</span><span class="w"> </span><span class="nx">j</span><span class="o">++</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nx">a</span><span class="p">.</span><span class="nx">Less</span><span class="p">(</span><span class="nx">j</span><span class="p">,</span><span class="w"> </span><span class="nx">iMin</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nx">iMin</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nx">j</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="nx">a</span><span class="p">.</span><span class="nx">Swap</span><span class="p">(</span><span class="nx">i</span><span class="p">,</span><span class="w"> </span><span class="nx">iMin</span><span class="p">)</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="Haskell"><a href="/wiki/Category:Haskell" title="Category:Haskell">Haskell</a> </span></h2> <div class="mw-highlight mw-highlight-lang-haskell mw-content-ltr" dir="ltr"><pre><span></span><span class="kr">import</span><span class="w"> </span><span class="nn">Data.List</span><span class="w"> </span><span class="p">(</span><span class="nf">delete</span><span class="p">)</span> <span class="nf">selSort</span><span class="w"> </span><span class="ow">::</span><span class="w"> </span><span class="p">(</span><span class="kt">Ord</span><span class="w"> </span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="ow">=></span><span class="w"> </span><span class="p">[</span><span class="n">a</span><span class="p">]</span><span class="w"> </span><span class="ow">-></span><span class="w"> </span><span class="p">[</span><span class="n">a</span><span class="p">]</span> <span class="nf">selSort</span><span class="w"> </span><span class="kt">[]</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="kt">[]</span> <span class="nf">selSort</span><span class="w"> </span><span class="n">xs</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="n">selSort</span><span class="w"> </span><span class="p">(</span><span class="n">delete</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="n">xs</span><span class="p">)</span><span class="w"> </span><span class="o">++</span><span class="w"> </span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="w"> </span><span class="kr">where</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="ow">=</span><span class="w"> </span><span class="n">maximum</span><span class="w"> </span><span class="n">xs</span> </pre></div> <h2><span class="mw-headline" id="Haxe"><a href="/wiki/Category:Haxe" title="Category:Haxe">Haxe</a> </span></h2> <div class="mw-highlight mw-highlight-lang-haxe mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">class</span><span class="w"> </span><span class="n">SelectionSort</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nd">@:generic</span> <span class="w"> </span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nf">sort</span><span class="p"><</span><span class="n">T</span><span class="p">>(</span><span class="n">arr</span><span class="p">:</span><span class="n">Array</span><span class="p"><</span><span class="n">T</span><span class="p">>)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span>len<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arr</span><span class="p">.</span><span class="n">length</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">...</span><span class="n">len</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span>minIndex<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">index</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">remainingIndex</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">...</span><span class="n">len</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">Reflect</span><span class="p">.</span><span class="n">compare</span><span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="n">minIndex</span><span class="p">],</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">remainingIndex</span><span class="p">])</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">0</span><span class="p">)</span> <span class="w"> </span><span class="n">minIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">remainingIndex</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">minIndex</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span>temp<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="p">];</span> <span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">index</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">minIndex</span><span class="p">];</span> <span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">minIndex</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">temp</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> <span class="kd">class</span><span class="w"> </span><span class="n">Main</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span>integerArray<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">19</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">23</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">];</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span>floatArray<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mf">1.0</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">3.2</span><span class="p">,</span><span class="w"> </span><span class="mf">5.2</span><span class="p">,</span><span class="w"> </span><span class="mf">10.8</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">5.7</span><span class="p">,</span><span class="w"> </span><span class="mf">7.3</span><span class="p">,</span><span class="w"> </span> <span class="w"> </span><span class="mf">3.5</span><span class="p">,</span><span class="w"> </span><span class="mf">0.0</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">4.1</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">9.5</span><span class="p">];</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span>stringArray<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="s1">'We'</span><span class="p">,</span><span class="w"> </span><span class="s1">'hold'</span><span class="p">,</span><span class="w"> </span><span class="s1">'these'</span><span class="p">,</span><span class="w"> </span><span class="s1">'truths'</span><span class="p">,</span><span class="w"> </span><span class="s1">'to'</span><span class="p">,</span><span class="w"> </span> <span class="w"> </span><span class="s1">'be'</span><span class="p">,</span><span class="w"> </span><span class="s1">'self-evident'</span><span class="p">,</span><span class="w"> </span><span class="s1">'that'</span><span class="p">,</span><span class="w"> </span><span class="s1">'all'</span><span class="p">,</span><span class="w"> </span> <span class="w"> </span><span class="s1">'men'</span><span class="p">,</span><span class="w"> </span><span class="s1">'are'</span><span class="p">,</span><span class="w"> </span><span class="s1">'created'</span><span class="p">,</span><span class="w"> </span><span class="s1">'equal'</span><span class="p">];</span> <span class="w"> </span><span class="n">Sys</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s1">'Unsorted Integers:'</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">integerArray</span><span class="p">);</span> <span class="w"> </span><span class="n">SelectionSort</span><span class="p">.</span><span class="n">sort</span><span class="p">(</span><span class="n">integerArray</span><span class="p">);</span> <span class="w"> </span><span class="n">Sys</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s1">'Sorted Integers: '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">integerArray</span><span class="p">);</span> <span class="w"> </span><span class="n">Sys</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s1">'Unsorted Floats: '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">floatArray</span><span class="p">);</span> <span class="w"> </span><span class="n">SelectionSort</span><span class="p">.</span><span class="n">sort</span><span class="p">(</span><span class="n">floatArray</span><span class="p">);</span> <span class="w"> </span><span class="n">Sys</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s1">'Sorted Floats: '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">floatArray</span><span class="p">);</span> <span class="w"> </span><span class="n">Sys</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s1">'Unsorted Strings: '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">stringArray</span><span class="p">);</span> <span class="w"> </span><span class="n">SelectionSort</span><span class="p">.</span><span class="n">sort</span><span class="p">(</span><span class="n">stringArray</span><span class="p">);</span> <span class="w"> </span><span class="n">Sys</span><span class="p">.</span><span class="n">println</span><span class="p">(</span><span class="s1">'Sorted Strings: '</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">stringArray</span><span class="p">);</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>Unsorted Integers: [1,10,2,5,-1,5,-19,4,23,0] Sorted Integers: [-19,-1,0,1,2,4,5,5,10,23] Unsorted Floats: [1,-3.2,5.2,10.8,-5.7,7.3,3.5,0,-4.1,-9.5] Sorted Floats: [-9.5,-5.7,-4.1,-3.2,0,1,3.5,5.2,7.3,10.8] Unsorted Strings: [We,hold,these,truths,to,be,self-evident,that,all,men,are,created,equal] Sorted Strings: [We,all,are,be,created,equal,hold,men,self-evident,that,these,to,truths] </pre> <h2><span class="mw-headline" id="Icon_and_Unicon"><a href="/wiki/Category:Icon" title="Category:Icon">Icon</a> and <a href="/wiki/Category:Unicon" title="Category:Unicon">Unicon</a> </span></h2> <div class="mw-highlight mw-highlight-lang-icon mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">procedure</span> <span class="nf">main</span><span class="p">()</span> <span class="c1">#: demonstrate various ways to sort a list and string </span> <span class="n">demosort</span><span class="p">(</span><span class="n">selectionsort</span><span class="p">,[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span><span class="s">"qwerty"</span><span class="p">)</span> <span class="kr">end</span> <span class="kd">procedure</span> <span class="nf">selectionsort</span><span class="p">(</span><span class="nv">X</span><span class="p">,</span><span class="nv">op</span><span class="p">)</span> <span class="c1">#: return sorted list ascending(or descending)</span> <span class="kr">local</span> <span class="n">i</span><span class="p">,</span><span class="n">m</span> <span class="n">op</span> <span class="o">:=</span> <span class="n">sortop</span><span class="p">(</span><span class="n">op</span><span class="p">,</span><span class="n">X</span><span class="p">)</span> <span class="c1"># select how and what we sort</span> <span class="kr">every</span> <span class="n">i</span> <span class="o">:=</span> <span class="mi">1</span> <span class="kr">to</span> <span class="o">*</span><span class="n">X</span><span class="mi">-1</span> <span class="kr">do</span> <span class="p">{</span> <span class="n">m</span> <span class="o">:=</span> <span class="n">i</span> <span class="kr">every</span> <span class="n">j</span> <span class="o">:=</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span> <span class="kr">to</span> <span class="o">*</span><span class="n">X</span> <span class="kr">do</span> <span class="kr">if</span> <span class="n">op</span><span class="p">(</span><span class="n">X</span><span class="p">[</span><span class="n">j</span><span class="p">],</span><span class="n">X</span><span class="p">[</span><span class="n">m</span><span class="p">])</span> <span class="kr">then</span> <span class="n">m</span> <span class="o">:=</span> <span class="n">j</span> <span class="c1"># find X that belongs @i low (or high)</span> <span class="n">X</span><span class="p">[</span><span class="n">m</span> <span class="o">~=</span> <span class="n">i</span><span class="p">]</span> <span class="o">:=:</span> <span class="n">X</span><span class="p">[</span><span class="n">m</span><span class="p">]</span> <span class="p">}</span> <span class="kr">return</span> <span class="n">X</span> <span class="kr">end</span> </pre></div> <p>Note: This example relies on <a href="/wiki/Sorting_algorithms/Bubble_sort#Icon" title="Sorting algorithms/Bubble sort"> the supporting procedures 'sortop', and 'demosort' in Bubble Sort</a>. The full demosort exercises the named sort of a list with op = "numeric", "string", ">>" (lexically gt, descending),">" (numerically gt, descending), a custom comparator, and also a string. </p> <div> <dl><dt>Output:</dt></dl></div><p> Abbreviated sample </p><pre>Sorting Demo using procedure selectionsort on list : [ 3 14 1 5 9 2 6 3 ] with op = &null: [ 1 2 3 3 5 6 9 14 ] (0 ms) ... on string : "qwerty" with op = &null: "eqrtwy" (0 ms)</pre> <h2><span class="mw-headline" id="Io"><a href="/wiki/Category:Io" title="Category:Io">Io</a> </span></h2> <div class="mw-highlight mw-highlight-lang-io mw-content-ltr" dir="ltr"><pre><span></span><span class="nb">List</span><span class="w"> </span><span class="k">do</span><span class="w"> </span><span class="o">(</span> <span class="w"> </span><span class="n">selectionSortInPlace</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="k">method</span><span class="o">(</span> <span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="n">repeat</span><span class="o">(</span><span class="n">idx</span><span class="o">,</span> <span class="w"> </span><span class="n">swapIndices</span><span class="o">(</span><span class="n">idx</span><span class="o">,</span><span class="w"> </span><span class="n">indexOf</span><span class="o">(</span><span class="n">slice</span><span class="o">(</span><span class="n">idx</span><span class="o">,</span><span class="w"> </span><span class="n">size</span><span class="o">)</span><span class="w"> </span><span class="n">min</span><span class="o">))</span> <span class="w"> </span><span class="o">)</span> <span class="w"> </span><span class="o">)</span> <span class="o">)</span> <span class="n">l</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nb">list</span><span class="o">(-</span><span class="mf">1</span><span class="o">,</span><span class="w"> </span><span class="mf">4</span><span class="o">,</span><span class="w"> </span><span class="mf">2</span><span class="o">,</span><span class="w"> </span><span class="o">-</span><span class="mf">9</span><span class="o">)</span> <span class="n">l</span><span class="w"> </span><span class="n">selectionSortInPlace</span><span class="w"> </span><span class="n">println</span><span class="w"> </span><span class="c1"># ==> list(-9, -1, 2, 4)</span> </pre></div> <h2><span class="mw-headline" id="J"><a href="/wiki/Category:J" title="Category:J">J</a> </span></h2> <div class="messagebox" style="text-align:center"><small>Generally, this task should be accomplished in J using <code>/:~</code>. Here we take an approach that's more comparable with the other examples on this page.</small></div> <p>Create the following script and load it to a J session. </p> <div class="mw-highlight mw-highlight-lang-j mw-content-ltr" dir="ltr"><pre><span></span><span class="nv">selectionSort</span><span class="o">=:</span><span class="w"> </span><span class="nf">verb</span><span class="w"> </span><span class="nf">define</span> <span class="w"> </span><span class="nv">data</span><span class="o">=.</span><span class="w"> </span><span class="nd">y</span> <span class="w"> </span><span class="nv">for_xyz</span><span class="o">.</span><span class="w"> </span><span class="nd">y</span><span class="w"> </span><span class="nl">do.</span> <span class="w"> </span><span class="nv">temp</span><span class="o">=.</span><span class="w"> </span><span class="nv">xyz_index</span><span class="w"> </span><span class="o">}.</span><span class="w"> </span><span class="nv">data</span> <span class="w"> </span><span class="nv">nvidx</span><span class="o">=.</span><span class="w"> </span><span class="nv">xyz_index</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nv">temp</span><span class="w"> </span><span class="nv">i</span><span class="o">.</span><span class="w"> </span><span class="o"><./</span><span class="w"> </span><span class="nv">temp</span> <span class="w"> </span><span class="nv">data</span><span class="o">=.</span><span class="w"> </span><span class="p">((</span><span class="nv">xyz_index</span><span class="o">,</span><span class="w"> </span><span class="nv">nvidx</span><span class="p">)</span><span class="w"> </span><span class="o">{</span><span class="w"> </span><span class="nv">data</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="nv">nvidx</span><span class="o">,</span><span class="w"> </span><span class="nv">xyz_index</span><span class="p">)</span><span class="w"> </span><span class="o">}</span><span class="w"> </span><span class="nv">data</span> <span class="w"> </span><span class="nl">end.</span> <span class="w"> </span><span class="nv">data</span> <span class="nl">)</span> </pre></div> <p>In an email discussion, Roger_Hui presented the following tacit code: </p> <div class="mw-highlight mw-highlight-lang-j mw-content-ltr" dir="ltr"><pre><span></span><span class="nv">ix</span><span class="o">=:</span><span class="w"> </span><span class="nv">C</span><span class="o">.~</span><span class="w"> </span><span class="o"><@~.@</span><span class="p">(</span><span class="mi">0</span><span class="o">,</span><span class="w"> </span><span class="p">(</span><span class="nv">i</span><span class="o">.</span><span class="w"> </span><span class="o"><./</span><span class="p">))</span><span class="w"> </span> <span class="nv">ss1</span><span class="o">=:</span><span class="w"> </span><span class="p">(</span><span class="o">{.</span><span class="w"> </span><span class="o">,</span><span class="w"> </span><span class="o">$:@}.</span><span class="p">)</span><span class="o">@</span><span class="nv">ix</span><span class="o">^:</span><span class="p">(</span><span class="o">*@#</span><span class="p">)</span> </pre></div> <p>To validate: </p> <div class="mw-highlight mw-highlight-lang-j mw-content-ltr" dir="ltr"><pre><span></span><span class="w"> </span><span class="o">[</span><span class="nv">data</span><span class="o">=.</span><span class="w"> </span><span class="mi">6</span><span class="w"> </span><span class="mi">15</span><span class="w"> </span><span class="mi">19</span><span class="w"> </span><span class="mi">12</span><span class="w"> </span><span class="mi">14</span><span class="w"> </span><span class="mi">19</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="mi">17</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="mi">14</span> <span class="mi">6</span><span class="w"> </span><span class="mi">15</span><span class="w"> </span><span class="mi">19</span><span class="w"> </span><span class="mi">12</span><span class="w"> </span><span class="mi">14</span><span class="w"> </span><span class="mi">19</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="mi">17</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="mi">14</span> <span class="w"> </span><span class="nv">selectionSort</span><span class="w"> </span><span class="nv">data</span> <span class="mi">0</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="mi">6</span><span class="w"> </span><span class="mi">12</span><span class="w"> </span><span class="mi">14</span><span class="w"> </span><span class="mi">14</span><span class="w"> </span><span class="mi">15</span><span class="w"> </span><span class="mi">17</span><span class="w"> </span><span class="mi">19</span><span class="w"> </span><span class="mi">19</span> <span class="w"> </span><span class="nv">ss1</span><span class="w"> </span><span class="nv">data</span> <span class="mi">0</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="mi">6</span><span class="w"> </span><span class="mi">12</span><span class="w"> </span><span class="mi">14</span><span class="w"> </span><span class="mi">14</span><span class="w"> </span><span class="mi">15</span><span class="w"> </span><span class="mi">17</span><span class="w"> </span><span class="mi">19</span><span class="w"> </span><span class="mi">19</span> </pre></div> <h2><span class="mw-headline" id="Java"><a href="/wiki/Category:Java" title="Category:Java">Java</a> </span></h2> <p>This algorithm sorts in place. The call <tt>sort(array)</tt> will rearrange the array and not create a new one. </p> <div class="mw-highlight mw-highlight-lang-java mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">public</span><span class="w"> </span><span class="kd">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">sort</span><span class="p">(</span><span class="kt">int</span><span class="o">[]</span><span class="w"> </span><span class="n">nums</span><span class="p">){</span> <span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">currentPlace</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="n">currentPlace</span><span class="o"><</span><span class="n">nums</span><span class="p">.</span><span class="na">length</span><span class="o">-</span><span class="mi">1</span><span class="p">;</span><span class="n">currentPlace</span><span class="o">++</span><span class="p">){</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">smallest</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Integer</span><span class="p">.</span><span class="na">MAX_VALUE</span><span class="p">;</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">smallestAt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">currentPlace</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">check</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">currentPlace</span><span class="p">;</span><span class="w"> </span><span class="n">check</span><span class="o"><</span><span class="n">nums</span><span class="p">.</span><span class="na">length</span><span class="p">;</span><span class="n">check</span><span class="o">++</span><span class="p">){</span> <span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="n">nums</span><span class="o">[</span><span class="n">check</span><span class="o">]<</span><span class="n">smallest</span><span class="p">){</span> <span class="w"> </span><span class="n">smallestAt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">check</span><span class="p">;</span> <span class="w"> </span><span class="n">smallest</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">check</span><span class="o">]</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">currentPlace</span><span class="o">]</span><span class="p">;</span> <span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">currentPlace</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">smallestAt</span><span class="o">]</span><span class="p">;</span> <span class="w"> </span><span class="n">nums</span><span class="o">[</span><span class="n">smallestAt</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">temp</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="JavaScript"><a href="/wiki/Category:JavaScript" title="Category:JavaScript">JavaScript</a> </span></h2> <p>This algorithm sorts array of numbers. </p> <div class="mw-highlight mw-highlight-lang-javascript mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">function</span><span class="w"> </span><span class="nx">selectionSort</span><span class="p">(</span><span class="nx">nums</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">nums</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mf">0</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">len</span><span class="p">;</span><span class="w"> </span><span class="nx">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">minAt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">i</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="p">(</span><span class="kd">var</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mf">1</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">len</span><span class="p">;</span><span class="w"> </span><span class="nx">j</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="nx">nums</span><span class="p">[</span><span class="nx">j</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">minAt</span><span class="p">])</span> <span class="w"> </span><span class="nx">minAt</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">j</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">if</span><span class="p">(</span><span class="nx">minAt</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="nx">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="nx">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span> <span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">minAt</span><span class="p">];</span> <span class="w"> </span><span class="nx">nums</span><span class="p">[</span><span class="nx">minAt</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">temp</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nx">nums</span><span class="p">;</span> <span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="jq"><a href="/wiki/Category:Jq" title="Category:Jq">jq</a> </span></h2> <p>The following implementation does not impose any restrictions on the types of entities that may appear in the array to be sorted. That is, the array may include any collection of JSON entities. </p><p> The definition also illustrates the use of an inner function (swap), and the use of jq's reduction operator, <tt>reduce</tt>.</p><div class="mw-highlight mw-content-ltr" dir="ltr"><pre># Sort any array def selection_sort: def swap(i;j): if i == j then . else .[i] as $tmp | .[i] = .[j] | .[j] = $tmp end; length as $length | reduce range(0; $length) as $currentPlace # state: $array ( .; . as $array | (reduce range( $currentPlace; $length) as $check # state: [ smallestAt, smallest] except initially [null] ( [$currentPlace+1] ; if length == 1 or $array[$check] < .[1] then [$check, $array[$check] ] else . end )) as $ans | swap( $currentPlace; $ans[0] ) ) ;</pre></div><p>Example:</p><div class="mw-highlight mw-content-ltr" dir="ltr"><pre>[1, 3.3, null, 2, null, [1,{"a":1 }] ] | selection_sort</pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>[ null, null, 1, 2, 3.3, [ 1, { "a": 1 } ] ] </pre> <h2><span class="mw-headline" id="Julia"><a href="/wiki/Category:Julia" title="Category:Julia">Julia</a> </span></h2> <div class="examplemeta workswith"><b>Works with</b>: <a href="/wiki/Julia" class="mw-redirect" title="Julia">Julia</a> version 0.6</div> <div class="mw-highlight mw-highlight-lang-julia mw-content-ltr" dir="ltr"><pre><span></span><span class="k">function</span><span class="w"> </span><span class="n">selectionsort!</span><span class="p">(</span><span class="n">arr</span><span class="o">::</span><span class="kt">Vector</span><span class="p">{</span><span class="o"><:</span><span class="kt">Real</span><span class="p">})</span> <span class="w"> </span><span class="n">len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">length</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">len</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">arr</span><span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="n">len</span><span class="o">-</span><span class="mi">1</span> <span class="w"> </span><span class="n">lmin</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">findmin</span><span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="o">:</span><span class="k">end</span><span class="p">])</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">lmin</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">lmin</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">arr</span> <span class="k">end</span> <span class="n">v</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">rand</span><span class="p">(</span><span class="o">-</span><span class="mi">10</span><span class="o">:</span><span class="mi">10</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span> <span class="n">println</span><span class="p">(</span><span class="s">"# unordered: </span><span class="si">$v</span><span class="se">\n</span><span class="s"> -> ordered: "</span><span class="p">,</span><span class="w"> </span><span class="n">selectionsort!</span><span class="p">(</span><span class="n">v</span><span class="p">))</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre># unordered: [2, -10, 0, -10, -9, -3, -3, 7, 8, -3] -> ordered: [-10, -10, -9, -3, -3, -3, 0, 2, 7, 8]</pre> <h2><span class="mw-headline" id="Kotlin"><a href="/wiki/Category:Kotlin" title="Category:Kotlin">Kotlin</a> </span></h2> <div class="examplemeta translation"><b>Translation of</b>: <a href="#C#">C#</a></div> <div class="mw-highlight mw-highlight-lang-scala mw-content-ltr" dir="ltr"><pre><span></span><span class="n">fun</span><span class="w"> </span><span class="o"><</span><span class="nc">T</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="nc">Comparable</span><span class="o"><</span><span class="nc">T</span><span class="o">>></span><span class="w"> </span><span class="nc">Array</span><span class="o"><</span><span class="nc">T</span><span class="o">></span><span class="p">.</span><span class="n">selection_sort</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="n">in</span><span class="w"> </span><span class="mi">0</span><span class="p">..</span><span class="n">size</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">2</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="n">in</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">..</span><span class="n">size</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="bp">this</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="bp">this</span><span class="p">[</span><span class="n">k</span><span class="p">])</span> <span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">k</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">this</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="w"> </span><span class="bp">this</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">this</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="w"> </span><span class="bp">this</span><span class="p">[</span><span class="n">k</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> <span class="n">fun</span><span class="w"> </span><span class="n">main</span><span class="p">(</span><span class="n">args</span><span class="p">:</span><span class="w"> </span><span class="nc">Array</span><span class="o"><</span><span class="nc">String</span><span class="o">></span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arrayOf</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">)</span> <span class="w"> </span><span class="n">i</span><span class="p">.</span><span class="n">selection_sort</span><span class="p">()</span> <span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="n">i</span><span class="p">.</span><span class="n">joinToString</span><span class="p">())</span> <span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Array</span><span class="p">(</span><span class="n">i</span><span class="p">.</span><span class="n">size</span><span class="p">,</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">-</span><span class="n">i</span><span class="p">[</span><span class="n">it</span><span class="p">].</span><span class="n">toShort</span><span class="p">()</span><span class="w"> </span><span class="p">})</span> <span class="w"> </span><span class="n">s</span><span class="p">.</span><span class="n">selection_sort</span><span class="p">()</span> <span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="n">s</span><span class="p">.</span><span class="n">joinToString</span><span class="p">())</span> <span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arrayOf</span><span class="p">(</span><span class="sc">'z'</span><span class="p">,</span><span class="w"> </span><span class="sc">'h'</span><span class="p">,</span><span class="w"> </span><span class="sc">'d'</span><span class="p">,</span><span class="w"> </span><span class="sc">'c'</span><span class="p">,</span><span class="w"> </span><span class="sc">'a'</span><span class="p">)</span> <span class="w"> </span><span class="n">c</span><span class="p">.</span><span class="n">selection_sort</span><span class="p">()</span> <span class="w"> </span><span class="n">println</span><span class="p">(</span><span class="n">c</span><span class="p">.</span><span class="n">joinToString</span><span class="p">())</span> <span class="p">}</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>-5, -2, 0, 1, 3, 4, 6, 7, 8, 9 -9, -8, -7, -6, -4, -3, -1, 0, 2, 5 a, c, d, h, z</pre> <h2><span class="mw-headline" id="Liberty_BASIC"><a href="/wiki/Category:Liberty_BASIC" title="Category:Liberty BASIC">Liberty BASIC</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre> itemCount = 20 dim A(itemCount) for i = 1 to itemCount A(i) = int(rnd(1) * 100) next i print "Before Sort" gosub [printArray] '--- Selection sort algorithm for i = 1 to itemCount-1 jMin = i for j = i+1 to itemCount if A(j) < A(jMin) then jMin = j next tmp = A(i) A(i) = A(jMin) A(jMin) = tmp next '--- end of (Selection sort algorithm) print "After Sort" gosub [printArray] end [printArray] for i = 1 to itemCount print using("###", A(i)); next i print return</pre></div> <h2><span class="mw-headline" id="LSE"><a href="/wiki/Category:LSE" title="Category:LSE">LSE</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>(* ** Tri par Sélection ** (LSE2000) *) PROCEDURE &Test(TABLEAU DE ENTIER pDonnees[], ENTIER pTaille) LOCAL pTaille ENTIER i, j, minimum, tmp POUR i <- 0 JUSQUA pTaille-1 FAIRE minimum <- i POUR j <- i+1 JUSQUA pTaille FAIRE SI pDonnees[j] < pDonnees[minimum] ALORS minimum <- j FIN SI BOUCLER SI i # min ALORS tmp <- pDonnees[i] pDonnees[i] <- pDonnees[minimum] pDonnees[minimum] <- tmp FIN SI BOUCLER FIN PROCEDURE</pre></div> <h2><span class="mw-headline" id="Lua"><a href="/wiki/Category:Lua" title="Category:Lua">Lua</a> </span></h2> <div class="mw-highlight mw-highlight-lang-lua mw-content-ltr" dir="ltr"><pre><span></span><span class="kr">function</span> <span class="nf">SelectionSort</span><span class="p">(</span> <span class="n">f</span> <span class="p">)</span> <span class="kr">for</span> <span class="n">k</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="o">#</span><span class="n">f</span><span class="o">-</span><span class="mi">1</span> <span class="kr">do</span> <span class="kd">local</span> <span class="n">idx</span> <span class="o">=</span> <span class="n">k</span> <span class="kr">for</span> <span class="n">i</span> <span class="o">=</span> <span class="n">k</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="o">#</span><span class="n">f</span> <span class="kr">do</span> <span class="kr">if</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o"><</span> <span class="n">f</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="kr">then</span> <span class="n">idx</span> <span class="o">=</span> <span class="n">i</span> <span class="kr">end</span> <span class="kr">end</span> <span class="n">f</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">f</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="n">idx</span><span class="p">],</span> <span class="n">f</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="kr">end</span> <span class="kr">end</span> <span class="n">f</span> <span class="o">=</span> <span class="p">{</span> <span class="mi">15</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="o">-</span><span class="mi">8</span> <span class="p">}</span> <span class="n">SelectionSort</span><span class="p">(</span> <span class="n">f</span> <span class="p">)</span> <span class="kr">for</span> <span class="n">i</span> <span class="kr">in</span> <span class="nb">next</span><span class="p">,</span> <span class="n">f</span> <span class="kr">do</span> <span class="nb">print</span><span class="p">(</span> <span class="n">f</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="p">)</span> <span class="kr">end</span> </pre></div> <h2><span class="mw-headline" id="Maple"><a href="/wiki/Category:Maple" title="Category:Maple">Maple</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>arr:= Array([17,3,72,0,36,2,3,8,40,0]): len := numelems(arr): for i to len-1 do j_min := i: for j from i+1 to len do if arr[j] < arr[j_min] then j_min := j: end if: end do: if (not j_min = i) then temp := arr[i]: arr[i] := arr[j_min]: arr[j_min] := temp: end if: end do: arr;</pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>[0,0,2,3,3,8,17,36,40,72]</pre> <h2><span id="Mathematica_.2FWolfram_Language"></span><span class="mw-headline" id="Mathematica_/Wolfram_Language"><a href="/wiki/Category:Mathematica" title="Category:Mathematica">Mathematica</a> /<a href="/wiki/Category:Wolfram_Language" class="mw-redirect" title="Category:Wolfram Language">Wolfram Language</a> </span></h2> <p>Procedural solution with custom min function: </p> <div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span></span><span class="n">SelectSort</span><span class="p">[</span><span class="nv">x_List</span><span class="p">]</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="n">Module</span><span class="p">[{</span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">temp</span><span class="p">,</span><span class="w"> </span><span class="n">xi</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="p">},</span> <span class="w"> </span><span class="n">While</span><span class="p">[</span><span class="n">n</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">Length</span><span class="o">@</span><span class="n">x</span><span class="p">,</span> <span class="w"> </span><span class="n">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">xi</span><span class="p">[[</span><span class="n">n</span><span class="p">]];</span> <span class="w"> </span><span class="n">For</span><span class="p">[</span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">Length</span><span class="o">@</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">,</span> <span class="w"> </span><span class="n">If</span><span class="p">[</span><span class="n">xi</span><span class="p">[[</span><span class="n">j</span><span class="p">]]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">temp</span><span class="p">,</span><span class="w"> </span><span class="n">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">xi</span><span class="p">[[</span><span class="n">j</span><span class="p">]]];</span> <span class="w"> </span><span class="p">];</span> <span class="w"> </span><span class="n">xi</span><span class="p">[[</span><span class="n">n</span><span class="w"> </span><span class="o">;;</span><span class="p">]]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="n">temp</span><span class="p">}</span><span class="err">~</span><span class="n">Join</span><span class="err">~</span> <span class="w"> </span><span class="n">Delete</span><span class="p">[</span><span class="n">xi</span><span class="p">[[</span><span class="n">n</span><span class="w"> </span><span class="o">;;</span><span class="p">]],</span><span class="w"> </span><span class="n">First</span><span class="o">@</span><span class="n">Position</span><span class="p">[</span><span class="n">xi</span><span class="p">[[</span><span class="n">n</span><span class="w"> </span><span class="o">;;</span><span class="p">]],</span><span class="w"> </span><span class="n">temp</span><span class="p">]</span><span class="w"> </span><span class="p">];</span> <span class="w"> </span><span class="n">n</span><span class="o">++</span><span class="p">;</span> <span class="w"> </span><span class="p">];</span> <span class="w"> </span><span class="n">xi</span> <span class="w"> </span><span class="p">]</span> </pre></div> <p>Recursive solution using a pre-existing Min[] function: </p> <div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span></span><span class="n">SelectSort2</span><span class="p">[</span><span class="nv">x_List</span><span class="p">]</span><span class="o">:=</span><span class="w"> </span><span class="n">Flatten</span><span class="p">[{</span><span class="n">Min</span><span class="o">@</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">If</span><span class="p">[</span><span class="n">Length</span><span class="o">@</span><span class="n">x</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">SelectSort2</span><span class="o">@</span><span class="n">Drop</span><span class="p">[</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">First</span><span class="o">@</span><span class="n">Position</span><span class="p">[</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">Min</span><span class="o">@</span><span class="n">x</span><span class="p">]],</span><span class="w"> </span><span class="p">{}]</span><span class="w"> </span><span class="p">}];</span> </pre></div> <p>Validate by testing the ordering of a random number of randomly-sized random lists: </p> <div class="mw-highlight mw-highlight-lang-mathematica mw-content-ltr" dir="ltr"><pre><span></span><span class="p">{</span><span class="n">And</span><span class="w"> </span><span class="o">@@</span><span class="w"> </span><span class="n">Table</span><span class="p">[</span><span class="n">l</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RandomInteger</span><span class="p">[</span><span class="mi">150</span><span class="p">,</span><span class="w"> </span><span class="n">RandomInteger</span><span class="p">[</span><span class="mi">1000</span><span class="p">]];</span> <span class="w"> </span><span class="n">Through</span><span class="p">[</span><span class="n">And</span><span class="p">[</span><span class="n">Length</span><span class="o">@</span><span class="nv">#</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">Length</span><span class="o">@</span><span class="n">SelectSort</span><span class="o">@</span><span class="nv">#</span><span class="w"> </span><span class="o">&</span><span class="p">,</span><span class="w"> </span><span class="n">OrderedQ</span><span class="o">@</span><span class="n">SelectSort</span><span class="o">@</span><span class="nv">#</span><span class="w"> </span><span class="o">&</span><span class="p">]</span><span class="o">@</span><span class="n">l</span><span class="p">],</span> <span class="w"> </span><span class="p">{</span><span class="n">RandomInteger</span><span class="p">[</span><span class="mi">150</span><span class="p">]}],</span> <span class="w"> </span><span class="n">Block</span><span class="p">[{</span><span class="err">$</span><span class="n">RecursionLimit</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Infinity</span><span class="p">},</span> <span class="w"> </span><span class="n">And</span><span class="w"> </span><span class="o">@@</span><span class="w"> </span><span class="n">Table</span><span class="p">[</span><span class="n">l</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RandomInteger</span><span class="p">[</span><span class="mi">150</span><span class="p">,</span><span class="w"> </span><span class="n">RandomInteger</span><span class="p">[</span><span class="mi">1000</span><span class="p">]];</span> <span class="w"> </span><span class="n">Through</span><span class="p">[</span><span class="n">And</span><span class="p">[</span><span class="n">Length</span><span class="o">@</span><span class="nv">#</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">Length</span><span class="o">@</span><span class="n">SelectSort2</span><span class="o">@</span><span class="nv">#</span><span class="w"> </span><span class="o">&</span><span class="p">,</span><span class="w"> </span><span class="n">OrderedQ</span><span class="o">@</span><span class="n">SelectSort2</span><span class="o">@</span><span class="nv">#</span><span class="w"> </span><span class="o">&</span><span class="p">]</span><span class="o">@</span><span class="n">l</span><span class="p">],</span> <span class="w"> </span><span class="p">{</span><span class="n">RandomInteger</span><span class="p">[</span><span class="mi">150</span><span class="p">]}]</span> <span class="w"> </span><span class="p">]}</span> </pre></div> <p>Validation Result: </p> <pre>{True, True}</pre> <h2><span id="MATLAB_.2F_Octave"></span><span class="mw-headline" id="MATLAB_/_Octave"><a href="/wiki/Category:MATLAB" title="Category:MATLAB">MATLAB</a> / <a href="/wiki/Category:Octave" title="Category:Octave">Octave</a> </span></h2> <div class="mw-highlight mw-highlight-lang-matlab mw-content-ltr" dir="ltr"><pre><span></span><span class="k">function</span><span class="w"> </span>list<span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nf">selectionSort</span><span class="p">(</span>list<span class="p">)</span> <span class="w"> </span><span class="n">listSize</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">numel</span><span class="p">(</span><span class="n">list</span><span class="p">);</span> <span class="w"> </span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nb">i</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">:</span><span class="n">listSize</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="w"> </span><span class="n">minElem</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">list</span><span class="p">(</span><span class="nb">i</span><span class="p">);</span> <span class="w"> </span><span class="n">minIndex</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">i</span><span class="p">;</span> <span class="w"> </span> <span class="w"> </span><span class="c">%This for loop can be vectorized, but there will be no significant</span> <span class="w"> </span><span class="c">%increase in sorting efficiency.</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nb">j</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="nb">i</span><span class="p">:</span><span class="n">listSize</span><span class="p">)</span><span class="w"> </span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">list</span><span class="p">(</span><span class="nb">j</span><span class="p">)</span><span class="w"> </span><span class="o"><=</span><span class="w"> </span><span class="n">minElem</span> <span class="w"> </span><span class="n">minElem</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">list</span><span class="p">(</span><span class="nb">j</span><span class="p">);</span> <span class="w"> </span><span class="n">minIndex</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">j</span><span class="p">;</span> <span class="w"> </span><span class="k">end</span><span class="w"> </span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nb">i</span><span class="w"> </span><span class="o">~=</span><span class="w"> </span><span class="n">minIndex</span> <span class="w"> </span><span class="n">list</span><span class="p">([</span><span class="n">minIndex</span><span class="w"> </span><span class="nb">i</span><span class="p">])</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">list</span><span class="p">([</span><span class="nb">i</span><span class="w"> </span><span class="n">minIndex</span><span class="p">]);</span><span class="w"> </span><span class="c">%Swap</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span> <span class="w"> </span><span class="k">end</span><span class="w"> </span><span class="c">%for</span> <span class="k">end</span><span class="w"> </span><span class="c">%selectionSort</span> </pre></div> <p>Sample Usage: </p> <div class="mw-highlight mw-highlight-lang-matlab mw-content-ltr" dir="ltr"><pre><span></span><span class="o">>></span><span class="w"> </span><span class="n">selectionSort</span><span class="p">([</span><span class="mi">4</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="mi">6</span><span class="w"> </span><span class="mi">2</span><span class="p">])</span> <span class="nb">ans</span><span class="w"> </span><span class="p">=</span> <span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="mi">6</span> </pre></div> <h2><span class="mw-headline" id="Maxima"><a href="/wiki/Category:Maxima" title="Category:Maxima">Maxima</a> </span></h2> <div class="mw-highlight mw-highlight-lang-maxima mw-content-ltr" dir="ltr"><pre><span></span><span class="nf">selection_sort</span><span class="p">(</span><span class="nv">v</span><span class="p">)</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nf">block</span><span class="p">([</span><span class="nv">k</span><span class="p">,</span><span class="w"> </span><span class="nv">m</span><span class="p">,</span><span class="w"> </span><span class="nv">n</span><span class="p">],</span> <span class="nv">n</span><span class="o">:</span><span class="w"> </span><span class="nf">length</span><span class="p">(</span><span class="nv">v</span><span class="p">),</span> <span class="k">for</span><span class="w"> </span><span class="nv">i</span><span class="o">:</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">thru</span><span class="w"> </span><span class="nv">n</span><span class="w"> </span><span class="k">do</span><span class="w"> </span><span class="p">(</span> <span class="w"> </span><span class="nv">k</span><span class="o">:</span><span class="w"> </span><span class="nv">i</span><span class="p">,</span> <span class="w"> </span><span class="nv">m</span><span class="o">:</span><span class="w"> </span><span class="nv">v</span><span class="p">[</span><span class="nv">i</span><span class="p">],</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nv">j</span><span class="o">:</span><span class="w"> </span><span class="nv">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="k">thru</span><span class="w"> </span><span class="nv">n</span><span class="w"> </span><span class="k">do</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nv">v</span><span class="p">[</span><span class="nv">j</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nv">m</span><span class="w"> </span><span class="k">then</span><span class="w"> </span><span class="p">(</span><span class="nv">k</span><span class="o">:</span><span class="w"> </span><span class="nv">j</span><span class="p">,</span><span class="w"> </span><span class="nv">m</span><span class="o">:</span><span class="w"> </span><span class="nv">v</span><span class="p">[</span><span class="nv">j</span><span class="p">]),</span> <span class="w"> </span><span class="nv">v</span><span class="p">[</span><span class="nv">k</span><span class="p">]</span><span class="o">:</span><span class="w"> </span><span class="nv">v</span><span class="p">[</span><span class="nv">i</span><span class="p">],</span> <span class="w"> </span><span class="nv">v</span><span class="p">[</span><span class="nv">i</span><span class="p">]</span><span class="o">:</span><span class="w"> </span><span class="nv">m</span> <span class="p">))$</span> <span class="nv">v</span><span class="o">:</span><span class="w"> </span><span class="nf">makelist</span><span class="p">(</span><span class="nf">random</span><span class="p">(</span><span class="mi">199</span><span class="p">)</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">99</span><span class="p">,</span><span class="w"> </span><span class="nv">i</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">10</span><span class="p">);</span><span class="w"> </span><span class="cm">/* [52, -85, 41, -70, -59, 88, 19, 80, 90, 44] */</span> <span class="nf">selection_sort</span><span class="p">(</span><span class="nv">v</span><span class="p">)$</span> <span class="nv">v</span><span class="p">;</span><span class="w"> </span><span class="cm">/* [-85, -70, -59, 19, 41, 44, 52, 80, 88, 90] */</span> </pre></div> <h2><span class="mw-headline" id="MAXScript"><a href="/wiki/Category:MAXScript" title="Category:MAXScript">MAXScript</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>fn selectionSort arr = ( local min = undefined for i in 1 to arr.count do ( min = i for j in i+1 to arr.count do ( if arr[j] < arr[min] then ( min = j ) ) swap arr[i] arr[min] ) arr ) data = selectionSort #(4, 9, 3, -2, 0, 7, -5, 1, 6, 8) print data</pre></div> <h2><span id="N.2Ft.2Froff"></span><span class="mw-headline" id="N/t/roff"><a href="/wiki/Category:N/t/roff" title="Category:N/t/roff">N/t/roff</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>.de end .. .de array . nr \\$1.c 0 1 . de \\$1.push end . nr \\$1..\\\\n+[\\$1.c] \\\\$1 . end . de \\$1.pushln end . if \\\\n(.$>0 .\\$1.push \\\\$1 . if \\\\n(.$>1 \{ \ . shift . \\$1.pushln \\\\$@ . \} . end . de \\$1.dump end . nr i 0 1 . while \\\\n+i<=\\\\n[\\$1.c] .tm \\\\n[\\$1..\\\\ni] . rr i . end . de \\$1.swap end . if (\\\\$1<=\\\\n[\\$1.c])&(\\\\$2<=\\\\n[\\$1.c]) \{ \ . nr b \\\\n[\\$1..\\\\$1] . nr \\$1..\\\\$1 \\\\n[\\$1..\\\\$2] . nr \\$1..\\\\$2 \\\\nb . rr b . \} . end .. .array myArray .myArray.pushln 14 62 483 21 12 11 0 589 212 10 5 4 95 4 2 2 12 0 0 .de sort . nr i 0 1 . while \\n+i<=\\n[\\$1.c] \{ \ . nr j \\ni 1 . nr st \\nj . while \\n+j<=\\n[\\$1.c] \{ \ . if \\n[\\$1..\\nj]<\\n[\\$1..\\n(st] .nr st \\nj . \} . if !\\n(st=\\ni .\\$1.swap \\ni \\n(st . \} .. .sort myArray .myArray.dump</pre></div> <h3><span class="mw-headline" id="Output">Output</span></h3> <div class="mw-highlight mw-highlight-lang-text mw-content-ltr" dir="ltr"><pre><span></span>0 0 0 2 2 4 4 5 10 11 12 12 14 21 62 95 212 483 589 </pre></div> <h2><span class="mw-headline" id="Nanoquery"><a href="/wiki/Category:Nanoquery" title="Category:Nanoquery">Nanoquery</a> </span></h2> <div class="examplemeta translation"><b>Translation of</b>: <a href="#Java">Java</a></div> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>import math def sort(nums) global math for currentPlace in range(0, len(nums) - 2) smallest = math.maxint smallestAt = currentPlace + 1 for check in range(currentPlace, len(nums) - 1) if nums[check] < smallest smallestAt = check smallest = nums[check] end end temp = nums[currentPlace] nums[currentPlace] = nums[smallestAt] nums[smallestAt] = temp end return nums end</pre></div> <h2><span class="mw-headline" id="Nemerle"><a href="/wiki/Category:Nemerle" title="Category:Nemerle">Nemerle</a> </span></h2> <div class="examplemeta translation"><b>Translation of</b>: <a href="#C#">C#</a></div> <div class="mw-highlight mw-highlight-lang-nemerle mw-content-ltr" dir="ltr"><pre><span></span><span class="k">using</span><span class="w"> </span><span class="n">System</span><span class="p">;</span> <span class="k">using</span><span class="w"> </span><span class="n">System</span><span class="p">.</span><span class="n">Console</span><span class="p">;</span> <span class="k">module</span><span class="w"> </span><span class="n">Selection</span> <span class="p">{</span> <span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">Sort</span><span class="p">[</span><span class="n">T</span><span class="p">](</span><span class="k">this</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="kt">array</span><span class="p">[</span><span class="n">T</span><span class="p">])</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="kt">void</span> <span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">IComparable</span> <span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">mutable</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="m">0</span><span class="p">;</span> <span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="n">lastindex</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">Length</span><span class="w"> </span><span class="p">-</span><span class="w"> </span><span class="m">1</span><span class="p">;</span> <span class="w"> </span> <span class="w"> </span><span class="k">foreach</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="n">in</span><span class="w"> </span><span class="p">[</span><span class="m">0</span><span class="w"> </span><span class="p">..</span><span class="w"> </span><span class="n">lastindex</span><span class="p">])</span> <span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span> <span class="w"> </span><span class="k">foreach</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="n">in</span><span class="w"> </span><span class="p">[</span><span class="n">i</span><span class="w"> </span><span class="p">..</span><span class="w"> </span><span class="n">lastindex</span><span class="p">])</span> <span class="w"> </span><span class="k">when</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="p">].</span><span class="n">CompareTo</span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">k</span><span class="p">])</span><span class="w"> </span><span class="p"><</span><span class="w"> </span><span class="m">0</span><span class="p">)</span><span class="w"> </span><span class="n">k</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">j</span><span class="p">;</span> <span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="p"><-></span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">k</span><span class="p">];</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span> <span class="w"> </span><span class="n">Main</span><span class="p">()</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="kt">void</span> <span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="n">arr</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="kt">array</span><span class="p">[</span><span class="m">6</span><span class="p">,</span><span class="w"> </span><span class="m">2</span><span class="p">,</span><span class="w"> </span><span class="m">8</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">4</span><span class="p">,</span><span class="w"> </span><span class="m">7</span><span class="p">,</span><span class="w"> </span><span class="m">3</span><span class="p">,</span><span class="w"> </span><span class="m">9</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="p">];</span> <span class="w"> </span><span class="n">arr</span><span class="p">.</span><span class="n">Sort</span><span class="p">();</span> <span class="w"> </span><span class="k">foreach</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="n">in</span><span class="w"> </span><span class="n">arr</span><span class="p">)</span><span class="w"> </span><span class="n">Write</span><span class="p">(</span><span class="s">$"</span><span class="n">$i</span><span class="s"> "</span><span class="p">);</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="NetRexx"><a href="/wiki/Category:NetRexx" title="Category:NetRexx">NetRexx</a> </span></h2> <div class="mw-highlight mw-highlight-lang-rexx mw-content-ltr" dir="ltr"><pre><span></span><span class="cm">/* NetRexx */</span> <span class="kr">options</span><span class="w"> </span>replace<span class="w"> </span>format<span class="w"> </span>comments<span class="w"> </span>java<span class="w"> </span>crossref<span class="w"> </span>savelog<span class="w"> </span>symbols<span class="w"> </span>binary import<span class="w"> </span>java<span class="o">.</span>util<span class="o">.</span>List placesList<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="err">[</span>String<span class="w"> </span><span class="o">-</span> <span class="w"> </span><span class="s">"UK London"</span><span class="o">,</span><span class="w"> </span><span class="s">"US New York"</span><span class="o">,</span><span class="w"> </span><span class="s">"US Boston"</span><span class="o">,</span><span class="w"> </span><span class="s">"US Washington"</span><span class="w"> </span><span class="o">-</span> <span class="w"> </span><span class="o">,</span><span class="w"> </span><span class="s">"UK Washington"</span><span class="o">,</span><span class="w"> </span><span class="s">"US Birmingham"</span><span class="o">,</span><span class="w"> </span><span class="s">"UK Birmingham"</span><span class="o">,</span><span class="w"> </span><span class="s">"UK Boston"</span><span class="w"> </span><span class="o">-</span> <span class="err">]</span> lists<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="err">[</span><span class="w"> </span><span class="o">-</span> <span class="w"> </span>placesList<span class="w"> </span><span class="o">-</span> <span class="w"> </span><span class="o">,</span><span class="w"> </span>selectionSort<span class="o">(</span>String<span class="err">[]</span><span class="w"> </span>Arrays<span class="o">.</span>copyOf<span class="o">(</span>placesList<span class="o">,</span><span class="w"> </span>placesList<span class="o">.</span>length<span class="o">))</span><span class="w"> </span><span class="o">-</span> <span class="err">]</span> loop<span class="w"> </span>ln<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="w"> </span><span class="kr">to</span><span class="w"> </span>lists<span class="o">.</span>length<span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="m">1</span> <span class="w"> </span>cl<span class="w"> </span><span class="o">=</span><span class="w"> </span>lists<span class="err">[</span>ln<span class="err">]</span> <span class="w"> </span>loop<span class="w"> </span>ct<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="w"> </span><span class="kr">to</span><span class="w"> </span>cl<span class="o">.</span>length<span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="m">1</span> <span class="w"> </span><span class="kr">say</span><span class="w"> </span>cl<span class="err">[</span>ct<span class="err">]</span> <span class="w"> </span><span class="kr">end</span><span class="w"> </span>ct <span class="w"> </span><span class="kr">say</span> <span class="w"> </span><span class="kr">end</span><span class="w"> </span>ln <span class="kr">return</span> method<span class="w"> </span>selectionSort<span class="o">(</span>a<span class="w"> </span><span class="o">=</span><span class="w"> </span>String<span class="err">[]</span><span class="o">)</span><span class="w"> </span>public<span class="w"> </span>constant<span class="w"> </span>binary<span class="w"> </span>returns<span class="w"> </span>String<span class="err">[]</span> <span class="w"> </span>rl<span class="w"> </span><span class="o">=</span><span class="w"> </span>String<span class="err">[</span>a<span class="o">.</span>length<span class="err">]</span> <span class="w"> </span>al<span class="w"> </span><span class="o">=</span><span class="w"> </span>List<span class="w"> </span>selectionSort<span class="o">(</span>Arrays<span class="o">.</span>asList<span class="o">(</span>a<span class="o">))</span> <span class="w"> </span>al<span class="o">.</span>toArray<span class="o">(</span>rl<span class="o">)</span> <span class="w"> </span><span class="kr">return</span><span class="w"> </span>rl method<span class="w"> </span>selectionSort<span class="o">(</span>a<span class="w"> </span><span class="o">=</span><span class="w"> </span>List<span class="o">)</span><span class="w"> </span>public<span class="w"> </span>constant<span class="w"> </span>binary<span class="w"> </span>returns<span class="w"> </span>ArrayList <span class="w"> </span>ra<span class="w"> </span><span class="o">=</span><span class="w"> </span>ArrayList<span class="o">(</span>a<span class="o">)</span> <span class="w"> </span>n<span class="w"> </span><span class="o">=</span><span class="w"> </span>ra<span class="o">.</span>size <span class="w"> </span>iPos<span class="w"> </span><span class="o">=</span><span class="w"> </span>int <span class="w"> </span>iMin<span class="w"> </span><span class="o">=</span><span class="w"> </span>int <span class="w"> </span>loop<span class="w"> </span>iPos<span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0</span><span class="w"> </span><span class="kr">to</span><span class="w"> </span>n<span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="m">1</span> <span class="w"> </span>iMin<span class="w"> </span><span class="o">=</span><span class="w"> </span>iPos <span class="w"> </span>loop<span class="w"> </span>i_<span class="w"> </span><span class="o">=</span><span class="w"> </span>iPos<span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="m">1</span><span class="w"> </span><span class="kr">to</span><span class="w"> </span>n<span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="m">1</span> <span class="w"> </span><span class="kr">if</span><span class="w"> </span><span class="o">(</span>Comparable<span class="w"> </span>ra<span class="o">.</span>get<span class="o">(</span>i_<span class="o">)).</span>compareTo<span class="o">(</span>Comparable<span class="w"> </span>ra<span class="o">.</span>get<span class="o">(</span>iMin<span class="o">))</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="m">0</span><span class="w"> </span><span class="kr">then</span><span class="w"> </span><span class="kr">do</span> <span class="w"> </span>iMin<span class="w"> </span><span class="o">=</span><span class="w"> </span>i_ <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="kr">end</span><span class="w"> </span>i_ <span class="w"> </span><span class="kr">if</span><span class="w"> </span>iMin<span class="w"> </span><span class="o">\=</span><span class="w"> </span>iPos<span class="w"> </span><span class="kr">then</span><span class="w"> </span><span class="kr">do</span> <span class="w"> </span>swap<span class="w"> </span><span class="o">=</span><span class="w"> </span>ra<span class="o">.</span>get<span class="o">(</span>iPos<span class="o">)</span> <span class="w"> </span>ra<span class="o">.</span>set<span class="o">(</span>iPos<span class="o">,</span><span class="w"> </span>ra<span class="o">.</span>get<span class="o">(</span>iMin<span class="o">))</span> <span class="w"> </span>ra<span class="o">.</span>set<span class="o">(</span>iMin<span class="o">,</span><span class="w"> </span>swap<span class="o">)</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="kr">end</span><span class="w"> </span>iPos <span class="w"> </span><span class="kr">return</span><span class="w"> </span>ra </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>UK London US New York US Boston US Washington UK Washington US Birmingham UK Birmingham UK Boston UK Birmingham UK Boston UK London UK Washington US Birmingham US Boston US New York US Washington </pre> <h2><span class="mw-headline" id="Nim"><a href="/wiki/Category:Nim" title="Category:Nim">Nim</a> </span></h2> <div class="mw-highlight mw-highlight-lang-nim mw-content-ltr" dir="ltr"><pre><span></span><span class="k">proc</span><span class="w"> </span><span class="nf">selectionSort</span><span class="o">[</span><span class="n">T</span><span class="o">]</span><span class="p">(</span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">openarray</span><span class="o">[</span><span class="n">T</span><span class="o">]</span><span class="p">)</span><span class="w"> </span><span class="o">=</span> <span class="w"> </span><span class="k">let</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">len</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">..</span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">:</span> <span class="w"> </span><span class="kd">var</span><span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="p">..</span><span class="o"><</span><span class="w"> </span><span class="n">n</span><span class="p">:</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">a</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">a</span><span class="o">[</span><span class="n">m</span><span class="o">]</span><span class="p">:</span> <span class="w"> </span><span class="n">m</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span> <span class="w"> </span><span class="n">swap</span><span class="w"> </span><span class="n">a</span><span class="o">[</span><span class="n">i</span><span class="o">]</span><span class="p">,</span><span class="w"> </span><span class="n">a</span><span class="o">[</span><span class="n">m</span><span class="o">]</span> <span class="kd">var</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">@[</span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">65</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">31</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">99</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">83</span><span class="p">,</span><span class="w"> </span><span class="mi">782</span><span class="o">]</span> <span class="n">selectionSort</span><span class="w"> </span><span class="n">a</span> <span class="n">echo</span><span class="w"> </span><span class="n">a</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre> <h2><span class="mw-headline" id="OCaml"><a href="/wiki/Category:OCaml" title="Category:OCaml">OCaml</a> </span></h2> <div class="mw-highlight mw-highlight-lang-ocaml mw-content-ltr" dir="ltr"><pre><span></span><span class="k">let</span> <span class="k">rec</span> <span class="n">selection_sort</span> <span class="o">=</span> <span class="k">function</span> <span class="bp">[]</span> <span class="o">-></span> <span class="bp">[]</span> <span class="o">|</span> <span class="n">first</span><span class="o">::</span><span class="n">lst</span> <span class="o">-></span> <span class="k">let</span> <span class="k">rec</span> <span class="n">select_r</span> <span class="n">small</span> <span class="n">output</span> <span class="o">=</span> <span class="k">function</span> <span class="bp">[]</span> <span class="o">-></span> <span class="n">small</span> <span class="o">::</span> <span class="n">selection_sort</span> <span class="n">output</span> <span class="o">|</span> <span class="n">x</span><span class="o">::</span><span class="n">xs</span> <span class="k">when</span> <span class="n">x</span> <span class="o"><</span> <span class="n">small</span> <span class="o">-></span> <span class="n">select_r</span> <span class="n">x</span> <span class="o">(</span><span class="n">small</span><span class="o">::</span><span class="n">output</span><span class="o">)</span> <span class="n">xs</span> <span class="o">|</span> <span class="n">x</span><span class="o">::</span><span class="n">xs</span> <span class="o">-></span> <span class="n">select_r</span> <span class="n">small</span> <span class="o">(</span><span class="n">x</span><span class="o">::</span><span class="n">output</span><span class="o">)</span> <span class="n">xs</span> <span class="k">in</span> <span class="n">select_r</span> <span class="n">first</span> <span class="bp">[]</span> <span class="n">lst</span> </pre></div> <h2><span class="mw-headline" id="Oforth"><a href="/wiki/Category:Oforth" title="Category:Oforth">Oforth</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>: selectSort(l) | b j i k s | l size ->s l asListBuffer ->b s loop: i [ i dup ->k b at i 1 + s for: j [ b at(j) 2dup <= ifTrue: [ drop ] else: [ nip j ->k ] ] k i b at b put i swap b put ] b dup freeze ;</pre></div> <h2><span class="mw-headline" id="ooRexx"><a href="/wiki/Category:OoRexx" title="Category:OoRexx">ooRexx</a> </span></h2> <div class="mw-highlight mw-highlight-lang-rexx mw-content-ltr" dir="ltr"><pre><span></span><span class="cm">/*REXX ****************************************************************</span> <span class="cm">* program sorts an array using the selection-sort method.</span> <span class="cm">* derived from REXX solution</span> <span class="cm">* Note that ooRexx can process Elements of the stem argument (Use Arg)</span> <span class="cm">* 06.10.2010 Walter Pachl</span> <span class="cm">**********************************************************************/</span> <span class="kr">call</span><span class="w"> </span>generate<span class="w"> </span><span class="cm">/*generate the array elements. */</span> <span class="kr">call</span><span class="w"> </span>show<span class="w"> </span><span class="s">'before sort'</span><span class="w"> </span><span class="cm">/*show the before array elements,*/</span> <span class="kr">call</span><span class="w"> </span>selectionSort<span class="w"> </span>x<span class="o">.</span><span class="w"> </span><span class="cm">/*invoke the selection sort. */</span> <span class="kr">call</span><span class="w"> </span>show<span class="w"> </span><span class="s">'after sort'</span><span class="w"> </span><span class="cm">/*show the after array elements.*/</span> <span class="kr">exit</span><span class="w"> </span><span class="cm">/*stick a fork in it, we're done.*/</span> <span class="nf">selectionSort</span><span class="o">:</span><span class="w"> </span><span class="kd">Procedure</span> <span class="w"> </span>Use<span class="w"> </span><span class="kr">Arg</span><span class="w"> </span>s<span class="o">.</span><span class="w"> </span><span class="cm">/* gain access to the argument */</span> <span class="w"> </span><span class="kr">do</span><span class="w"> </span>j<span class="o">=</span><span class="m">1</span><span class="w"> </span><span class="kr">To</span><span class="w"> </span>s<span class="o">.</span><span class="m">0</span><span class="o">-</span><span class="m">1</span> <span class="w"> </span>t<span class="o">=</span>s<span class="o">.</span>j<span class="err">;</span> <span class="w"> </span>p<span class="o">=</span>j<span class="err">;</span> <span class="w"> </span><span class="kr">do</span><span class="w"> </span>k<span class="o">=</span>j<span class="o">+</span><span class="m">1</span><span class="w"> </span><span class="kr">to</span><span class="w"> </span>s<span class="o">.</span><span class="m">0</span> <span class="w"> </span><span class="kr">if</span><span class="w"> </span>s<span class="o">.</span>k<span class="o"><</span>t<span class="w"> </span><span class="kr">then</span><span class="w"> </span><span class="kr">do</span><span class="err">;</span> <span class="w"> </span>t<span class="o">=</span>s<span class="o">.</span>k<span class="err">;</span> <span class="w"> </span>p<span class="o">=</span>k<span class="err">;</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="kr">if</span><span class="w"> </span>p<span class="o">=</span>j<span class="w"> </span><span class="kr">then</span> <span class="w"> </span><span class="kr">iterate</span> <span class="w"> </span>t<span class="o">=</span>s<span class="o">.</span>j<span class="err">;</span> <span class="w"> </span>s<span class="o">.</span>j<span class="o">=</span>s<span class="o">.</span>p<span class="err">;</span> <span class="w"> </span>s<span class="o">.</span>p<span class="o">=</span>t <span class="w"> </span><span class="kr">end</span> <span class="w"> </span><span class="kr">return</span> <span class="nl">show</span><span class="o">:</span> <span class="w"> </span><span class="kr">Parse</span><span class="w"> </span><span class="kr">Arg</span><span class="w"> </span>heading <span class="w"> </span><span class="kr">Say</span><span class="w"> </span>heading <span class="w"> </span><span class="kr">Do</span><span class="w"> </span>i<span class="o">=</span><span class="m">1</span><span class="w"> </span><span class="kr">To</span><span class="w"> </span>x<span class="o">.</span><span class="m">0</span> <span class="w"> </span><span class="kr">Say</span><span class="w"> </span>i<span class="s">' '</span>x<span class="o">.</span>i <span class="w"> </span><span class="kr">End</span> <span class="w"> </span><span class="kr">say</span><span class="w"> </span><span class="nb">copies</span><span class="o">(</span><span class="s">'-'</span><span class="o">,</span><span class="m">79</span><span class="o">)</span> <span class="w"> </span><span class="kr">Return</span> <span class="kr">return</span> <span class="nl">generate</span><span class="o">:</span> <span class="w"> </span>x<span class="o">.</span><span class="m">1</span><span class="o">=</span><span class="s">'---The seven hills of Rome:---'</span> <span class="w"> </span>x<span class="o">.</span><span class="m">2</span><span class="o">=</span><span class="s">'=============================='</span> <span class="w"> </span>x<span class="o">.</span><span class="m">3</span><span class="o">=</span><span class="s">'Caelian'</span> <span class="w"> </span>x<span class="o">.</span><span class="m">4</span><span class="o">=</span><span class="s">'Palatine'</span> <span class="w"> </span>x<span class="o">.</span><span class="m">5</span><span class="o">=</span><span class="s">'Capitoline'</span> <span class="w"> </span>x<span class="o">.</span><span class="m">6</span><span class="o">=</span><span class="s">'Virminal'</span> <span class="w"> </span>x<span class="o">.</span><span class="m">7</span><span class="o">=</span><span class="s">'Esquiline'</span> <span class="w"> </span>x<span class="o">.</span><span class="m">8</span><span class="o">=</span><span class="s">'Quirinal'</span> <span class="w"> </span>x<span class="o">.</span><span class="m">9</span><span class="o">=</span><span class="s">'Aventine'</span> <span class="w"> </span>x<span class="o">.</span><span class="m">0</span><span class="o">=</span><span class="m">9</span> <span class="w"> </span><span class="kr">return</span> </pre></div> <h2><span class="mw-headline" id="Oz"><a href="/wiki/Category:Oz" title="Category:Oz">Oz</a> </span></h2> <p>Although lists are much more used in Oz than arrays, this algorithm seems more natural for arrays. </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>declare proc {SelectionSort Arr} proc {Swap K L} Arr.K := (Arr.L := Arr.K) end Low = {Array.low Arr} High = {Array.high Arr} in  %% for every index I of the array for I in Low..High do %% find the index of the minimum element %% with an index >= I Min = {NewCell Arr.I} MinIndex = {NewCell I} in for J in I..High do if Arr.J < @Min then Min := Arr.J MinIndex := J end end %% and put that minimum element to the left {Swap @MinIndex I} end end A = {Tuple.toArray unit(3 1 4 1 5 9 2 6 5)} in {SelectionSort A} {Show {Array.toRecord unit A}}</pre></div> <h2><span id="PARI.2FGP"></span><span class="mw-headline" id="PARI/GP"><a href="/wiki/Category:PARI/GP" title="Category:PARI/GP">PARI/GP</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>selectionSort(v)={ for(i=1,#v-1, my(mn=i,t); for(j=i+1,#v, if(v[j]<v[mn],mn=j) ); t=v[mn]; v[mn]=v[i]; v[i]=t ); v };</pre></div> <h2><span class="mw-headline" id="Pascal"><a href="/wiki/Category:Pascal" title="Category:Pascal">Pascal</a> </span></h2> <p>See <a class="mw-selflink-fragment" href="#Delphi"> Delphi</a> </p> <h2><span class="mw-headline" id="Perl"><a href="/wiki/Category:Perl" title="Category:Perl">Perl</a> </span></h2> <div class="examplemeta translation"><b>Translation of</b>: <a href="#Tcl">Tcl</a></div> <div class="mw-highlight mw-highlight-lang-perl mw-content-ltr" dir="ltr"><pre><span></span><span class="k">sub</span><span class="w"> </span><span class="nf">selection_sort</span> <span class="w"> </span><span class="p">{</span><span class="k">my</span><span class="w"> </span><span class="nv">@a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">@_</span><span class="p">;</span> <span class="w"> </span><span class="k">foreach</span><span class="w"> </span><span class="k">my</span><span class="w"> </span><span class="nv">$i</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="w"> </span><span class="o">..</span><span class="w"> </span><span class="nv">$#a</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span> <span class="w"> </span><span class="p">{</span><span class="k">my</span><span class="w"> </span><span class="nv">$min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">$i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span> <span class="w"> </span><span class="nv">$a</span><span class="p">[</span><span class="nv">$_</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nv">$a</span><span class="p">[</span><span class="nv">$min</span><span class="p">]</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nv">$min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">$_</span><span class="w"> </span><span class="k">foreach</span><span class="w"> </span><span class="nv">$min</span><span class="w"> </span><span class="o">..</span><span class="w"> </span><span class="nv">$#a</span><span class="p">;</span> <span class="w"> </span><span class="nv">$a</span><span class="p">[</span><span class="nv">$i</span><span class="p">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="nv">$a</span><span class="p">[</span><span class="nv">$min</span><span class="p">]</span><span class="w"> </span><span class="ow">and</span><span class="w"> </span><span class="nv">@a</span><span class="p">[</span><span class="nv">$i</span><span class="p">,</span><span class="w"> </span><span class="nv">$min</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nv">@a</span><span class="p">[</span><span class="nv">$min</span><span class="p">,</span><span class="w"> </span><span class="nv">$i</span><span class="p">];}</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nv">@a</span><span class="p">;}</span> </pre></div> <h2><span class="mw-headline" id="Phix"><a href="/wiki/Category:Phix" title="Category:Phix">Phix</a> </span></h2> <pre><span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #008080;">function</span> <span style="color: #000000;">selection_sort</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span> <span style="color: #004080;">object</span> <span style="color: #000000;">si</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">sm</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #004080;">object</span> <span style="color: #000000;">sj</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">if</span> <span style="color: #000000;">sj</span><span style="color: #0000FF;"><</span><span style="color: #000000;">sm</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">sm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">sj</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span> <span style="color: #008080;">if</span> <span style="color: #000000;">sm</span><span style="color: #0000FF;"><</span><span style="color: #000000;">si</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (or equivalently m!=i)</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sm</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">si</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span> <span style="color: #008080;">return</span> <span style="color: #000000;">s</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span> <span style="color: #0000FF;">?</span><span style="color: #000000;">selection_sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)))</span> </pre> <div> <dl><dt>Output:</dt></dl></div> <pre>{1,2,3,4,5,6,7,8,9,10} </pre> <h2><span class="mw-headline" id="PHP"><a href="/wiki/Category:PHP" title="Category:PHP">PHP</a> </span></h2> <p>Iterative: </p> <div class="mw-highlight mw-highlight-lang-php mw-content-ltr" dir="ltr"><pre><span></span><span class="k">function</span> <span class="nf">selection_sort</span><span class="p">(</span><span class="o">&</span><span class="nv">$arr</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$n</span> <span class="o">=</span> <span class="nb">count</span><span class="p">(</span><span class="nv">$arr</span><span class="p">);</span> <span class="k">for</span><span class="p">(</span><span class="nv">$i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="nv">$i</span> <span class="o"><</span> <span class="nb">count</span><span class="p">(</span><span class="nv">$arr</span><span class="p">);</span> <span class="nv">$i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$min</span> <span class="o">=</span> <span class="nv">$i</span><span class="p">;</span> <span class="k">for</span><span class="p">(</span><span class="nv">$j</span> <span class="o">=</span> <span class="nv">$i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">;</span> <span class="nv">$j</span> <span class="o"><</span> <span class="nv">$n</span><span class="p">;</span> <span class="nv">$j</span><span class="o">++</span><span class="p">){</span> <span class="k">if</span><span class="p">(</span><span class="nv">$arr</span><span class="p">[</span><span class="nv">$j</span><span class="p">]</span> <span class="o"><</span> <span class="nv">$arr</span><span class="p">[</span><span class="nv">$min</span><span class="p">]){</span> <span class="nv">$min</span> <span class="o">=</span> <span class="nv">$j</span><span class="p">;</span> <span class="p">}</span> <span class="p">}</span> <span class="k">list</span><span class="p">(</span><span class="nv">$arr</span><span class="p">[</span><span class="nv">$i</span><span class="p">],</span><span class="nv">$arr</span><span class="p">[</span><span class="nv">$min</span><span class="p">])</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span><span class="nv">$arr</span><span class="p">[</span><span class="nv">$min</span><span class="p">],</span><span class="nv">$arr</span><span class="p">[</span><span class="nv">$i</span><span class="p">]);</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>Recursive: </p> <div class="mw-highlight mw-highlight-lang-php mw-content-ltr" dir="ltr"><pre><span></span><span class="k">function</span> <span class="nf">selectionsort</span><span class="p">(</span><span class="nv">$arr</span><span class="p">,</span><span class="nv">$result</span><span class="o">=</span><span class="k">array</span><span class="p">()){</span> <span class="k">if</span><span class="p">(</span><span class="nb">count</span><span class="p">(</span><span class="nv">$arr</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">){</span> <span class="k">return</span> <span class="nv">$result</span><span class="p">;</span> <span class="p">}</span> <span class="nv">$nresult</span> <span class="o">=</span> <span class="nv">$result</span><span class="p">;</span> <span class="nv">$nresult</span><span class="p">[]</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="nv">$arr</span><span class="p">);</span> <span class="nb">unset</span><span class="p">(</span><span class="nv">$arr</span><span class="p">[</span><span class="nb">array_search</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="nv">$arr</span><span class="p">),</span><span class="nv">$arr</span><span class="p">)]);</span> <span class="k">return</span> <span class="nx">selectionsort</span><span class="p">(</span><span class="nv">$arr</span><span class="p">,</span><span class="nv">$nresult</span><span class="p">);</span> <span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="PicoLisp"><a href="/wiki/Category:PicoLisp" title="Category:PicoLisp">PicoLisp</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>(de selectionSort (Lst) (map '((L) (and (cdr L) (xchg L (member (apply min @) L)))) Lst ) Lst )</pre></div> <h2><span id="PL.2FI"></span><span class="mw-headline" id="PL/I"><a href="/wiki/Category:PL/I" title="Category:PL/I">PL/I</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>Selection: procedure options (main); /* 2 November 2013 */ declare a(10) fixed binary initial ( 5, 7, 3, 98, 4, -3, 25, 20, 60, 17); put edit (trim(a)) (a, x(1)); call Selection_Sort (a); put skip edit (trim(a)) (a, x(1)); Selection_sort: procedure (a); declare a(*) fixed binary; declare t fixed binary; declare n fixed binary; declare (i, j, k) fixed binary; n = hbound(a,1); do j = 1 to n; k = j; t = a(j); do i = j+1 to n; if t > a(i) then do; t = a(i); k = i; end; end; a(k) = a(j); a(j) = t; end; end Selection_Sort; end Selection;</pre></div> <p>Results: </p> <pre>5 7 3 98 4 -3 25 20 60 17 -3 3 4 5 7 17 20 25 60 98 </pre> <h2><span class="mw-headline" id="PowerShell"><a href="/wiki/Category:PowerShell" title="Category:PowerShell">PowerShell</a> </span></h2> <div class="mw-highlight mw-highlight-lang-powershell mw-content-ltr" dir="ltr"><pre><span></span><span class="k">Function</span> <span class="n">SelectionSort</span><span class="p">(</span> <span class="no">[Array]</span> <span class="nv">$data</span> <span class="p">)</span> <span class="p">{</span> <span class="nv">$datal</span><span class="p">=</span><span class="nv">$data</span><span class="p">.</span><span class="n">length</span><span class="p">-</span><span class="n">1</span> <span class="n">0</span><span class="p">..(</span> <span class="nv">$datal</span> <span class="p">-</span> <span class="n">1</span> <span class="p">)</span> <span class="p">|</span> <span class="k">ForEach</span><span class="n">-Object</span> <span class="p">{</span> <span class="nv">$min</span> <span class="p">=</span> <span class="nv">$data</span><span class="p">[</span> <span class="nv">$_</span> <span class="p">]</span> <span class="nv">$mini</span> <span class="p">=</span> <span class="nv">$_</span> <span class="p">(</span> <span class="nv">$_</span> <span class="p">+</span> <span class="n">1</span> <span class="p">)..</span><span class="nv">$datal</span> <span class="p">|</span> <span class="k">ForEach</span><span class="n">-Object</span> <span class="p">{</span> <span class="k">if</span><span class="p">(</span> <span class="nv">$data</span><span class="p">[</span> <span class="nv">$_</span> <span class="p">]</span> <span class="o">-lt</span> <span class="nv">$min</span> <span class="p">)</span> <span class="p">{</span> <span class="nv">$min</span> <span class="p">=</span> <span class="nv">$data</span><span class="p">[</span> <span class="nv">$_</span> <span class="p">]</span> <span class="nv">$mini</span> <span class="p">=</span> <span class="nv">$_</span> <span class="p">}</span> <span class="p">}</span> <span class="nv">$temp</span> <span class="p">=</span> <span class="nv">$data</span><span class="p">[</span> <span class="nv">$_</span> <span class="p">]</span> <span class="nv">$data</span><span class="p">[</span> <span class="nv">$_</span> <span class="p">]</span> <span class="p">=</span> <span class="nv">$min</span> <span class="nv">$data</span><span class="p">[</span> <span class="nv">$mini</span> <span class="p">]</span> <span class="p">=</span> <span class="nv">$temp</span> <span class="p">}</span> <span class="nv">$data</span> <span class="p">}</span> <span class="nv">$l</span> <span class="p">=</span> <span class="n">100</span><span class="p">;</span> <span class="n">SelectionSort</span><span class="p">(</span> <span class="p">(</span> <span class="n">1</span><span class="p">..</span><span class="nv">$l</span> <span class="p">|</span> <span class="k">ForEach</span><span class="n">-Object</span> <span class="p">{</span> <span class="nv">$Rand</span> <span class="p">=</span> <span class="nb">New-Object</span> <span class="n">Random</span> <span class="p">}{</span> <span class="nv">$Rand</span><span class="p">.</span><span class="n">Next</span><span class="p">(</span> <span class="n">0</span><span class="p">,</span> <span class="nv">$l</span> <span class="p">-</span> <span class="n">1</span> <span class="p">)</span> <span class="p">}</span> <span class="p">)</span> <span class="p">)</span> </pre></div> <h2><span class="mw-headline" id="Prolog"><a href="/wiki/Category:Prolog" title="Category:Prolog">Prolog</a> </span></h2> <p>Works with <b>SWI-Prolog 6.3.11</b> (needs nth0/4). </p> <div class="mw-highlight mw-highlight-lang-prolog mw-content-ltr" dir="ltr"><pre><span></span><span class="nf">selection_sort</span><span class="p">([],</span> <span class="p">[]).</span> <span class="nf">selection_sort</span><span class="p">([</span><span class="nv">H</span> <span class="p">|</span> <span class="nv">L</span><span class="p">],</span> <span class="p">[</span><span class="nv">H1</span> <span class="p">|</span> <span class="nv">L2</span><span class="p">])</span> <span class="p">:-</span> <span class="nf">exchange</span><span class="p">(</span><span class="nv">H</span><span class="p">,</span> <span class="nv">L</span><span class="p">,</span> <span class="nv">H1</span><span class="p">,</span> <span class="nv">L1</span><span class="p">),</span> <span class="nf">selection_sort</span><span class="p">(</span><span class="nv">L1</span><span class="p">,</span> <span class="nv">L2</span><span class="p">).</span> <span class="nf">exchange</span><span class="p">(</span><span class="nv">H</span><span class="p">,</span> <span class="p">[],</span> <span class="nv">H</span><span class="p">,</span> <span class="p">[]).</span> <span class="nf">exchange</span><span class="p">(</span><span class="nv">H</span><span class="p">,</span> <span class="nv">L</span><span class="p">,</span> <span class="nv">H1</span><span class="p">,</span> <span class="nv">L1</span><span class="p">)</span> <span class="p">:-</span> <span class="nf">min_list</span><span class="p">(</span><span class="nv">L</span><span class="p">,</span> <span class="nv">H2</span><span class="p">),</span> <span class="p">(</span> <span class="nv">H</span> <span class="o"><</span> <span class="nv">H2</span> <span class="s s-Atom">-></span> <span class="nv">H1</span> <span class="o">=</span> <span class="nv">H</span><span class="p">,</span> <span class="nv">L1</span> <span class="o">=</span> <span class="nv">L</span> <span class="p">;</span> <span class="nv">H1</span> <span class="o">=</span> <span class="nv">H2</span><span class="p">,</span> <span class="c1">% does the exchange of the number H </span> <span class="c1">% and the min of the list</span> <span class="nf">nth0</span><span class="p">(</span><span class="nv">Ind</span><span class="p">,</span> <span class="nv">L</span><span class="p">,</span> <span class="nv">H1</span><span class="p">,</span> <span class="nv">L2</span><span class="p">),</span> <span class="nf">nth0</span><span class="p">(</span><span class="nv">Ind</span><span class="p">,</span> <span class="nv">L1</span><span class="p">,</span> <span class="nv">H</span><span class="p">,</span> <span class="nv">L2</span><span class="p">)).</span> </pre></div> <h2><span class="mw-headline" id="PureBasic"><a href="/wiki/Category:PureBasic" title="Category:PureBasic">PureBasic</a> </span></h2> <div class="mw-highlight mw-highlight-lang-basic mw-content-ltr" dir="ltr"><pre><span></span><span class="vg">Procedure</span><span class="w"> </span><span class="vg">selectionSort</span><span class="p">(</span><span class="vg">Array</span><span class="w"> </span><span class="vg">a</span><span class="p">(</span><span class="il">1</span><span class="p">))</span> <span class="w"> </span><span class="vg">Protected</span><span class="w"> </span><span class="vg">i</span><span class="p">,</span><span class="w"> </span><span class="vg">j</span><span class="p">,</span><span class="w"> </span><span class="vg">lastIndex</span><span class="p">,</span><span class="w"> </span><span class="vg">minIndex</span> <span class="w"> </span> <span class="w"> </span><span class="vg">lastIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">ArraySize</span><span class="p">(</span><span class="vg">a</span><span class="p">())</span> <span class="w"> </span><span class="vg">For</span><span class="w"> </span><span class="vg">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">0</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="vg">lastIndex</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="il">1</span> <span class="w"> </span><span class="vg">minIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">i</span> <span class="w"> </span><span class="vg">For</span><span class="w"> </span><span class="vg">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="il">1</span><span class="w"> </span><span class="vg">To</span><span class="w"> </span><span class="vg">lastIndex</span> <span class="w"> </span><span class="vg">If</span><span class="w"> </span><span class="vg">a</span><span class="p">(</span><span class="vg">minIndex</span><span class="p">)</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="vg">a</span><span class="p">(</span><span class="vg">j</span><span class="p">)</span> <span class="w"> </span><span class="vg">minIndex</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="vg">j</span> <span class="w"> </span><span class="vg">EndIf</span> <span class="w"> </span><span class="vg">Next</span> <span class="w"> </span><span class="vg">Swap</span><span class="w"> </span><span class="vg">a</span><span class="p">(</span><span class="vg">minIndex</span><span class="p">),</span><span class="w"> </span><span class="vg">a</span><span class="p">(</span><span class="vg">i</span><span class="p">)</span> <span class="w"> </span><span class="vg">Next</span><span class="w"> </span> <span class="vg">EndProcedure</span> </pre></div> <h2><span class="mw-headline" id="Python"><a href="/wiki/Category:Python" title="Category:Python">Python</a> </span></h2> <div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span></span><span class="k">def</span> <span class="nf">selection_sort</span><span class="p">(</span><span class="n">lst</span><span class="p">):</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">e</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">lst</span><span class="p">):</span> <span class="n">mn</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">lst</span><span class="p">)),</span> <span class="n">key</span><span class="o">=</span><span class="n">lst</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">)</span> <span class="n">lst</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">lst</span><span class="p">[</span><span class="n">mn</span><span class="p">]</span> <span class="o">=</span> <span class="n">lst</span><span class="p">[</span><span class="n">mn</span><span class="p">],</span> <span class="n">e</span> <span class="k">return</span> <span class="n">lst</span> </pre></div> <h2><span class="mw-headline" id="Qi"><a href="/wiki/Category:Qi" title="Category:Qi">Qi</a> </span></h2> <div class="examplemeta translation"><b>Translation of</b>: <a href="#sml">sml</a></div> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>(define select-r Small [] Output -> [Small | (selection-sort Output)] Small [X|Xs] Output -> (select-r X Xs [Small|Output]) where (< X Small) Small [X|Xs] Output -> (select-r Small Xs [X|Output])) (define selection-sort [] -> [] [First|Lst] -> (select-r First Lst [])) (selection-sort [8 7 4 3 2 0 9 1 5 6])</pre></div> <h2><span class="mw-headline" id="Quackery"><a href="/wiki/Category:Quackery" title="Category:Quackery">Quackery</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre> [ 0 swap behead swap witheach [ 2dup > iff [ nip nip i^ 1+ swap ] else drop ] drop ] is least ( [ --> n ) [ [] swap dup size times [ dup least pluck swap dip join ] drop ] is ssort ( [ --> [ ) [] 20 times [ 10 random join ] dup echo cr ssort echo cr</pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>[ 5 2 5 0 4 5 1 5 1 1 0 3 7 2 0 9 6 1 8 7 ] [ 0 0 0 1 1 1 1 2 2 3 4 5 5 5 5 6 7 7 8 9 ] </pre> <h2><span class="mw-headline" id="R"><a href="/wiki/Category:R" title="Category:R">R</a> </span></h2> <p>For loop: </p> <div class="mw-highlight mw-highlight-lang-r mw-content-ltr" dir="ltr"><pre><span></span><span class="n">selectionsort.loop</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">function</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="w"> </span><span class="n">lenx</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">length</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="w"> </span><span class="nf">for</span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="n">in</span><span class="w"> </span><span class="nf">seq_along</span><span class="p">(</span><span class="n">x</span><span class="p">))</span> <span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">mini</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="m">1</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nf">which.min</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="o">:</span><span class="n">lenx</span><span class="p">])</span> <span class="w"> </span><span class="n">start_</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">seq_len</span><span class="p">(</span><span class="n">i</span><span class="m">-1</span><span class="p">)</span> <span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">start_</span><span class="p">],</span><span class="w"> </span><span class="n">x</span><span class="p">[</span><span class="n">mini</span><span class="p">],</span><span class="w"> </span><span class="n">x</span><span class="p">[</span><span class="o">-</span><span class="nf">c</span><span class="p">(</span><span class="n">start_</span><span class="p">,</span><span class="w"> </span><span class="n">mini</span><span class="p">)])</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="n">x</span> <span class="p">}</span> </pre></div> <p>Recursive: </p><p>(A prettier solution, but, you may need to increase the value of options("expressions") to test it. Also, you may get a stack overflow if the length of the input vector is more than a few thousand.) </p> <div class="mw-highlight mw-highlight-lang-r mw-content-ltr" dir="ltr"><pre><span></span><span class="n">selectionsort.rec</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">function</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="w"> </span><span class="nf">if</span><span class="p">(</span><span class="nf">length</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="m">1</span><span class="p">)</span> <span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">mini</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="nf">which.min</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="w"> </span><span class="nf">c</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="n">mini</span><span class="p">],</span><span class="w"> </span><span class="nf">selectionsort</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="o">-</span><span class="n">mini</span><span class="p">]))</span> <span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="n">else</span><span class="w"> </span><span class="n">x</span> <span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="Ra"><a href="/wiki/Category:Ra" title="Category:Ra">Ra</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>class SelectionSort **Sort a list with the Selection Sort algorithm** on start args := program arguments .sort(args) print args define sort(list) is shared **Sort the list** test list := [4, 2, 7, 3] .sort(list) assert list = [2, 3, 4, 7] body count := list.count last := count - 1 for i in last minCandidate := i j := i + 1 while j < count if list[j] < list[minCandidate], minCandidate := j j :+ 1 temp := list[i] list[i] := list[minCandidate] list[minCandidate] := temp</pre></div> <h2><span class="mw-headline" id="Racket"><a href="/wiki/Category:Racket" title="Category:Racket">Racket</a> </span></h2> <div class="mw-highlight mw-highlight-lang-racket mw-content-ltr" dir="ltr"><pre><span></span><span class="kn">#lang </span><span class="nn">racket</span> <span class="p">(</span><span class="k">define</span><span class="w"> </span><span class="p">(</span><span class="n">selection-sort</span><span class="w"> </span><span class="n">xs</span><span class="p">)</span> <span class="w"> </span><span class="p">(</span><span class="k">cond</span><span class="w"> </span><span class="p">[(</span><span class="nb">empty?</span><span class="w"> </span><span class="n">xs</span><span class="p">)</span><span class="w"> </span><span class="o">'</span><span class="p">()]</span> <span class="w"> </span><span class="p">[</span><span class="k">else</span><span class="w"> </span><span class="p">(</span><span class="k">define</span><span class="w"> </span><span class="n">x0</span><span class="w"> </span><span class="p">(</span><span class="nb">apply</span><span class="w"> </span><span class="nb">min</span><span class="w"> </span><span class="n">xs</span><span class="p">))</span> <span class="w"> </span><span class="p">(</span><span class="nb">cons</span><span class="w"> </span><span class="n">x0</span><span class="w"> </span><span class="p">(</span><span class="n">selection-sort</span><span class="w"> </span><span class="p">(</span><span class="nb">remove</span><span class="w"> </span><span class="n">x0</span><span class="w"> </span><span class="n">xs</span><span class="p">)))]))</span> </pre></div> <h2><span class="mw-headline" id="Raku"><a href="/wiki/Category:Raku" title="Category:Raku">Raku</a> </span></h2> <p>(formerly Perl 6) Solution 1: </p> <div class="mw-highlight mw-highlight-lang-raku mw-content-ltr mw-highlight-lines" dir="ltr"><pre><span></span><span class="linenos" data-line="1"></span><span class="k">sub</span> <span class="n">selection_sort</span> ( <span class="nv">@a</span> <span class="k">is</span> <span class="nb">copy</span> ) { <span class="linenos" data-line="2"></span> <span class="k">for</span> <span class="mi">0</span> ..^ <span class="nv">@a</span>.<span class="nb">end</span> -> <span class="nv">$i</span> { <span class="linenos" data-line="3"></span> <span class="k">my</span> <span class="nv">$min</span> = [ <span class="nv">$i</span><span class="o">+</span><span class="mi">1</span> .. <span class="nv">@a</span>.<span class="nb">end</span> ].<span class="o">min</span>: { <span class="nv">@a</span>[<span class="nv">$_</span>] }; <span class="linenos" data-line="4"></span> <span class="nv">@a</span>[<span class="nv">$i</span>, <span class="nv">$min</span>] = <span class="nv">@a</span>[<span class="nv">$min</span>, <span class="nv">$i</span>] <span class="k">if</span> <span class="nv">@a</span>[<span class="nv">$i</span>] > <span class="nv">@a</span>[<span class="nv">$min</span>]; <span class="linenos" data-line="5"></span> } <span class="linenos" data-line="6"></span> <span class="k">return</span> <span class="nv">@a</span>; <span class="linenos" data-line="7"></span>} <span class="linenos" data-line="8"></span> <span class="linenos" data-line="9"></span><span class="k">my</span> <span class="nv">@data</span> = <span class="mi">22</span>, <span class="mi">7</span>, <span class="mi">2</span>, -<span class="mi">5</span>, <span class="mi">8</span>, <span class="mi">4</span>; <span class="linenos" data-line="10"></span><span class="nb">say</span> <span class="s">'input = '</span> ~ <span class="nv">@data</span>; <span class="linenos" data-line="11"></span><span class="nb">say</span> <span class="s">'output = '</span> ~ <span class="nv">@data</span>.<span class="nv">&selection_sort</span>; </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>input = 22 7 2 -5 8 4 output = -5 2 4 7 8 22 </pre> <p>Solution 2: </p> <div class="mw-highlight mw-highlight-lang-raku mw-content-ltr mw-highlight-lines" dir="ltr"><pre><span></span><span class="linenos" data-line="1"></span><span class="k">sub</span> <span class="n">selectionSort</span>(<span class="nv">@tmp</span>) { <span class="linenos" data-line="2"></span> <span class="k">for</span> ^<span class="nv">@tmp</span> -> <span class="nv">$i</span> { <span class="linenos" data-line="3"></span> <span class="k">my</span> <span class="nv">$min</span> = <span class="nv">$i</span>; <span class="nv">@tmp</span>[<span class="nv">$i</span>, <span class="nv">$_</span>] = <span class="nv">@tmp</span>[<span class="nv">$_</span>, <span class="nv">$i</span>] <span class="k">if</span> <span class="nv">@tmp</span>[<span class="nv">$min</span>] > <span class="nv">@tmp</span>[<span class="nv">$_</span>] <span class="k">for</span> <span class="nv">$i</span>^..^<span class="nv">@tmp</span>; <span class="linenos" data-line="4"></span> } <span class="linenos" data-line="5"></span> <span class="k">return</span> <span class="nv">@tmp</span>; <span class="linenos" data-line="6"></span>} </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>input = 22 7 2 -5 8 4 output = -5 2 4 7 8 22 </pre> <h2><span class="mw-headline" id="REXX"><a href="/wiki/Category:REXX" title="Category:REXX">REXX</a> </span></h2> <div class="mw-highlight mw-highlight-lang-rexx mw-content-ltr" dir="ltr"><pre><span></span><span class="cm">/*REXX program sorts a stemmed array using the selection─sort algorithm. */</span> <span class="kr">call</span><span class="w"> </span>init<span class="w"> </span><span class="cm">/*assign some values to an array: @. */</span> <span class="kr">call</span><span class="w"> </span>show<span class="w"> </span><span class="s">'before sort'</span><span class="w"> </span><span class="cm">/*show the before array elements. */</span> <span class="w"> </span><span class="kr">say</span><span class="w"> </span><span class="nb">copies</span><span class="o">(</span><span class="s">'▒'</span><span class="o">,</span><span class="w"> </span><span class="m">65</span><span class="o">)</span><span class="w"> </span><span class="cm">/*show a nice separator line (fence). */</span> <span class="kr">call</span><span class="w"> </span>selectionSort<span class="w"> </span><span class="err">#</span><span class="w"> </span><span class="cm">/*invoke selection sort (and # items). */</span> <span class="kr">call</span><span class="w"> </span>show<span class="w"> </span><span class="s">' after sort'</span><span class="w"> </span><span class="cm">/*show the after array elements. */</span> <span class="kr">exit</span><span class="w"> </span><span class="m">0</span><span class="w"> </span><span class="cm">/*stick a fork in it, we're all done. */</span> <span class="cm">/*──────────────────────────────────────────────────────────────────────────────────────*/</span> <span class="nl">init</span><span class="o">:</span><span class="w"> </span><span class="err">@</span><span class="o">.=</span><span class="err">;</span><span class="w"> </span><span class="err">@</span><span class="o">.</span><span class="m">1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">'---The seven hills of Rome:---'</span> <span class="w"> </span><span class="err">@</span><span class="o">.</span><span class="m">2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">'=============================='</span><span class="err">;</span><span class="w"> </span><span class="err">@</span><span class="o">.</span><span class="m">6</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">'Virminal'</span> <span class="w"> </span><span class="err">@</span><span class="o">.</span><span class="m">3</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">'Caelian'</span><span class="w"> </span><span class="err">;</span><span class="w"> </span><span class="err">@</span><span class="o">.</span><span class="m">7</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">'Esquiline'</span> <span class="w"> </span><span class="err">@</span><span class="o">.</span><span class="m">4</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">'Palatine'</span><span class="w"> </span><span class="err">;</span><span class="w"> </span><span class="err">@</span><span class="o">.</span><span class="m">8</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">'Quirinal'</span> <span class="w"> </span><span class="err">@</span><span class="o">.</span><span class="m">5</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">'Capitoline'</span><span class="w"> </span><span class="err">;</span><span class="w"> </span><span class="err">@</span><span class="o">.</span><span class="m">9</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">'Aventine'</span> <span class="w"> </span><span class="kr">do</span><span class="w"> </span><span class="err">#</span><span class="o">=</span><span class="m">1</span><span class="w"> </span><span class="kr">until</span><span class="w"> </span><span class="err">@</span><span class="o">.</span><span class="err">#</span><span class="o">==</span><span class="s">''</span><span class="err">;</span><span class="w"> </span><span class="kr">end</span><span class="w"> </span><span class="cm">/*find the number of items in the array*/</span> <span class="w"> </span><span class="err">#</span><span class="o">=</span><span class="w"> </span><span class="err">#</span><span class="o">-</span><span class="m">1</span><span class="err">;</span><span class="w"> </span><span class="kr">return</span><span class="w"> </span><span class="cm">/*adjust # (because of DO index). */</span> <span class="cm">/*──────────────────────────────────────────────────────────────────────────────────────*/</span> <span class="nf">selectionSort</span><span class="o">:</span><span class="w"> </span><span class="kd">procedure</span><span class="w"> </span>expose<span class="w"> </span><span class="err">@</span><span class="o">.</span><span class="err">;</span><span class="w"> </span><span class="kr">parse</span><span class="w"> </span><span class="kr">arg</span><span class="w"> </span>n <span class="w"> </span><span class="kr">do</span><span class="w"> </span>j<span class="o">=</span><span class="m">1</span><span class="w"> </span><span class="kr">for</span><span class="w"> </span>n<span class="o">-</span><span class="m">1</span><span class="err">;</span><span class="w"> </span>_<span class="o">=</span><span class="w"> </span><span class="err">@</span><span class="o">.</span>j<span class="err">;</span><span class="w"> </span>p<span class="o">=</span><span class="w"> </span>j <span class="w"> </span><span class="kr">do</span><span class="w"> </span>k<span class="o">=</span>j<span class="o">+</span><span class="m">1</span><span class="w"> </span><span class="kr">to</span><span class="w"> </span>n<span class="err">;</span><span class="w"> </span><span class="kr">if</span><span class="w"> </span><span class="err">@</span><span class="o">.</span>k<span class="o">>=</span>_<span class="w"> </span><span class="kr">then</span><span class="w"> </span><span class="kr">iterate</span> <span class="w"> </span>_<span class="o">=</span><span class="w"> </span><span class="err">@</span><span class="o">.</span>k<span class="err">;</span><span class="w"> </span>p<span class="o">=</span><span class="w"> </span>k<span class="w"> </span><span class="cm">/*this item is out─of─order, swap later*/</span> <span class="w"> </span><span class="kr">end</span><span class="w"> </span><span class="cm">/*k*/</span> <span class="w"> </span><span class="kr">if</span><span class="w"> </span>p<span class="o">==</span>j<span class="w"> </span><span class="kr">then</span><span class="w"> </span><span class="kr">iterate</span><span class="w"> </span><span class="cm">/*if the same, the order of items is OK*/</span> <span class="w"> </span>_<span class="o">=</span><span class="w"> </span><span class="err">@</span><span class="o">.</span>j<span class="err">;</span><span class="w"> </span><span class="err">@</span><span class="o">.</span>j<span class="o">=</span><span class="w"> </span><span class="err">@</span><span class="o">.</span>p<span class="err">;</span><span class="w"> </span><span class="err">@</span><span class="o">.</span>p<span class="o">=</span><span class="w"> </span>_<span class="w"> </span><span class="cm">/*swap 2 items that're out─of─sequence.*/</span> <span class="w"> </span><span class="kr">end</span><span class="w"> </span><span class="cm">/*j*/</span> <span class="w"> </span><span class="kr">return</span> <span class="cm">/*──────────────────────────────────────────────────────────────────────────────────────*/</span> <span class="nl">show</span><span class="o">:</span><span class="w"> </span><span class="kr">do</span><span class="w"> </span>i<span class="o">=</span><span class="m">1</span><span class="w"> </span><span class="kr">for</span><span class="w"> </span><span class="err">#;</span><span class="w"> </span><span class="kr">say</span><span class="w"> </span><span class="s">' element'</span><span class="w"> </span><span class="nb">right</span><span class="o">(</span>i<span class="o">,</span><span class="nb">length</span><span class="o">(</span><span class="err">#</span><span class="o">))</span><span class="w"> </span><span class="nb">arg</span><span class="o">(</span><span class="m">1</span><span class="o">)</span><span class="s">":"</span><span class="w"> </span><span class="err">@</span><span class="o">.</span>i<span class="err">;</span><span class="w"> </span><span class="kr">end</span><span class="err">;</span><span class="w"> </span><span class="kr">return</span> </pre></div> <div> <dl><dt>output:</dt></dl></div> <pre> element 1 before sort: ---The seven hills of Rome:--- element 2 before sort: ============================== element 3 before sort: Caelian element 4 before sort: Palatine element 5 before sort: Capitoline element 6 before sort: Virminal element 7 before sort: Esquiline element 8 before sort: Quirinal element 9 before sort: Aventine ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ element 1 after sort: ---The seven hills of Rome:--- element 2 after sort: ============================== element 3 after sort: Aventine element 4 after sort: Caelian element 5 after sort: Capitoline element 6 after sort: Esquiline element 7 after sort: Palatine element 8 after sort: Quirinal element 9 after sort: Virminal </pre> <h2><span class="mw-headline" id="Ring"><a href="/wiki/Category:Ring" title="Category:Ring">Ring</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>aList = [7,6,5,9,8,4,3,1,2,0] see sortList(aList) func sortList list count = len(list) + 1 last = count - 1 for i = 1 to last minCandidate = i j = i + 1 while j < count if list[j] < list[minCandidate] minCandidate = j ok j = j + 1 end temp = list[i] list[i] = list[minCandidate] list[minCandidate] = temp next return list</pre></div> <h2><span class="mw-headline" id="Ruby"><a href="/wiki/Category:Ruby" title="Category:Ruby">Ruby</a> </span></h2> <div class="mw-highlight mw-highlight-lang-ruby mw-content-ltr" dir="ltr"><pre><span></span><span class="c1"># a relatively readable version - creates a distinct array</span> <span class="k">def</span><span class="w"> </span><span class="nf">sequential_sort</span><span class="p">(</span><span class="n">array</span><span class="p">)</span> <span class="w"> </span><span class="n">sorted</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[]</span> <span class="w"> </span><span class="k">while</span><span class="w"> </span><span class="n">array</span><span class="o">.</span><span class="n">any?</span> <span class="w"> </span><span class="n">index_of_smallest_element</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">find_smallest_index</span><span class="p">(</span><span class="n">array</span><span class="p">)</span><span class="w"> </span><span class="c1"># defined below</span> <span class="w"> </span><span class="n">sorted</span><span class="w"> </span><span class="o"><<</span><span class="w"> </span><span class="n">array</span><span class="o">.</span><span class="n">delete_at</span><span class="p">(</span><span class="n">index_of_smallest_element</span><span class="p">)</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="n">sorted</span> <span class="k">end</span> <span class="k">def</span><span class="w"> </span><span class="nf">find_smallest_index</span><span class="p">(</span><span class="n">array</span><span class="p">)</span> <span class="w"> </span><span class="n">smallest_element</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">array</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span> <span class="w"> </span><span class="n">smallest_index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span> <span class="w"> </span><span class="n">array</span><span class="o">.</span><span class="n">each_with_index</span><span class="w"> </span><span class="k">do</span><span class="w"> </span><span class="o">|</span><span class="n">ele</span><span class="p">,</span><span class="w"> </span><span class="n">idx</span><span class="o">|</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">ele</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">smallest_element</span> <span class="w"> </span><span class="n">smallest_element</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ele</span> <span class="w"> </span><span class="n">smallest_index</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">idx</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="n">smallest_index</span> <span class="k">end</span> <span class="nb">puts</span><span class="w"> </span><span class="s2">"sequential_sort([9, 6, 8, 7, 5]): </span><span class="si">#{</span><span class="n">sequential_sort</span><span class="p">(</span><span class="o">[</span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">5</span><span class="o">]</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span> <span class="c1"># prints: sequential_sort([9, 6, 8, 7, 5]): [5, 6, 7, 8, 9]</span> <span class="c1"># more efficient version - swaps the array's elements in place</span> <span class="k">def</span><span class="w"> </span><span class="nf">sequential_sort_with_swapping</span><span class="p">(</span><span class="n">array</span><span class="p">)</span> <span class="w"> </span><span class="n">array</span><span class="o">.</span><span class="n">each_with_index</span><span class="w"> </span><span class="k">do</span><span class="w"> </span><span class="o">|</span><span class="n">element</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="o">|</span> <span class="w"> </span><span class="n">smallest_unsorted_element_so_far</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element</span> <span class="w"> </span><span class="n">smallest_unsorted_index_so_far</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">index</span> <span class="w"> </span><span class="p">(</span><span class="n">index</span><span class="o">+</span><span class="mi">1</span><span class="o">...</span><span class="n">array</span><span class="o">.</span><span class="n">length</span><span class="p">)</span><span class="o">.</span><span class="n">each</span><span class="w"> </span><span class="k">do</span><span class="w"> </span><span class="o">|</span><span class="n">index_value</span><span class="o">|</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">array</span><span class="o">[</span><span class="n">index_value</span><span class="o">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">smallest_unsorted_element_so_far</span> <span class="w"> </span><span class="n">smallest_unsorted_element_so_far</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">array</span><span class="o">[</span><span class="n">index_value</span><span class="o">]</span> <span class="w"> </span><span class="n">smallest_unsorted_index_so_far</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">index_value</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="c1"># swap index_value-th smallest element for index_value-th element</span> <span class="w"> </span><span class="n">array</span><span class="o">[</span><span class="n">index</span><span class="o">]</span><span class="p">,</span><span class="w"> </span><span class="n">array</span><span class="o">[</span><span class="n">smallest_unsorted_index_so_far</span><span class="o">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">array</span><span class="o">[</span><span class="n">smallest_unsorted_index_so_far</span><span class="o">]</span><span class="p">,</span><span class="w"> </span><span class="n">array</span><span class="o">[</span><span class="n">index</span><span class="o">]</span> <span class="w"> </span><span class="k">end</span> <span class="w"> </span><span class="n">array</span> <span class="k">end</span> <span class="nb">puts</span><span class="w"> </span><span class="s2">"sequential_sort_with_swapping([7,6,5,9,8,4,3,1,2,0]): </span><span class="si">#{</span><span class="n">sequential_sort_with_swapping</span><span class="p">(</span><span class="o">[</span><span class="mi">7</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">0</span><span class="o">]</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span> <span class="c1"># prints: sequential_sort_with_swapping([7,6,5,9,8,4,3,1,2,0]): [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</span> </pre></div> <h2><span class="mw-headline" id="Run_BASIC"><a href="/wiki/Category:Run_BASIC" title="Category:Run BASIC">Run BASIC</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>siz = 10 dim srdData(siz) for i = 1 to siz srtData(i) = rnd(0) * 100 next i FOR i = 1 TO siz-1 lo = i FOR j = (i + 1) TO siz IF srtData(j) < srtData(lo) lo = j NEXT j if i <> lo then temp = srtData(i) srtData(i) = srtData(lo) srtData(lo) = temp end if NEXT i for i = 1 to siz print i;chr$(9);srtData(i) next i</pre></div> <pre>1 20.5576419 2 32.4299311 3 48.345375 4 54.135847 5 63.1427764 6 67.8079128 7 85.2134895 8 91.3576602 9 95.4280853 10 98.8323211</pre> <h2><span class="mw-headline" id="Rust"><a href="/wiki/Category:Rust" title="Category:Rust">Rust</a> </span></h2> <div class="mw-highlight mw-highlight-lang-rust mw-content-ltr" dir="ltr"><pre><span></span><span class="k">fn</span> <span class="nf">selection_sort</span><span class="p">(</span><span class="n">array</span>: <span class="kp">&</span><span class="nc">mut</span><span class="w"> </span><span class="p">[</span><span class="kt">i32</span><span class="p">])</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">min</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="o">..</span><span class="n">array</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span><span class="o">..</span><span class="n">array</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">min</span><span class="p">]</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">i</span><span class="p">];</span> <span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">min</span><span class="p">];</span> <span class="w"> </span><span class="n">array</span><span class="p">[</span><span class="n">min</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> <span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="mi">9</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mi">5</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="w"> </span><span class="p">];</span> <span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">"The initial array is {:?}"</span><span class="p">,</span><span class="w"> </span><span class="n">array</span><span class="p">);</span> <span class="w"> </span><span class="n">selection_sort</span><span class="p">(</span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">array</span><span class="p">);</span> <span class="w"> </span><span class="fm">println!</span><span class="p">(</span><span class="s">" The sorted array is {:?}"</span><span class="p">,</span><span class="w"> </span><span class="n">array</span><span class="p">);</span> <span class="p">}</span> </pre></div><p>Another way:</p><div class="mw-highlight mw-highlight-lang-rust mw-content-ltr" dir="ltr"><pre><span></span><span class="k">fn</span> <span class="nf">selection_sort</span><span class="o"><</span><span class="n">T</span>: <span class="nc">std</span>::<span class="n">cmp</span>::<span class="nb">PartialOrd</span><span class="o">></span><span class="p">(</span><span class="n">arr</span>: <span class="kp">&</span><span class="nc">mut</span><span class="w"> </span><span class="p">[</span><span class="n">T</span><span class="p">])</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="o">..</span><span class="w"> </span><span class="n">arr</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">unsorted</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&</span><span class="k">mut</span><span class="w"> </span><span class="n">arr</span><span class="p">[</span><span class="n">i</span><span class="o">..</span><span class="p">];</span> <span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">unsorted_min</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="p">,</span><span class="w"> </span><span class="n">entry</span><span class="p">)</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">unsorted</span><span class="p">.</span><span class="n">iter</span><span class="p">().</span><span class="n">enumerate</span><span class="p">()</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">*</span><span class="n">entry</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">unsorted</span><span class="p">[</span><span class="n">unsorted_min</span><span class="p">]</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">unsorted_min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span><span class="p">;</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="n">unsorted</span><span class="p">.</span><span class="n">swap</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">unsorted_min</span><span class="p">);</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="Scala"><a href="/wiki/Category:Scala" title="Category:Scala">Scala</a> </span></h2> <div class="mw-highlight mw-highlight-lang-scala mw-content-ltr" dir="ltr"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">swap</span><span class="p">(</span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="nc">Array</span><span class="p">[</span><span class="nc">Int</span><span class="p">],</span><span class="w"> </span><span class="n">i1</span><span class="p">:</span><span class="w"> </span><span class="nc">Int</span><span class="p">,</span><span class="w"> </span><span class="n">i2</span><span class="p">:</span><span class="w"> </span><span class="nc">Int</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">i1</span><span class="p">);</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">i1</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">i2</span><span class="p">);</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">i2</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="p">}</span> <span class="k">def</span><span class="w"> </span><span class="nf">selectionSort</span><span class="p">(</span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="nc">Array</span><span class="p">[</span><span class="nc">Int</span><span class="p">])</span><span class="w"> </span><span class="o">=</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="n">until</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span> <span class="w"> </span><span class="n">swap</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="n">until</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">).</span><span class="n">foldLeft</span><span class="p">(</span><span class="n">i</span><span class="p">)((</span><span class="n">currMin</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="p">)</span><span class="w"> </span><span class="o">=></span><span class="w"> </span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">(</span><span class="n">index</span><span class="p">)</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">currMin</span><span class="p">))</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="n">currMin</span><span class="p">))</span> </pre></div> <p>This version avoids the extra definition by using a function literal: </p> <div class="mw-highlight mw-highlight-lang-scala mw-content-ltr" dir="ltr"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">selectionSort</span><span class="p">(</span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="nc">Array</span><span class="p">[</span><span class="nc">Int</span><span class="p">])</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="n">until</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="w"> </span> <span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">(</span><span class="n">i1</span><span class="p">:</span><span class="w"> </span><span class="nc">Int</span><span class="p">,</span><span class="w"> </span><span class="n">i2</span><span class="p">:</span><span class="w"> </span><span class="nc">Int</span><span class="p">)</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">i1</span><span class="p">);</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">i1</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">i2</span><span class="p">);</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">i2</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span><span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">)</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="n">until</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">size</span><span class="p">).</span><span class="n">foldLeft</span><span class="p">(</span><span class="n">i</span><span class="p">)((</span><span class="n">currMin</span><span class="p">,</span><span class="w"> </span><span class="n">index</span><span class="p">)</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">(</span><span class="n">index</span><span class="p">)</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">a</span><span class="p">(</span><span class="n">currMin</span><span class="p">))</span><span class="w"> </span><span class="n">index</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="n">currMin</span><span class="p">)</span><span class="w"> </span><span class="p">)</span> </pre></div> <p>Functional way: </p> <div class="mw-highlight mw-highlight-lang-scala mw-content-ltr" dir="ltr"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">selectionSort</span><span class="p">[</span><span class="nc">T</span><span class="w"> </span><span class="o"><%</span><span class="w"> </span><span class="nc">Ordered</span><span class="p">[</span><span class="nc">T</span><span class="p">]](</span><span class="n">list</span><span class="p">:</span><span class="w"> </span><span class="nc">List</span><span class="p">[</span><span class="nc">T</span><span class="p">]):</span><span class="w"> </span><span class="nc">List</span><span class="p">[</span><span class="nc">T</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">def</span><span class="w"> </span><span class="nf">remove</span><span class="p">(</span><span class="n">e</span><span class="p">:</span><span class="w"> </span><span class="nc">T</span><span class="p">,</span><span class="w"> </span><span class="n">list</span><span class="p">:</span><span class="w"> </span><span class="nc">List</span><span class="p">[</span><span class="nc">T</span><span class="p">]):</span><span class="w"> </span><span class="nc">List</span><span class="p">[</span><span class="nc">T</span><span class="p">]</span><span class="w"> </span><span class="o">=</span> <span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="k">match</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="nc">Nil</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="nc">Nil</span> <span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">::</span><span class="w"> </span><span class="n">xs</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="n">xs</span> <span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">::</span><span class="w"> </span><span class="n">xs</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">::</span><span class="w"> </span><span class="n">remove</span><span class="p">(</span><span class="n">e</span><span class="p">,</span><span class="w"> </span><span class="n">xs</span><span class="p">)</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="n">list</span><span class="w"> </span><span class="k">match</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="nc">Nil</span><span class="w"> </span><span class="o">=></span><span class="w"> </span><span class="nc">Nil</span> <span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="o">=></span> <span class="w"> </span><span class="kd">val</span><span class="w"> </span><span class="n">min</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">list</span><span class="p">.</span><span class="n">min</span> <span class="w"> </span><span class="n">min</span><span class="w"> </span><span class="o">::</span><span class="w"> </span><span class="n">selectionSort</span><span class="p">(</span><span class="n">remove</span><span class="p">(</span><span class="n">min</span><span class="p">,</span><span class="w"> </span><span class="n">list</span><span class="p">))</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="Seed7"><a href="/wiki/Category:Seed7" title="Category:Seed7">Seed7</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>const proc: selectionSort (inout array elemType: arr) is func local var integer: i is 0; var integer: j is 0; var integer: min is 0; var elemType: help is elemType.value; begin for i range 1 to length(arr) - 1 do min := i; for j range i + 1 to length(arr) do if arr[j] < arr[min] then min := j; end if; end for; help := arr[min]; arr[min] := arr[i]; arr[i] := help; end for; end func;</pre></div> <p>Original source: <a rel="nofollow" class="external autonumber" href="http://seed7.sourceforge.net/algorith/sorting.htm#selectionSort">[1]</a> </p> <h2><span class="mw-headline" id="Sidef"><a href="/wiki/Category:Sidef" title="Category:Sidef">Sidef</a> </span></h2> <div class="examplemeta translation"><b>Translation of</b>: <a href="#Ruby">Ruby</a></div> <div class="mw-highlight mw-highlight-lang-ruby mw-content-ltr" dir="ltr"><pre><span></span><span class="k">class</span><span class="w"> </span><span class="nc">Array</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nb">method</span><span class="w"> </span><span class="n">selectionsort</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="o">^</span><span class="p">(</span><span class="nb">self</span><span class="o">.</span><span class="n">end</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">var</span><span class="w"> </span><span class="n">min_idx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="w"> </span><span class="o">..</span><span class="w"> </span><span class="nb">self</span><span class="o">.</span><span class="n">end</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nb">self</span><span class="o">[</span><span class="n">j</span><span class="o">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nb">self</span><span class="o">[</span><span class="n">min_idx</span><span class="o">]</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">min_idx</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="nb">self</span><span class="o">.</span><span class="n">swap</span><span class="p">(</span><span class="n">i</span><span class="p">,</span><span class="w"> </span><span class="n">min_idx</span><span class="p">)</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">self</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> <span class="n">var</span><span class="w"> </span><span class="n">nums</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="mi">7</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">0</span><span class="o">]</span><span class="p">;</span> <span class="n">say</span><span class="w"> </span><span class="n">nums</span><span class="o">.</span><span class="n">selectionsort</span><span class="p">;</span> <span class="n">var</span><span class="w"> </span><span class="n">strs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">[</span><span class="s2">"John"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Kate"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Zerg"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Alice"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Joe"</span><span class="p">,</span><span class="w"> </span><span class="s2">"Jane"</span><span class="o">]</span><span class="p">;</span> <span class="n">say</span><span class="w"> </span><span class="n">strs</span><span class="o">.</span><span class="n">selectionsort</span><span class="p">;</span> </pre></div> <h2><span class="mw-headline" id="Standard_ML"><a href="/wiki/Category:Standard_ML" title="Category:Standard ML">Standard ML</a> </span></h2> <div class="mw-highlight mw-highlight-lang-sml mw-content-ltr" dir="ltr"><pre><span></span><span class="kr">fun</span> <span class="nf">selection_sort</span> <span class="p">[]</span> <span class="p">=</span> <span class="p">[]</span> <span class="p">|</span> <span class="nf">selection_sort</span> <span class="p">(</span><span class="n">first::lst</span><span class="p">)</span> <span class="p">=</span> <span class="kr">let</span> <span class="kr">val</span> <span class="p">(</span><span class="n">small</span><span class="p">,</span> <span class="n">output</span><span class="p">)</span> <span class="nv">=</span> <span class="n">foldl</span> <span class="p">(</span><span class="kr">fn</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="p">(</span><span class="n">small</span><span class="p">,</span> <span class="n">output</span><span class="p">))</span> <span class="p">=></span> <span class="kr">if</span> <span class="n">x</span> <span class="n"><</span> <span class="n">small</span> <span class="kr">then</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">small::output</span><span class="p">)</span> <span class="kr">else</span> <span class="p">(</span><span class="n">small</span><span class="p">,</span> <span class="n">x::output</span><span class="p">)</span> <span class="p">)</span> <span class="p">(</span><span class="n">first</span><span class="p">,</span> <span class="p">[])</span> <span class="n">lst</span> <span class="kr">in</span> <span class="n">small</span> <span class="n">::</span> <span class="n">selection_sort</span> <span class="n">output</span> <span class="kr">end</span> </pre></div> <h2><span class="mw-headline" id="Stata"><a href="/wiki/Category:Stata" title="Category:Stata">Stata</a> </span></h2> <div class="mw-highlight mw-highlight-lang-stata mw-content-ltr" dir="ltr"><pre><span></span><span class="k">mata</span> function selection_sort(real vector a) { real<span class="k"> scalar</span> i, j, k,<span class="k"> n</span> <span class="k"> n</span> = length(a) <span class="k"> for</span> (i = <span class="m">1</span>; i <span class="o"><</span><span class="k"> n</span>; i<span class="o">++</span>) { k = i <span class="k"> for</span> (j = i<span class="m">+1</span>; j <span class="o"><=</span><span class="k"> n</span>; j<span class="o">++</span>) { <span class="k"> if</span> (a[j] <span class="o"><</span> a[k]) k = j } <span class="k"> if</span> (k <span class="o">!=</span> i) a[(i, k)] = a[(k, i)] } } <span class="k">end</span> </pre></div> <h2><span class="mw-headline" id="Swift"><a href="/wiki/Category:Swift" title="Category:Swift">Swift</a> </span></h2> <div class="mw-highlight mw-highlight-lang-swift mw-content-ltr" dir="ltr"><pre><span></span><span class="kd">func</span> <span class="nf">selectionSort</span><span class="p">(</span><span class="kr">inout</span> <span class="n">arr</span><span class="p">:[</span><span class="nb">Int</span><span class="p">])</span> <span class="p">{</span> <span class="kd">var</span> <span class="nv">min</span><span class="p">:</span><span class="nb">Int</span> <span class="k">for</span> <span class="n">n</span> <span class="k">in</span> <span class="mf">0.</span><span class="p">.<</span><span class="n">arr</span><span class="p">.</span><span class="bp">count</span> <span class="p">{</span> <span class="bp">min</span> <span class="p">=</span> <span class="n">n</span> <span class="k">for</span> <span class="n">x</span> <span class="k">in</span> <span class="n">n</span><span class="o">+</span><span class="mf">1.</span><span class="p">.<</span><span class="n">arr</span><span class="p">.</span><span class="bp">count</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o"><</span> <span class="n">arr</span><span class="p">[</span><span class="bp">min</span><span class="p">])</span> <span class="p">{</span> <span class="bp">min</span> <span class="p">=</span> <span class="n">x</span> <span class="p">}</span> <span class="p">}</span> <span class="k">if</span> <span class="bp">min</span> <span class="o">!=</span> <span class="n">n</span> <span class="p">{</span> <span class="kd">let</span> <span class="nv">temp</span> <span class="p">=</span> <span class="n">arr</span><span class="p">[</span><span class="bp">min</span><span class="p">]</span> <span class="n">arr</span><span class="p">[</span><span class="bp">min</span><span class="p">]</span> <span class="p">=</span> <span class="n">arr</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="n">arr</span><span class="p">[</span><span class="n">n</span><span class="p">]</span> <span class="p">=</span> <span class="n">temp</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <h2><span class="mw-headline" id="Tcl"><a href="/wiki/Category:Tcl" title="Category:Tcl">Tcl</a> </span></h2> <div class="examplemeta libheader"><b>Library:</b> <a href="/wiki/Category:Tcllib" title="Category:Tcllib">Tcllib</a> <small>(<b>Package:</b> struct::list)</small></div> <div class="mw-highlight mw-highlight-lang-tcl mw-content-ltr" dir="ltr"><pre><span></span><span class="nb">package</span><span class="w"> </span>require<span class="w"> </span>Tcl<span class="w"> </span><span class="mf">8.5</span> <span class="nb">package</span><span class="w"> </span>require<span class="w"> </span>struct::list <span class="k">proc</span><span class="w"> </span>selectionsort<span class="w"> </span><span class="k">{</span><span class="nv">A</span><span class="k">}</span><span class="w"> </span><span class="k">{</span> <span class="w"> </span><span class="k">set</span><span class="w"> </span>len<span class="w"> </span><span class="k">[</span><span class="nb">llength</span><span class="w"> </span><span class="nv">$A</span><span class="k">]</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="k">{set</span><span class="w"> </span>i<span class="w"> </span><span class="mi">0</span><span class="k">}</span><span class="w"> </span><span class="k">{</span><span class="nv">$i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nv">$len</span><span class="w"> </span><span class="nv">-</span><span class="w"> </span><span class="mi">1</span><span class="k">}</span><span class="w"> </span><span class="k">{</span><span class="nb">incr</span><span class="w"> </span>i<span class="k">}</span><span class="w"> </span><span class="k">{</span> <span class="w"> </span><span class="k">set</span><span class="w"> </span>min_idx<span class="w"> </span><span class="k">[expr</span><span class="w"> </span><span class="k">{</span><span class="nv">$i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nv">1</span><span class="k">}]</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="k">{set</span><span class="w"> </span>j<span class="w"> </span><span class="nv">$min_idx</span><span class="k">}</span><span class="w"> </span><span class="k">{</span><span class="nv">$j</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="nv">$len</span><span class="k">}</span><span class="w"> </span><span class="k">{</span><span class="nb">incr</span><span class="w"> </span>j<span class="k">}</span><span class="w"> </span><span class="k">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="k">{[</span><span class="nb">lindex</span><span class="w"> </span><span class="nv">$A</span><span class="w"> </span><span class="nv">$j</span><span class="k">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="k">[</span><span class="nb">lindex</span><span class="w"> </span><span class="nv">$A</span><span class="w"> </span><span class="nv">$min_idx</span><span class="k">]}</span><span class="w"> </span><span class="k">{</span> <span class="w"> </span><span class="k">set</span><span class="w"> </span>min_idx<span class="w"> </span><span class="nv">$j</span> <span class="w"> </span><span class="k">}</span> <span class="w"> </span><span class="k">}</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="k">{[</span><span class="nb">lindex</span><span class="w"> </span><span class="nv">$A</span><span class="w"> </span><span class="nv">$i</span><span class="k">]</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="k">[</span><span class="nb">lindex</span><span class="w"> </span><span class="nv">$A</span><span class="w"> </span><span class="nv">$min_idx</span><span class="k">]}</span><span class="w"> </span><span class="k">{</span> <span class="w"> </span><span class="nv">struct</span><span class="o">::</span>list<span class="w"> </span>swap<span class="w"> </span>A<span class="w"> </span><span class="nv">$i</span><span class="w"> </span><span class="nv">$min_idx</span> <span class="w"> </span><span class="k">}</span> <span class="w"> </span><span class="k">}</span> <span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nv">$A</span> <span class="k">}</span> <span class="nb">puts</span><span class="w"> </span><span class="k">[</span><span class="nv">selectionsort</span><span class="w"> </span><span class="k">{</span><span class="nv">8</span><span class="w"> </span><span class="mi">6</span><span class="w"> </span><span class="mi">4</span><span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="mi">3</span><span class="w"> </span><span class="mi">5</span><span class="w"> </span><span class="mi">7</span><span class="w"> </span><span class="mi">9</span><span class="k">}]</span><span class="w"> </span><span class="k">;</span><span class="c"># => 1 2 3 4 5 6 7 8 9</span> </pre></div> <h2><span class="mw-headline" id="TI-83_BASIC"><a href="/wiki/Category:TI-83_BASIC" title="Category:TI-83 BASIC">TI-83 BASIC</a> </span></h2> <p>Store input into L<sub>1</sub> and prgmSORTSLCT will store the sorted output into L<sub>2</sub>. </p> <pre>:L<sub>1</sub>→L<sub>2</sub> :dim(L<sub>2</sub>)→I :For(A,1,I) :A→C :0→X :For(B,A,I) :If L<sub>2</sub>(B)<L<sub>2</sub>(C) :Then :B→C :1→X :End :End :If X=1 :Then :L<sub>2</sub>(C)→B :L<sub>2</sub>(A)→L<sub>2</sub>(C) :B→L<sub>2</sub>(A) :End :End :DelVar A :DelVar B :DelVar C :DelVar I :DelVar X :Return </pre> <h2><span id="uBasic.2F4tH"></span><span class="mw-headline" id="uBasic/4tH"><a href="/wiki/Category:UBasic/4tH" title="Category:UBasic/4tH">uBasic/4tH</a> </span></h2> <div class="mw-highlight mw-highlight-lang-text mw-content-ltr" dir="ltr"><pre><span></span>PRINT "Selection sort:" n = FUNC (_InitArray) PROC _ShowArray (n) PROC _Selectionsort (n) PROC _ShowArray (n) PRINT END _Selectionsort PARAM (1) ' Selection sort LOCAL (3) FOR b@ = 0 TO a@-1 c@ = b@ FOR d@ = b@ TO a@-1 IF @(d@) < @(c@) THEN c@ = d@ NEXT IF b@ # c@ THEN PROC _Swap (b@, c@) NEXT RETURN _Swap PARAM(2) ' Swap two array elements PUSH @(a@) @(a@) = @(b@) @(b@) = POP() RETURN _InitArray ' Init example array PUSH 4, 65, 2, -31, 0, 99, 2, 83, 782, 1 FOR i = 0 TO 9 @(i) = POP() NEXT RETURN (i) _ShowArray PARAM (1) ' Show array subroutine FOR i = 0 TO a@-1 PRINT @(i), NEXT PRINT RETURN </pre></div> <h2><span class="mw-headline" id="Ursala"><a href="/wiki/Category:Ursala" title="Category:Ursala">Ursala</a> </span></h2> <p>The selection_sort function is parameterized by a relational predicate p. There are no arrays in Ursala so it uses a list, and the selected item is deleted from the list and inserted into another on each iteration rather than swapped with a preceding item of the same list. </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>#import std selection_sort "p" = @iNX ~&l->rx ^(gldif ==,~&r)^/~&l ^|C/"p"$- ~&</pre></div> <p>This is already a bad way to code a sorting algorithm in this language, but with only a bit more work, we can get a bigger and slower version that more closely simulates the operations of repeatedly reordering an array. </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>selection_sort "p" = ~&itB^?a\~&a ^|JahPfatPRC/~& ~=-~BrhPltPClhPrtPCTlrTQrS^D/"p"$- ~&</pre></div> <p>Here is a test program sorting by the partial order relation on natural numbers. </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>#import nat #cast %nL example = selection_sort(nleq) <294,263,240,473,596,392,621,348,220,815></pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre><220,240,263,294,348,392,473,596,621,815></pre> <h2><span class="mw-headline" id="VBA"><a href="/wiki/Category:VBA" title="Category:VBA">VBA</a> </span></h2> <p>I shameless stole the swap function from the bubblesort VBscript implementation. </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>sub swap( byref a, byref b) dim tmp tmp = a a = b b = tmp end sub function selectionSort (a) for i = 0 to ubound(a) k = i for j=i+1 to ubound(a) if a(j) < a(i) then swap a(i), a(j) end if next next selectionSort = a end function</pre></div> <h2><span class="mw-headline" id="VBScript"><a href="/wiki/Category:VBScript" title="Category:VBScript">VBScript</a> </span></h2> <div class="mw-highlight mw-highlight-lang-vbnet mw-content-ltr" dir="ltr"><pre><span></span><span class="k">Function</span><span class="w"> </span><span class="nf">Selection_Sort</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="w"> </span><span class="n">arr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Split</span><span class="p">(</span><span class="n">s</span><span class="p">,</span><span class="s">","</span><span class="p">)</span> <span class="w"> </span><span class="k">For</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="k">To</span><span class="w"> </span><span class="n">UBound</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span> <span class="w"> </span><span class="k">For</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">To</span><span class="w"> </span><span class="n">UBound</span><span class="p">(</span><span class="n">arr</span><span class="p">)</span> <span class="w"> </span><span class="n">temp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arr</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="w"> </span><span class="k">If</span><span class="w"> </span><span class="n">arr</span><span class="p">(</span><span class="n">j</span><span class="p">)</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">arr</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="k">Then</span> <span class="w"> </span><span class="n">arr</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">arr</span><span class="p">(</span><span class="n">j</span><span class="p">)</span> <span class="w"> </span><span class="n">arr</span><span class="p">(</span><span class="n">j</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">temp</span> <span class="w"> </span><span class="k">End</span><span class="w"> </span><span class="k">If</span> <span class="w"> </span><span class="k">Next</span> <span class="w"> </span><span class="k">Next</span> <span class="w"> </span><span class="n">Selection_Sort</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">Join</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span><span class="s">","</span><span class="p">))</span> <span class="k">End</span><span class="w"> </span><span class="k">Function</span> <span class="n">WScript</span><span class="p">.</span><span class="n">StdOut</span><span class="p">.</span><span class="n">Write</span><span class="w"> </span><span class="s">"Pre-Sort"</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">vbTab</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="s">"Sorted"</span> <span class="n">WScript</span><span class="p">.</span><span class="n">StdOut</span><span class="p">.</span><span class="n">WriteLine</span> <span class="n">WScript</span><span class="p">.</span><span class="n">StdOut</span><span class="p">.</span><span class="n">Write</span><span class="w"> </span><span class="s">"3,2,5,4,1"</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">vbTab</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">Selection_Sort</span><span class="p">(</span><span class="s">"3,2,5,4,1"</span><span class="p">)</span> <span class="n">WScript</span><span class="p">.</span><span class="n">StdOut</span><span class="p">.</span><span class="n">WriteLine</span> <span class="n">WScript</span><span class="p">.</span><span class="n">StdOut</span><span class="p">.</span><span class="n">Write</span><span class="w"> </span><span class="s">"c,e,b,a,d"</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">vbTab</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">Selection_Sort</span><span class="p">(</span><span class="s">"c,e,b,a,d"</span><span class="p">)</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>Pre-Sort Sorted 3,2,5,4,1 1,2,3,4,5 c,e,b,a,d a,b,c,d,e </pre> <h2><span class="mw-headline" id="Wren"><a href="/wiki/Category:Wren" title="Category:Wren">Wren</a> </span></h2> <div class="examplemeta translation"><b>Translation of</b>: <a href="#Go">Go</a></div> <div class="mw-highlight mw-highlight-lang-wren mw-content-ltr" dir="ltr"><pre><span></span><span class="k">var</span><span class="w"> </span><span class="n">selectionSort</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nc">Fn</span><span class="o">.</span><span class="n">new</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">|</span><span class="n">a</span><span class="o">|</span> <span class="w"> </span><span class="k">var</span><span class="w"> </span><span class="n">last</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="o">.</span><span class="n">count</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mf">1</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="mf">0</span><span class="o">...</span><span class="n">last</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">var</span><span class="w"> </span><span class="n">aMin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="w"> </span><span class="k">var</span><span class="w"> </span><span class="n">iMin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span> <span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">i</span><span class="o">+</span><span class="mf">1</span><span class="o">..</span><span class="n">last</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">aMin</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="n">aMin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="w"> </span><span class="n">iMin</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">j</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="p">}</span> <span class="w"> </span><span class="k">var</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">aMin</span> <span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="n">iMin</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">t</span> <span class="w"> </span><span class="p">}</span> <span class="p">}</span> <span class="k">var</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="p">[</span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">65</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">31</span><span class="p">,</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">99</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">83</span><span class="p">,</span><span class="w"> </span><span class="mf">782</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">],</span><span class="w"> </span><span class="p">[</span><span class="mf">7</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">6</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">6</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">]</span><span class="w"> </span><span class="p">]</span> <span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">array</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nc">System</span><span class="o">.</span><span class="n">print</span><span class="p">(</span><span class="s">"Before: </span><span class="si">%(</span><span class="n">a</span><span class="si">)</span><span class="s">"</span><span class="p">)</span> <span class="w"> </span><span class="n">selectionSort</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="w"> </span><span class="nc">System</span><span class="o">.</span><span class="n">print</span><span class="p">(</span><span class="s">"After : </span><span class="si">%(</span><span class="n">a</span><span class="si">)</span><span class="s">"</span><span class="p">)</span> <span class="w"> </span><span class="nc">System</span><span class="o">.</span><span class="n">print</span><span class="p">()</span> <span class="p">}</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>Before: [4, 65, 2, -31, 0, 99, 2, 83, 782, 1] After : [-31, 0, 1, 2, 2, 4, 65, 83, 99, 782] Before: [7, 5, 2, 6, 1, 4, 2, 6, 3] After : [1, 2, 2, 3, 4, 5, 6, 6, 7] </pre> <p><br /> Alternatively we can just call a library method. </p> <div class="examplemeta libheader"><b>Library:</b> <a href="/wiki/Category:Wren-sort" title="Category:Wren-sort">Wren-sort</a></div> <div class="mw-highlight mw-highlight-lang-wren mw-content-ltr" dir="ltr"><pre><span></span><span class="k">import</span><span class="w"> </span><span class="s">"./sort"</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="nc">Sort</span> <span class="k">var</span><span class="w"> </span><span class="n">array</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="p">[</span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">65</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="o">-</span><span class="mf">31</span><span class="p">,</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span><span class="w"> </span><span class="mf">99</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">83</span><span class="p">,</span><span class="w"> </span><span class="mf">782</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">],</span><span class="w"> </span><span class="p">[</span><span class="mf">7</span><span class="p">,</span><span class="w"> </span><span class="mf">5</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">6</span><span class="p">,</span><span class="w"> </span><span class="mf">1</span><span class="p">,</span><span class="w"> </span><span class="mf">4</span><span class="p">,</span><span class="w"> </span><span class="mf">2</span><span class="p">,</span><span class="w"> </span><span class="mf">6</span><span class="p">,</span><span class="w"> </span><span class="mf">3</span><span class="p">]</span><span class="w"> </span><span class="p">]</span> <span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="ow">in</span><span class="w"> </span><span class="n">array</span><span class="p">)</span><span class="w"> </span><span class="p">{</span> <span class="w"> </span><span class="nc">System</span><span class="o">.</span><span class="n">print</span><span class="p">(</span><span class="s">"Before: </span><span class="si">%(</span><span class="n">a</span><span class="si">)</span><span class="s">"</span><span class="p">)</span> <span class="w"> </span><span class="nc">Sort</span><span class="o">.</span><span class="n">selection</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="w"> </span><span class="nc">System</span><span class="o">.</span><span class="n">print</span><span class="p">(</span><span class="s">"After : </span><span class="si">%(</span><span class="n">a</span><span class="si">)</span><span class="s">"</span><span class="p">)</span> <span class="w"> </span><span class="nc">System</span><span class="o">.</span><span class="n">print</span><span class="p">()</span> <span class="p">}</span> </pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>As above. </pre> <h2><span class="mw-headline" id="XPL0"><a href="/wiki/Category:XPL0" title="Category:XPL0">XPL0</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>include c:\cxpl\codes; \intrinsic 'code' declarations string 0; \use zero-terminated strings proc SelSort(A, N); \Selection sort char A; \address of array int N; \number of elements in array (size) int I, J, S, JS, T; [for I:= 0 to N-2 do [S:= (~0)>>1; for J:= I to N-1 do \find smallest element if A(J) < S then [S:= A(J); JS:= J]; T:= A(I); A(I):= A(JS); A(JS):= T; ]; ]; func StrLen(Str); \Return number of characters in an ASCIIZ string char Str; int I; for I:= 0 to -1>>1-1 do if Str(I) = 0 then return I; char Str; [Str:= "Pack my box with five dozen liquor jugs."; SelSort(Str, StrLen(Str)); Text(0, Str); CrLf(0); ]</pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre> .Pabcdeefghiiijklmnoooqrstuuvwxyz </pre> <h2><span class="mw-headline" id="zkl"><a href="/wiki/Category:Zkl" title="Category:Zkl">zkl</a> </span></h2> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>fcn selectionSort(list){ // sort a list of ints copy,r:=list.copy(),List(); while(copy){ min,idx:=(0).min(copy), copy.find(min); r.append(min); copy.del(idx); } r }</pre></div> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>selectionSort(List(28, 44, 46, 24, 19, -5, 2, 17, 11, 25, 4)).println();</pre></div> <div> <dl><dt>Output:</dt></dl></div> <pre>L(-5,2,4,11,17,19,24,25,28,44,46) </pre> <!-- NewPP limit report Parsed by mw164 Cached time: 20241125095502 Cache expiry: 1296000 Reduced expiry: false Complications: [show‐toc] [SMW] In‐text annotation parser time: 0.001 seconds CPU time usage: 0.938 seconds Real time usage: 1.650 seconds Preprocessor visited node count: 4318/1000000 Post‐expand include size: 20674/2097152 bytes Template argument size: 3914/2097152 bytes Highest expansion depth: 6/100 Expensive parser function count: 90/99 Unstrip recursion depth: 0/20 Unstrip post‐expand size: 428049/5000000 bytes --> <!-- Transclusion expansion time report (%,ms,calls,template) 100.00% 788.503 1 -total 10.78% 85.010 110 Template:Header 1.46% 11.513 1 Template:Task 0.80% 6.322 1 Template:Tcllib 0.68% 5.361 11 Template:Trans 0.46% 3.619 37 Template:Out 0.44% 3.438 1 Template:Omit_from 0.42% 3.282 1 Template:Libheader 0.38% 3.033 7 Template:Works_with 0.30% 2.388 1 Template:Sorting_Algorithm --> <!-- Saved in parser cache with key rosettacodewiki:pcache:idhash:1473-0!canonical and timestamp 20241125095502 and revision id 349291. Rendering was triggered because: diff-page --> </div><noscript><img src="https://login.miraheze.org/wiki/Special:CentralAutoLogin/start?type=1x1" alt="" width="1" height="1" style="border: none; position: absolute;"></noscript> <div class="printfooter" data-nosnippet="">Retrieved from "<a dir="ltr" href="https://rosettacode.org/wiki/Sorting_algorithms/Selection_sort?oldid=349291">https://rosettacode.org/wiki/Sorting_algorithms/Selection_sort?oldid=349291</a>"</div></div> <div id="catlinks" class="catlinks" data-mw="interface"><div id="mw-normal-catlinks" class="mw-normal-catlinks"><a href="/wiki/Special:Categories" title="Special:Categories">Categories</a>: <ul><li><a href="/wiki/Category:Programming_Tasks" title="Category:Programming Tasks">Programming Tasks</a></li><li><a href="/wiki/Category:Sorting_Algorithms" title="Category:Sorting Algorithms">Sorting Algorithms</a></li><li><a href="/wiki/Category:Sorting" title="Category:Sorting">Sorting</a></li><li><a href="/wiki/Category:11l" title="Category:11l">11l</a></li><li><a href="/wiki/Category:360_Assembly" title="Category:360 Assembly">360 Assembly</a></li><li><a href="/wiki/Category:AArch64_Assembly" title="Category:AArch64 Assembly">AArch64 Assembly</a></li><li><a href="/wiki/Category:Action!" title="Category:Action!">Action!</a></li><li><a href="/wiki/Category:ActionScript" title="Category:ActionScript">ActionScript</a></li><li><a href="/wiki/Category:Ada" title="Category:Ada">Ada</a></li><li><a href="/wiki/Category:ALGOL_68" title="Category:ALGOL 68">ALGOL 68</a></li><li><a href="/wiki/Category:AppleScript" title="Category:AppleScript">AppleScript</a></li><li><a href="/wiki/Category:ARM_Assembly" title="Category:ARM Assembly">ARM Assembly</a></li><li><a href="/wiki/Category:Arturo" title="Category:Arturo">Arturo</a></li><li><a href="/wiki/Category:AutoHotkey" title="Category:AutoHotkey">AutoHotkey</a></li><li><a href="/wiki/Category:AWK" title="Category:AWK">AWK</a></li><li><a href="/wiki/Category:BBC_BASIC" title="Category:BBC BASIC">BBC BASIC</a></li><li><a href="/wiki/Category:BASIC" title="Category:BASIC">BASIC</a></li><li><a href="/wiki/Category:GWBASIC?action=edit&redlink=1" class="new" title="Category:GWBASIC (page does not exist)">GWBASIC</a></li><li><a href="/wiki/Category:IS-BASIC" title="Category:IS-BASIC">IS-BASIC</a></li><li><a href="/wiki/Category:BCPL" title="Category:BCPL">BCPL</a></li><li><a href="/wiki/Category:C" title="Category:C">C</a></li><li><a href="/wiki/Category:C_sharp" title="Category:C sharp">C sharp</a></li><li><a href="/wiki/Category:C%2B%2B" title="Category:C++">C++</a></li><li><a href="/wiki/Category:Clojure" title="Category:Clojure">Clojure</a></li><li><a href="/wiki/Category:COBOL" title="Category:COBOL">COBOL</a></li><li><a href="/wiki/Category:Common_Lisp" title="Category:Common Lisp">Common Lisp</a></li><li><a href="/wiki/Category:Crystal" title="Category:Crystal">Crystal</a></li><li><a href="/wiki/Category:D" title="Category:D">D</a></li><li><a href="/wiki/Category:Dart" title="Category:Dart">Dart</a></li><li><a href="/wiki/Category:Delphi" title="Category:Delphi">Delphi</a></li><li><a href="/wiki/Category:E" title="Category:E">E</a></li><li><a href="/wiki/Category:EasyLang" title="Category:EasyLang">EasyLang</a></li><li><a href="/wiki/Category:EchoLisp" title="Category:EchoLisp">EchoLisp</a></li><li><a href="/wiki/Category:Eiffel" title="Category:Eiffel">Eiffel</a></li><li><a href="/wiki/Category:Elena" title="Category:Elena">Elena</a></li><li><a href="/wiki/Category:Elixir" title="Category:Elixir">Elixir</a></li><li><a href="/wiki/Category:Erlang" title="Category:Erlang">Erlang</a></li><li><a href="/wiki/Category:Euphoria" title="Category:Euphoria">Euphoria</a></li><li><a href="/wiki/Category:F_Sharp" title="Category:F Sharp">F Sharp</a></li><li><a href="/wiki/Category:Factor" title="Category:Factor">Factor</a></li><li><a href="/wiki/Category:Forth" title="Category:Forth">Forth</a></li><li><a href="/wiki/Category:Fortran" title="Category:Fortran">Fortran</a></li><li><a href="/wiki/Category:FreeBASIC" title="Category:FreeBASIC">FreeBASIC</a></li><li><a href="/wiki/Category:Gambas" title="Category:Gambas">Gambas</a></li><li><a href="/wiki/Category:GAP" title="Category:GAP">GAP</a></li><li><a href="/wiki/Category:Go" title="Category:Go">Go</a></li><li><a href="/wiki/Category:Haskell" title="Category:Haskell">Haskell</a></li><li><a href="/wiki/Category:Haxe" title="Category:Haxe">Haxe</a></li><li><a href="/wiki/Category:Icon" title="Category:Icon">Icon</a></li><li><a href="/wiki/Category:Unicon" title="Category:Unicon">Unicon</a></li><li><a href="/wiki/Category:Io" title="Category:Io">Io</a></li><li><a href="/wiki/Category:J" title="Category:J">J</a></li><li><a href="/wiki/Category:Java" title="Category:Java">Java</a></li><li><a href="/wiki/Category:JavaScript" title="Category:JavaScript">JavaScript</a></li><li><a href="/wiki/Category:Jq" title="Category:Jq">Jq</a></li><li><a href="/wiki/Category:Julia" title="Category:Julia">Julia</a></li><li><a href="/wiki/Category:Kotlin" title="Category:Kotlin">Kotlin</a></li><li><a href="/wiki/Category:Liberty_BASIC" title="Category:Liberty BASIC">Liberty BASIC</a></li><li><a href="/wiki/Category:LSE" title="Category:LSE">LSE</a></li><li><a href="/wiki/Category:Lua" title="Category:Lua">Lua</a></li><li><a href="/wiki/Category:Maple" title="Category:Maple">Maple</a></li><li><a href="/wiki/Category:Mathematica" title="Category:Mathematica">Mathematica</a></li><li><a href="/wiki/Category:Wolfram_Language" class="mw-redirect" title="Category:Wolfram Language">Wolfram Language</a></li><li><a href="/wiki/Category:MATLAB" title="Category:MATLAB">MATLAB</a></li><li><a href="/wiki/Category:Octave" title="Category:Octave">Octave</a></li><li><a href="/wiki/Category:Maxima" title="Category:Maxima">Maxima</a></li><li><a href="/wiki/Category:MAXScript" title="Category:MAXScript">MAXScript</a></li><li><a href="/wiki/Category:N/t/roff" title="Category:N/t/roff">N/t/roff</a></li><li><a href="/wiki/Category:Nanoquery" title="Category:Nanoquery">Nanoquery</a></li><li><a href="/wiki/Category:Nemerle" title="Category:Nemerle">Nemerle</a></li><li><a href="/wiki/Category:NetRexx" title="Category:NetRexx">NetRexx</a></li><li><a href="/wiki/Category:Nim" title="Category:Nim">Nim</a></li><li><a href="/wiki/Category:OCaml" title="Category:OCaml">OCaml</a></li><li><a href="/wiki/Category:Oforth" title="Category:Oforth">Oforth</a></li><li><a href="/wiki/Category:OoRexx" title="Category:OoRexx">OoRexx</a></li><li><a href="/wiki/Category:Oz" title="Category:Oz">Oz</a></li><li><a href="/wiki/Category:PARI/GP" title="Category:PARI/GP">PARI/GP</a></li><li><a href="/wiki/Category:Pascal" title="Category:Pascal">Pascal</a></li><li><a href="/wiki/Category:Perl" title="Category:Perl">Perl</a></li><li><a href="/wiki/Category:Phix" title="Category:Phix">Phix</a></li><li><a href="/wiki/Category:PHP" title="Category:PHP">PHP</a></li><li><a href="/wiki/Category:PicoLisp" title="Category:PicoLisp">PicoLisp</a></li><li><a href="/wiki/Category:PL/I" title="Category:PL/I">PL/I</a></li><li><a href="/wiki/Category:PowerShell" title="Category:PowerShell">PowerShell</a></li><li><a href="/wiki/Category:Prolog" title="Category:Prolog">Prolog</a></li><li><a href="/wiki/Category:PureBasic" title="Category:PureBasic">PureBasic</a></li><li><a href="/wiki/Category:Python" title="Category:Python">Python</a></li><li><a href="/wiki/Category:Qi" title="Category:Qi">Qi</a></li><li><a href="/wiki/Category:Quackery" title="Category:Quackery">Quackery</a></li><li><a href="/wiki/Category:R" title="Category:R">R</a></li><li><a href="/wiki/Category:Ra" title="Category:Ra">Ra</a></li><li><a href="/wiki/Category:Racket" title="Category:Racket">Racket</a></li><li><a href="/wiki/Category:Raku" title="Category:Raku">Raku</a></li><li><a href="/wiki/Category:REXX" title="Category:REXX">REXX</a></li><li><a href="/wiki/Category:Ring" title="Category:Ring">Ring</a></li><li><a href="/wiki/Category:Ruby" title="Category:Ruby">Ruby</a></li><li><a href="/wiki/Category:Run_BASIC" title="Category:Run BASIC">Run BASIC</a></li><li><a href="/wiki/Category:Rust" title="Category:Rust">Rust</a></li><li><a href="/wiki/Category:Scala" title="Category:Scala">Scala</a></li><li><a href="/wiki/Category:Seed7" title="Category:Seed7">Seed7</a></li><li><a href="/wiki/Category:Sidef" title="Category:Sidef">Sidef</a></li><li><a href="/wiki/Category:Standard_ML" title="Category:Standard ML">Standard ML</a></li><li><a href="/wiki/Category:Stata" title="Category:Stata">Stata</a></li><li><a href="/wiki/Category:Swift" title="Category:Swift">Swift</a></li><li><a href="/wiki/Category:Tcl" title="Category:Tcl">Tcl</a></li><li><a href="/wiki/Category:Tcllib" title="Category:Tcllib">Tcllib</a></li><li><a href="/wiki/Category:TI-83_BASIC" title="Category:TI-83 BASIC">TI-83 BASIC</a></li><li><a href="/wiki/Category:UBasic/4tH" title="Category:UBasic/4tH">UBasic/4tH</a></li><li><a href="/wiki/Category:Ursala" title="Category:Ursala">Ursala</a></li><li><a href="/wiki/Category:VBA" title="Category:VBA">VBA</a></li><li><a href="/wiki/Category:VBScript" title="Category:VBScript">VBScript</a></li><li><a href="/wiki/Category:Wren" title="Category:Wren">Wren</a></li><li><a href="/wiki/Category:Wren-sort" title="Category:Wren-sort">Wren-sort</a></li><li><a href="/wiki/Category:XPL0" title="Category:XPL0">XPL0</a></li><li><a href="/wiki/Category:Zkl" title="Category:Zkl">Zkl</a></li><li><a href="/wiki/Category:GUISS/Omit" title="Category:GUISS/Omit">GUISS/Omit</a></li></ul></div><div id="mw-hidden-catlinks" class="mw-hidden-catlinks mw-hidden-cats-hidden">Hidden category: <ul><li><a href="/wiki/Category:Pages_with_syntax_highlighting_errors" title="Category:Pages with syntax highlighting errors">Pages with syntax highlighting errors</a></li></ul></div></div> </div> </main> <div id='mw-data-after-content'> <div class="mw-cookiewarning-container"><div class="mw-cookiewarning-text"><span>Cookies help us deliver our services. By using our services, you agree to our use of cookies.</span></div><form method="POST"><div class='oo-ui-layout oo-ui-horizontalLayout'><span class='oo-ui-widget oo-ui-widget-enabled oo-ui-buttonElement oo-ui-buttonElement-framed oo-ui-labelElement oo-ui-flaggedElement-progressive oo-ui-buttonWidget'><a role='button' tabindex='0' href='https://meta.miraheze.org/wiki/Special:MyLanguage/Privacy_Policy#2._Cookies' rel='nofollow' class='oo-ui-buttonElement-button'><span class='oo-ui-iconElement-icon oo-ui-iconElement-noIcon oo-ui-image-progressive'></span><span class='oo-ui-labelElement-label'>More information</span><span class='oo-ui-indicatorElement-indicator oo-ui-indicatorElement-noIndicator oo-ui-image-progressive'></span></a></span><span class='oo-ui-widget oo-ui-widget-enabled oo-ui-inputWidget oo-ui-buttonElement oo-ui-buttonElement-framed oo-ui-labelElement oo-ui-flaggedElement-primary oo-ui-flaggedElement-progressive oo-ui-buttonInputWidget'><button type='submit' tabindex='0' name='disablecookiewarning' value='OK' class='oo-ui-inputWidget-input oo-ui-buttonElement-button'><span class='oo-ui-iconElement-icon oo-ui-iconElement-noIcon oo-ui-image-invert'></span><span class='oo-ui-labelElement-label'>OK</span><span class='oo-ui-indicatorElement-indicator oo-ui-indicatorElement-noIndicator oo-ui-image-invert'></span></button></span></div></form></div> </div> </div> <div class="mw-footer-container"> <footer id="footer" class="mw-footer" > <ul id="footer-info"> <li id="footer-info-lastmod"> This page was last edited on 25 November 2024, at 09:55.</li> <li id="footer-info-copyright">Content is available under GNU Free Document License 1.3 unless otherwise noted.</li> </ul> <ul id="footer-places"> <li id="footer-places-privacy"><a href="https://meta.miraheze.org/wiki/Special:MyLanguage/Privacy_Policy">Privacy policy</a></li> <li id="footer-places-about"><a href="/wiki/Rosetta_Code:About">About Rosetta Code</a></li> <li id="footer-places-disclaimers"><a href="/wiki/Rosetta_Code:General_disclaimer">Disclaimers</a></li> <li id="footer-places-termsofservice"><a href="https://meta.miraheze.org/wiki/Special:MyLanguage/Terms_of_Use">Terms of Use</a></li> <li id="footer-places-donate"><a href="https://meta.miraheze.org/wiki/Special:MyLanguage/Donate">Donate to Miraheze</a></li> <li id="footer-places-mobileview"><a href="https://rosettacode.org/wiki/Sorting_algorithms/Selection_sort?mobileaction=toggle_view_mobile" class="noprint stopMobileRedirectToggle">Mobile view</a></li> </ul> <ul id="footer-icons" class="noprint"> <li id="footer-poweredbyico"><a href="https://meta.miraheze.org/wiki/Special:MyLanguage/Miraheze_Meta" class="cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled"><img src="https://static.wikitide.net/commonswiki/f/ff/Powered_by_Miraheze.svg" alt="Hosted by Miraheze" width="88" height="31" loading="lazy"></a><a href="https://www.mediawiki.org/" class="cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled"><img src="/1.43/resources/assets/poweredby_mediawiki.svg" alt="Powered by MediaWiki" width="88" height="31" loading="lazy"></a><a href="https://www.semantic-mediawiki.org/wiki/Semantic_MediaWiki" class="cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled"><img src="/1.43/extensions/SemanticMediaWiki/res/smw/assets/logo_footer.svg" alt="Powered by Semantic MediaWiki" class="smw-footer" width="88" height="31" loading="lazy"></a></li> <li id="footer-copyrightico"><a href="https://www.gnu.org/licenses/fdl-1.3.en.html" class="cdx-button cdx-button--fake-button cdx-button--size-large cdx-button--fake-button--enabled"><img src="https://www.gnu.org/graphics/gfdl-logo-tiny.png" alt="GNU Free Document License 1.3" width="88" height="31" loading="lazy"></a></li> </ul> </footer> </div> </div> </div> <div class="vector-settings" id="p-dock-bottom"> <ul></ul> </div><script>(RLQ=window.RLQ||[]).push(function(){mw.config.set({"wgHostname":"mw182","wgBackendResponseTime":231,"wgPageParseReport":{"smw":{"limitreport-intext-parsertime":0.001},"limitreport":{"cputime":"0.938","walltime":"1.650","ppvisitednodes":{"value":4318,"limit":1000000},"postexpandincludesize":{"value":20674,"limit":2097152},"templateargumentsize":{"value":3914,"limit":2097152},"expansiondepth":{"value":6,"limit":100},"expensivefunctioncount":{"value":90,"limit":99},"unstrip-depth":{"value":0,"limit":20},"unstrip-size":{"value":428049,"limit":5000000},"timingprofile":["100.00% 788.503 1 -total"," 10.78% 85.010 110 Template:Header"," 1.46% 11.513 1 Template:Task"," 0.80% 6.322 1 Template:Tcllib"," 0.68% 5.361 11 Template:Trans"," 0.46% 3.619 37 Template:Out"," 0.44% 3.438 1 Template:Omit_from"," 0.42% 3.282 1 Template:Libheader"," 0.38% 3.033 7 Template:Works_with"," 0.30% 2.388 1 Template:Sorting_Algorithm"]},"cachereport":{"origin":"mw164","timestamp":"20241125095502","ttl":1296000,"transientcontent":false}}});});</script> <script> var _paq = window._paq = window._paq || []; if ( 1 ) { _paq.push(['disableCookies']); } if ( 0 ) { _paq.push(['setRequestMethod', 'GET']); } _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u = "https://analytics.wikitide.net/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setDocumentTitle', "rosettacodewiki" + " - " + "Sorting algorithms/Selection sort"]); _paq.push(['setSiteId', 6510]); _paq.push(['setCustomVariable', 1, 'userType', "Anonymous", "visit"]); if ( 1 ) { _paq.push(['addTracker', u + 'matomo.php', 1]); } var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script> <noscript><p><img src="https://analytics.wikitide.net/matomo.php?idsite=6510&rec=1&action_name=Sorting_algorithms/Selection_sort" style="border:0;" alt="" /></p></noscript> </body> </html>