Selection sort is a simple sorting algorithm with asymptotic complexity O(n^{2}). In comparison with other quadratic sorting algorithms it almost always outperforms bubble sort, but it is usually slower than insertion sort. The advantage of selection sort over algorithms with O(n \\cdot \\log{n}) (quicksort, heapsort, merge sort) asymptotic complexity is it's constant memory complexity.

Description

The idea of selection sort is, that if we sort the array from largest to smallest element, than the first element of the sorted array will be the one with the largest value. Second will be the largest element of the rest of the array. Third will be the largest element of the new rest of the array (initial array without the two already sorted elements)...

So we can iteratively select the largest element of the (reduced) array, swap it with the first element and than reduce the problem size by 1 (sort only the rest of the array). When there remains only one element to sort, the algorithm terminates.

Visualization

Example

(3 2 8 7 6) // initial array, sort the elements in descending order
(3 2 8 7 6) // 8 is the largest value, swap it with 3 at index 0
(8 2 3 7 6) // 7 is the largest value, swap it with 2 at index 1
(8 7 3 2 6) // 6 is the largest value, swap it with 3 at index 2
(8 7 6 2 3) // 3 is the largest value, swap it with 2 at index 3
(8 7 6 3 2) // sorted
Selection sort (Licence: Public domain)
Selection sort - visualization

Code

      function selectionSort(array a)
          for i in 0 -> a.length - 2 do
              maxIndex = i
              for j in (i + 1) -> (a.length - 1) do
                  if a[j] > a[maxIndex]
                      maxIndex = j
              swap(a[i], a[maxIndex])
  
     /**
      * Selection sort (descending order)
      * @param array array to be sorted
      */
      public static void selectionSort(int[] array) {
          for (int i = 0; i < array.length - 1; i++) {
              int maxIndex = i;
              for (int j = i + 1; j < array.length; j++) {
                  if (array[j] > array[maxIndex]) maxIndex = j;
              }
              int tmp = array[i];
              array[i] = array[maxIndex];
              array[maxIndex] = tmp;
          } 
      }
  
  /**
   * Selection sort (descending order)
   * @param array array to be sorted
   * @param size size of the array
   */
  void selectionSort(int array[], int size) {
       for (int i = 0; i < size - 1; i++) {
           int maxIndex = i;
           for (int j = i + 1; j < size; j++) {
               if (array[j] > array[maxIndex]) maxIndex = j;
           }
           int tmp = array[i];
           array[i] = array[maxIndex];
           array[maxIndex] = tmp;
       } 
   }
  
         /**
          * Selection sort (descending order)
          * @param array array to be sorted
          */
          public static void SelectionSort(int[] array)
          {
              for (int i = 0; i < array.Length - 1; i++)
              {
                  int maxIndex = i;
                  for (int j = i + 1; j < array.Length; j++)
                  {
                      if (array[j] > array[maxIndex]) maxIndex = j;
                  }
                  int tmp = array[i];
                  array[i] = array[maxIndex];
                  array[maxIndex] = tmp;
              }
          }
  
    procedure SelectSort(var X : ArrayType; N : integer);
    var
      I,
      J,
      K,
      Y : integer;
    begin
      for I := 1 to N - 1 do
        begin
          K := I;
          Y := X[I];
          for J := (I + 1) to N do
            if (X[J] < Y) then
              begin
                K := J;
                Y := X[J]
              end;
          X[K] := X[J];
          X[I] := Y;
        end
    end;
  

SEO od společnosti Digital Pylon


Online casino s algoritmem

České casino online online slot-vegas.cz

Hrajte nejlepší hry jako je GoodGame Empire.





Zajímavé články: Jak najít práci snů? Zvolte kariéru v IT!, Češi mají rádi hrací automaty online, Jak funguje algoritmické obchodování Casino, Online výuka Algoritmus a online marketing mají svá pravidla, Automaty, Matematický vliv, Ratings, Jak fungují algoritmy hazardních her online: více znalostí, více peněz, SYPWAI - nástroj pro vědecký vývoj, Vynikají na globálním trhu: Nejlepší vývojáři softwaru pro online výherní automaty, Jak si vybrat nejlepší české online casino, Proč byste měli hrát online casino VPN revoluce, Kde najdeme algoritmy v každodenním životě?, Čeká vás pracovní pohovor mimo město? Podívejte se, jak dokonale zvládnout včasný příchod, 5 úžasných technologií ze světa hazardních her, Mirror and access to Mostbet, Svou kancelář můžete mít stále po ruce, Jaké výhody má digitalizovaná firma oproti off-line konkurenci?, Jaký systém vybrat pro snadné řízení výroby?, Nahradí umělá inteligence ajťáky?, Důvody, proč používat SnapTik ke stahování videí TikTok, Dokonalý den na pláži: Co si vzít s sebou, aby byl výlet zábavný a bezpečný?, Jak přežít dlouhý let?, Go pay GoodGame Empire, Blockchain, Rozhovor, Umělá inteligence, Ochranná známka pre softvér: Prečo ju registrovať?, Role kryptografických algoritmů v zabezpečení online kasin, Jaké jsou náklady na nákup 3D tiskárny?, Jak algoritmy vylepšují online zážitky v roce 2025, Epilace laserem a péče o pokožku před a po ní, Byty k pronájmu Sokolov - výhody a rizika pronájmu bytu bez realitky, Filmy a seriály plné hádanek: kryptografie jako hlavní téma


Doporučujeme

Internet pro vaši firmu na míru

https://www.algoritmy.net