Sorting algorithms/Selection sort: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Links, formatting)
(Ada solution added)
Line 5: Line 5:
Its asymptotic complexity is [[O]](n<sup>2</sup>) making it inefficient on large arrays.
Its asymptotic complexity is [[O]](n<sup>2</sup>) making it inefficient on large arrays.


=={{header|Ada}}==
<ada>
with Ada.Text_IO; use Ada.Text_IO;

procedure Test_Selection_Sort is

type Integer_Array is array (Positive range <>) of Integer;
procedure Sort (A : in out Integer_Array) is
Min : Positive;
Temp : Integer;
begin
for I in A'First..A'Last - 1 loop
Min := I;
for J in I + 1..A'Last loop
if A (Min) > A (J) then
Min := J;
end if;
end loop;
if Min /= I then
Temp := A (I);
A (I) := A (Min);
A (Min) := Temp;
end if;
end loop;
end Sort;

A : Integer_Array := (4, 9, 3, -2, 0, 7, -5, 1, 6, 8);
begin
Sort (A);
for I in A'Range loop
Put (Integer'Image (A (I)) & " ");
end loop;
end Test_Selection_Sort;
</ada>
Sample output:
<pre>
-5 -2 0 1 3 4 6 7 8 9
</pre>
=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
{{works with|Fortran|95 and later}}

Revision as of 20:47, 29 November 2008

Task
Sorting algorithms/Selection sort
You are encouraged to solve this task according to the task description, using any language you may know.

In this task, the goal is to 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 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. Its asymptotic complexity is O(n2) making it inefficient on large arrays.

Ada

<ada> with Ada.Text_IO; use Ada.Text_IO;

procedure Test_Selection_Sort is

  type Integer_Array is array (Positive range <>) of Integer;
  procedure Sort (A : in out Integer_Array) is
     Min  : Positive;
     Temp : Integer;
  begin
     for I in A'First..A'Last - 1 loop
        Min := I;
        for J in I + 1..A'Last loop
           if A (Min) > A (J) then
              Min := J;
           end if;
        end loop;
        if Min /= I then
           Temp    := A (I);
           A (I)   := A (Min);
           A (Min) := Temp;
        end if;
     end loop;
  end Sort;
  A : Integer_Array := (4, 9, 3, -2, 0, 7, -5, 1, 6, 8);

begin

  Sort (A);
  for I in A'Range loop
     Put (Integer'Image (A (I)) & " ");
  end loop;

end Test_Selection_Sort; </ada> Sample output:

-5 -2  0  1  3  4  6  7  8  9

Fortran

Works with: Fortran version 95 and later
PROGRAM SELECTION

  IMPLICIT NONE

  INTEGER :: intArray(10) = (/ 4, 9, 3, -2, 0, 7, -5, 1, 6, 8 /)
 
  WRITE(*,"(A,10I5)") "Unsorted array:", intArray
  CALL Selection_sort(intArray)
  WRITE(*,"(A,10I5)") "Sorted array  :", intArray
   
CONTAINS

  SUBROUTINE Selection_sort(a)
    INTEGER, INTENT(IN OUT) :: a(:)
    INTEGER :: i, minIndex, temp

    DO i = 1, SIZE(a)-1
       minIndex = MINLOC(a(i:), 1) + i - 1
       IF (a(i) > a(minIndex)) THEN
          temp = a(i)
          a(i) = a(minIndex)
          a(minIndex) = temp
       END IF
    END DO
  END SUBROUTINE Selection_sort

END PROGRAM SELECTION

Output

Unsorted array:    4    9    3   -2    0    7   -5    1    6    8
Sorted array  :   -5   -2    0    1    3    4    6    7    8    9

Java

This algorithm sorts in place. The call sort(array) will rearrange the array and not create a new one. <java>public static void sort(int[] nums){ for(int currentPlace = 0;currentPlace<nums.length-1;currentPlace++){ int smallest = Integer.MAX_VALUE; int smallestAt = currentPlace+1; for(int check = currentPlace+1; check<nums.length;check++){ if(nums[check]<smallest){ smallestAt = check; smallest = nums[check]; } } int temp = nums[currentPlace]; nums[currentPlace] = nums[smallestAt]; nums[smallestAt] = temp; } }</java>