Sort an integer array: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎[[UNIX Shell]]: Changed formatting)
(→‎[[Haskell]]: Added Win32Forth technique)
Line 91: Line 91:
nums.sort();
nums.sort();
}
}

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


==[[Haskell]]==
==[[Haskell]]==

Revision as of 16:26, 16 February 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.


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 l = *(int *)i1, r = *(int *)i2;
    return l >= r ? l > r ? 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

With a simple array:

#include <algorithm>

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

With a 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());
}

With a 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();
}

Forth

Interpreter:Win32Forth 4.2

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

Haskell

Interpreter: GHCi 6.6

import List

nums = [2,4,3,1,2]
sorted = 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 );

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);
?>

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])

Ruby

Interpreter: ruby 1.8.4

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

Tcl

 set result [lsort -integer $unsorted_list]

UNIX Shell

Bourne Again SHell

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