Sort an integer array: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎[[Java]]: Use Java header instead)
m (Switch to header template)
Line 3: Line 3:
Sort an array (or list) of integers in ascending numerical order. Use a sorting facility provided by the language/library if possible.
Sort an array (or list) of integers in ascending numerical order. Use a sorting facility provided by the language/library if possible.


==[[4D]]==
=={{header|4D}}==
[[Category:4D]]

===English===
===English===


Line 28: Line 26:
TRIER TABLEAU($nombres;<) ` pour effectuer un tri par ordre décroissant
TRIER TABLEAU($nombres;<) ` pour effectuer un tri par ordre décroissant


==[[Ada]]==
=={{header|Ada}}==
[[Category:Ada]]
'''Compiler:''' [[GNAT]] GPL 2006
'''Compiler:''' [[GNAT]] GPL 2006
with Gnat.Heap_Sort_G;
with Gnat.Heap_Sort_G;
Line 56: Line 53:
Heap_Sort.Sort(8);
Heap_Sort.Sort(8);
end Integer_Sort;
end Integer_Sort;
==[[C]]==
=={{header|C}}==
[[Category:C]]
'''Compiler:''' [[GCC]] 4.0.1
'''Compiler:''' [[GCC]] 4.0.1
#include <stdlib.h>
#include <stdlib.h>
Line 114: Line 110:
}
}


==[[Clean]]==
=={{header|Clean}}==
[[Category:Clean]]
We use list and array comprehensions to convert an array to and from a list in order to use the built-in <tt>sort</tt> on lists.
We use list and array comprehensions to convert an array to and from a list in order to use the built-in <tt>sort</tt> on lists.
import StdEnv
import StdEnv
Line 125: Line 120:
Start = sortArray {2, 4, 3, 1, 2}
Start = sortArray {2, 4, 3, 1, 2}


==[[Common Lisp]]==
=={{header|Common Lisp}}==
[[Category:Common Lisp]]

In Common Lisp, the ''sort'' function takes a predicate that is used as the comparator. This parameter can be any two-argument function. To sort a sequence (list or array) of integers, call ''sort'' with the < operator as the predicate:
In Common Lisp, the ''sort'' function takes a predicate that is used as the comparator. This parameter can be any two-argument function. To sort a sequence (list or array) of integers, call ''sort'' with the < operator as the predicate:


Line 134: Line 127:
#(-2 0 1 1 2 2 8 9)
#(-2 0 1 1 2 2 8 9)


==[[E]]==
=={{header|E}}==
[[Category:E]]

[2,4,3,1,2].sort()
[2,4,3,1,2].sort()


==[[Erlang]]==
=={{header|Erlang}}==
[[Category:Erlang]]

List = [2, 4, 3, 1, 2].
List = [2, 4, 3, 1, 2].
SortedList = lists:sort(List).
SortedList = lists:sort(List).


==[[Forth]]==
=={{header|Forth}}==
[[Category:Forth]]
'''Interpreter:'''[[Win32Forth]] 4.2
'''Interpreter:'''[[Win32Forth]] 4.2
create test-data 2 , 4 , 3 , 1 , 2 ,
create test-data 2 , 4 , 3 , 1 , 2 ,
test-data 5 cell-sort
test-data 5 cell-sort


==[[Haskell]]==
=={{header|Haskell}}==
[[Category:Haskell]]
'''Interpreter:''' [[GHC|GHCi]] 6.6
'''Interpreter:''' [[GHC|GHCi]] 6.6
Line 158: Line 145:
sorted = List.sort nums
sorted = List.sort nums


==[[IDL]]==
=={{header|IDL}}==
[[Category:IDL]]

result = array[sort(array)]
result = array[sort(array)]


Line 174: Line 159:
}
}


==[[JavaScript]]==
=={{header|JavaScript}}==
[[Category:JavaScript]]

'''Interpreter:''' Firefox 2.0
'''Interpreter:''' Firefox 2.0


Line 188: Line 171:
alert( numbers );
alert( numbers );


==[[MAXScript]]==
=={{header|MAXScript}}==
[[Category:MAXScript]]
arr = #(5, 4, 3, 2, 1)
arr = #(5, 4, 3, 2, 1)
arr = sort arr
arr = sort arr


==[[Objective-C]]==
=={{header|Objective-C}}==
[[Category:Objective-C]]
'''Compiler:''' [[GCC]] 4.0.1 (apple)
'''Compiler:''' [[GCC]] 4.0.1 (apple)
- (void)example
- (void)example
Line 209: Line 190:
}
}


==[[Perl]]==
=={{header|Perl}}==
[[Category:Perl]]
'''Interpreter:''' [[perl]] 5.8.6
'''Interpreter:''' [[perl]] 5.8.6
@nums = (2,4,3,1,2);
@nums = (2,4,3,1,2);
@sorted = sort {$a <=> $b} @nums;
@sorted = sort {$a <=> $b} @nums;


==[[PHP]]==
=={{header|PHP}}==
[[Category:PHP]]
'''Interpreter:''' [[PHP]] 4.4.4 CLI
'''Interpreter:''' [[PHP]] 4.4.4 CLI
<?php
<?php
Line 223: Line 202:
?>
?>


==[[Pop11]]==
=={{header|Pop11}}==
[[Category:Pop11]]

Pop11 library function sorts lists. So we first convert array to
Pop11 library function sorts lists. So we first convert array to
list, then sort and finally convert back:
list, then sort and finally convert back:
Line 253: Line 230:
ar.datalist.sort.destlist.consvector -> ar;
ar.datalist.sort.destlist.consvector -> ar;


==[[Python]]==
=={{header|Python}}==
[[Category:Python]]
'''Interpreter:''' [[Python]] 2.3
'''Interpreter:''' [[Python]] 2.3
nums = [2,4,3,1,2]
nums = [2,4,3,1,2]
Line 267: Line 243:
nums = sorted([2,4,3,1,2])
nums = sorted([2,4,3,1,2])


==[[Raven]]==
=={{header|Raven}}==
[[Category:Raven]]

Sort list in place:
Sort list in place:


[ 2 4 3 1 2 ] sort
[ 2 4 3 1 2 ] sort


==[[Ruby]]==
=={{header|Ruby}}==
[[Category:Ruby]]
'''Interpreter:''' [[ruby]] 1.8.4
'''Interpreter:''' [[ruby]] 1.8.4
nums = [2,4,3,1,2]
nums = [2,4,3,1,2]
sorted = nums.sort
sorted = nums.sort


==[[Seed7]]==
=={{header|Seed7}}==
[[Category:Seed7]]

var array integer: nums is [] (2, 4, 3, 1, 2);
var array integer: nums is [] (2, 4, 3, 1, 2);


nums := sort(nums);
nums := sort(nums);


==[[Smalltalk]]==
=={{header|Smalltalk}}==
[[Category:Smalltalk]]

#(7 5 2 9 0 -1) asSortedCollection
#(7 5 2 9 0 -1) asSortedCollection


==[[Tcl]]==
=={{header|Tcl}}==
[[Category:Tcl]]

set result [lsort -integer $unsorted_list]
set result [lsort -integer $unsorted_list]


==[[Toka]]==
=={{header|Toka}}==
[[Category:Toka]]
This can be done by using the bubble sort library:
This can be done by using the bubble sort library:


Line 306: Line 272:
See the Toka entry on [[Bubble_Sort]] for a full example.
See the Toka entry on [[Bubble_Sort]] for a full example.


==[[UNIX Shell]]==
=={{header|UNIX Shell}}==
[[Category:UNIX Shell]]
===[[Bourne Again SHell]]===
===[[Bourne Again SHell]]===
nums=(2 4 3 1 2)
nums=(2 4 3 1 2)

Revision as of 04:38, 13 November 2007

Task
Sort an integer array
You are encouraged to solve this task according to the task description, using any language you may know.

Sort an array (or list) of integers in ascending numerical order. Use a sorting facility provided by the language/library if possible.

4D

English

ARRAY INTEGER($nums;0)
APPEND TO ARRAY($nums;2)
APPEND TO ARRAY($nums;4)
APPEND TO ARRAY($nums;3)
APPEND TO ARRAY($nums;1)
APPEND TO ARRAY($nums;2)
SORT ARRAY($nums)  ` sort in ascending order
SORT ARRAY($nums;<)  ` sort in descending order

Français

TABLEAU ENTIER($nombres;0)
AJOUTER A TABLEAU($nombres;2)
AJOUTER A TABLEAU($nombres;4)
AJOUTER A TABLEAU($nombres;3)
AJOUTER A TABLEAU($nombres;1)
AJOUTER A TABLEAU($nombres;2)
TRIER TABLEAU($nombres)  ` pour effectuer un tri par ordre croissant
TRIER TABLEAU($nombres;<)  ` pour effectuer un tri par ordre décroissant

Ada

Compiler: GNAT GPL 2006

with Gnat.Heap_Sort_G;
 
procedure Integer_Sort is
   -- Heap sort package requires data to be in index values starting at
   -- 1 while index value 0 is used as temporary storage
   type Int_Array is array(Natural range <>) of Integer;
   Values : Int_Array := (0,1,8,2,7,3,6,4,5);
   
   -- define move and less than subprograms for use by the heap sort package
   procedure Move_Int(From : Natural; To : Natural) is
   begin
      Values(To) := Values(From);
   end Move_Int;
   
   function Lt_Int(Left, Right : Natural) return Boolean is
   begin
      return Values(Left) < Values (Right);
   end Lt_Int;
  
   -- Instantiate the generic heap sort package
   package Heap_Sort is new Gnat.Heap_Sort_G(Move_Int, Lt_Int);

begin
   Heap_Sort.Sort(8);
end Integer_Sort;

C

Compiler: GCC 4.0.1

#include <stdlib.h>

int intcmp(const void *i1, const void *i2)
{
    int left = *(int *)i1, right = *(int *)i2;
    return left >= right ? left > right ? 1 : 0 : -1;
}

int main()
{
    int nums[5] = {2,4,3,1,2};
    qsort(nums, 5, sizeof(int), intcmp);
}

C++

Compiler: GCC 4.0.1

Simple Array

#include <algorithm>

int main()
{
    int nums[] = {2,4,3,1,2};
    std::sort(nums, nums+5);
}

std::vector

#include <algorithm>
#include <vector>

int main()
{
    std::vector<int> nums;
    nums.push_back(2);
    nums.push_back(4);
    nums.push_back(3);
    nums.push_back(1);
    nums.push_back(2);
    std::sort(nums.begin(), nums.end());
}

std::list

#include <list>

int main()
{
    std::list<int> nums;
    nums.push_back(2);
    nums.push_back(4);
    nums.push_back(3);
    nums.push_back(1);
    nums.push_back(2);
    nums.sort();
}

Clean

We use list and array comprehensions to convert an array to and from a list in order to use the built-in sort on lists.

import StdEnv

sortArray :: (a e) -> a e | Array a e & Ord e
sortArray array = {y \\ y <- sort [x \\ x <-: array]}

Start :: {#Int}
Start = sortArray {2, 4, 3, 1, 2}

Common Lisp

In Common Lisp, the sort function takes a predicate that is used as the comparator. This parameter can be any two-argument function. To sort a sequence (list or array) of integers, call sort with the < operator as the predicate:


CL-USER> (sort #(9 -2 1 2 8 0 1 2) #'<)
#(-2 0 1 1 2 2 8 9)

E

[2,4,3,1,2].sort()

Erlang

List = [2, 4, 3, 1, 2].
SortedList = lists:sort(List).

Forth

Interpreter:Win32Forth 4.2

create test-data 2 , 4 , 3 , 1 , 2 ,
test-data 5 cell-sort

Haskell

Interpreter: GHCi 6.6

nums = [2,4,3,1,2] :: [Int]
sorted = List.sort nums

IDL

 result = array[sort(array)]

Java

import java.util.Arrays;

public class example {
    public static void main(String[] args)
    {
        int[] nums = {2,4,3,1,2};
        Arrays.sort(nums);
    }
}

JavaScript

Interpreter: Firefox 2.0

JavaScript sorts lexically by default, so "10000" comes before "2". To sort numerically, a custom comparator is used.

function numberSorter(a, b) {
  return a - b;
}
var numbers = [20, 7, 65, 10, 3, 0, 8, -60];
numbers.sort(numberSorter);
alert( numbers );

MAXScript

arr = #(5, 4, 3, 2, 1)
arr = sort arr

Objective-C

Compiler: GCC 4.0.1 (apple)

- (void)example
{
    NSArray *nums, *sorted;
    nums = [NSArray arrayWithObjects:
        [NSNumber numberWithInt:2],
        [NSNumber numberWithInt:4],
        [NSNumber numberWithInt:3],
        [NSNumber numberWithInt:1],
        [NSNumber numberWithInt:2],
        nil];
    sorted = [nums sortedArrayUsingSelector:@selector(compare:)];
}

Perl

Interpreter: perl 5.8.6

@nums = (2,4,3,1,2);
@sorted = sort {$a <=> $b} @nums;

PHP

Interpreter: PHP 4.4.4 CLI

<?php
$nums = array(2,4,3,1,2);
sort($nums);
?>

Pop11

Pop11 library function sorts lists. So we first convert array to list, then sort and finally convert back:

lvars ar = {2 4 3 1 2};
;;; Convert array to list
destvector(ar);
lvars ls = conslist();
;;; Sort it
sort(ls) -> ls;
;;; Convert list to array
destlist(ls);
consvector() -> ar;


Alternatively, using the datalist function and more economical, but possibly more opaque syntax, using pop11 as a functional language:

lvars ar = {2 4 3 1 2};
consvector(destlist(sort(datalist(ar)))) -> ar;

(The list will be garbage-collected.)

or in Forth-like pop11 postfix syntax:

lvars ar = {2 4 3 1 2};
ar.datalist.sort.destlist.consvector -> ar;

Python

Interpreter: Python 2.3

nums = [2,4,3,1,2]
nums.sort()

Note: The array nums is sorted in place.

Interpreter: Python 2.4 (and above)

You could also use the built-in sorted() function

 nums = sorted([2,4,3,1,2])

Raven

Sort list in place:

[ 2 4 3 1 2 ] sort

Ruby

Interpreter: ruby 1.8.4

nums = [2,4,3,1,2]
sorted = nums.sort

Seed7

var array integer: nums is [] (2, 4, 3, 1, 2);
nums := sort(nums);

Smalltalk

#(7 5 2 9 0 -1) asSortedCollection

Tcl

 set result [lsort -integer $unsorted_list]

Toka

This can be done by using the bubble sort library:

needs bsort
arrayname number_elements bsort

See the Toka entry on Bubble_Sort for a full example.

UNIX Shell

Bourne Again SHell

nums=(2 4 3 1 2)
sorted=($(for i in ${nums[*]}; do echo $i; done | sort -n))