Sort an integer array: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎[[Ruby]]: one more mixup)
(Added AppleScript version)
Line 2: Line 2:


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.

==[[AppleScript]]==
[[Category:AppleScript]]
This routine was copied directly from [http://www.apple.com/applescript/guidebook/sbrt/pgs/sbrt.05.htm Apple.com] and therefore is probably under their copyright, so: '''Copyright Apple, Inc. 2007'''. The reason I posted this is simply because this the best routine available without the use of an additional library.

ASCII_Sort({5, 2, 4, 3, 2, 1}) as list of integer
on ASCII_Sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end ASCII_Sort


==[[C]]==
==[[C]]==

Revision as of 02:58, 29 January 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.

AppleScript

This routine was copied directly from Apple.com and therefore is probably under their copyright, so: Copyright Apple, Inc. 2007. The reason I posted this is simply because this the best routine available without the use of an additional library.

ASCII_Sort({5, 2, 4, 3, 2, 1}) as list of integer

on ASCII_Sort(my_list)
    set the index_list to {}
    set the sorted_list to {}
    repeat (the number of items in my_list) times
        set the low_item to ""
        repeat with i from 1 to (number of items in my_list)
            if i is not in the index_list then
                set this_item to item i of my_list as text
                if the low_item is "" then
                    set the low_item to this_item
                    set the low_item_index to i
                else if this_item comes before the low_item then
                    set the low_item to this_item
                    set the low_item_index to i
                end if
            end if
        end repeat
        set the end of sorted_list to the low_item
        set the end of the index_list to the low_item_index
    end repeat
    return the sorted_list
end ASCII_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();
}

Haskell

Interpreter: GHCi 6.6

import List

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

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

Ruby

Interpreter: ruby 1.8.4

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

UNIX Shell

Interpreter: Bash 2.05b

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