Algorithm implementation/Sorting/Selection sort

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search


Contents

[edit] C/C++

 void selection(int *array, int length)
 {
   int max, i, temp;
   while(length > 0)
   {
     max = 0;
     for(i = 1; i < length; i++)
       if(array[i] > array[max])
         max = i;
     temp = array[length-1];
     array[length-1] = array[max];
     array[max] = temp;
     length--;
   }
 }

[edit] C++

#include <algorithm> // for: std::iter_swap, std::min_element
 
template <typename Iterator>
void selection_sort(Iterator begin, Iterator end)
{
    Iterator min;
    while (begin != end)
    {
        min = std::min_element(begin, end);
        std::iter_swap(begin, min);
        ++begin;
    }
}

[edit] Java

public static int[] selectionsort(int[] numbers){
    for (int i = 0; i < numbers.length; i++)
    {
        int index = i;
        for (int j = i+1; j < numbers.length; j++)
            if (numbers[j] < numbers[index]) //Finds smallest number
                index = j;  
 
        int smallerNumber = numbers[index];  //Swap
        numbers[index] = numbers[i];
        numbers[i] = smallerNumber;
 
    }
    return numbers;
}

[edit] JavaScript

function selectionSort (sortMe) {
   for (i = 0; i < sortMe.length; i++) {
      tmp = i;
      for (j = i+1; j < sortMe.length; j++) {
         if (sortMe[j] < sortMe[tmp]) {
            tmp = j;
         }
      }
      tmp2 = sortMe[tmp];
      sortMe[tmp] = sortMe[i];
      sortMe[i] = tmp2;
   }
}

[edit] ML

(* Selection sort is modified slightly for use on linked lists (which are the only inbuilt array-like structures in ML) 
      in a functional programming language *)
fun selection_sort [] = []
|   selection_sort (first::lst) =
    let
      fun select_r small ([], output) = small::(selection_sort output)
      |   select_r small (x::xs, output) = 
            if (x< small) then 
              select_r x (xs, small::output)
            else
              select_r small (xs, x::output)
    in
      select_r first (lst, [])
    end
;

[edit] PHP

// selection sort function module in PHP
function selectionSort(&$a) {
   $n = count($a);
   for($i = 0; $i < count($a); $i++) {
      $min = $i;
      for($j = $i + 1; $j < $n; $j++)
         if($a[$j] < $a[$min])
            $min = $j;
      $temp = $a[$min]; $a[$min] = $a[$i]; $a[$i] = $temp;
   }
}

[edit] Python

  def selection_sort(list):
    l=list[:]                  # create a copy of the list
    sorted=[]                  # this new list will hold the results
    while len(l):              # while there are elements to sort... 
        lowest=l[0]            # create a variable to identify lowest
        for x in l:            # and check every item in the list...
            if x<lowest:       # to see if it might be lower.
                lowest=x
        sorted.append(lowest)  # add the lowest one to the new list
        l.remove(lowest)       # and delete it from the old one
    return sorted

[edit] Ruby

 
  def sort!(keys)
    for i in 0...keys.size-1
      min = i
      for j in i+1...keys.size
        min = j if keys[j] < keys[min]
        end
      keys[i], keys[min] = keys[min], keys[i]
      end
    keys
  end
Personal tools
Create a book
In other languages