Programming Search⌘K

Taskintro

Sort a list

Also called sort an array, sort numbers, order a list, ascending order, sort integers, numeric sort.

Sort a list of integers in ascending numeric order, and do not compare the numbers as text.

Task

Sort a list of integers in ascending order. The function sort_ints(items) returns the items from the smallest to the largest. For example, sort_ints([3, 1, 2]) is [1, 2, 3], and sort_ints([10, 9, 100, 1]) is [1, 9, 10, 100].

The comparison is by value, not by the text of the numbers, so 9 comes before 10. Equal items stay, so the result has the same length as the input. The items are 32-bit signed integers.

Solutions

Language Sort the integers
Python sorted(items)
JavaScript, TypeScript [...items].sort((a, b) => a - b)
Java Arrays.sort on a copy
C# Array.Sort on a copy
Go slices.Sort on slices.Clone(items)
Rust sort_unstable on items.to_vec()
C qsort with a compare function, in place
C++ std::ranges::sort
Bash printf '%s\n' "$@" | sort -n
  • Java. Arrays.sort(int[]) "Sorts the specified array into ascending numerical order"1. The solution sorts a copy, so the input does not change.
  • Go. "Sort sorts a slice of any ordered type in ascending order"2. slices.Clone makes the copy.
  • Rust. Equal integers are the same value, so a stable sort and an unstable sort give the same result.
  • C. The compare function returns (x > y) - (x < y). The cppreference example calls arg1 - arg2 an "erroneous shortcut: undefined behavior in case of integer overflow"3.
  • C++. The solution takes the vector by value, sorts it and returns it.
  • Bash. sort -n compares "according to string numerical value"4. LC_ALL=C gives the same result on each machine.

Edge cases

The cases are in cases/basic.jsonl and cases/edge.jsonl.

  • empty and single: an empty list and a list of one item are already sorted.
  • sorted and reversed: the input can be in order, or in the opposite order.
  • duplicates: equal items stay next to each other in the result.
  • negative: -10 comes before -1.
  • digits: 10, 9, 100 and 1 sort to 1, 9, 10, 100. A text sort gives 1, 10, 100, 9.
  • extremes: -2147483648 and 2147483647 are the two ends. A compare function with x - y overflows for them.

Pitfalls

  • Python list.sort returns None. "To remind users that it operates by side effect, it does not return the sorted sequence"5. Use sorted for a new list:
items = [3, 1, 2]
result = items.sort()
print(result, items)
print(sorted([3, 1, 2]))
Output
None [1, 2, 3]
[1, 2, 3]
  • JavaScript sort compares text. "The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code unit values"6. Give a compare function for numbers:
console.log([10, 9, 100, 1].sort());
console.log([10, 9, 100, 1].sort((a, b) => a - b));
Output
[ 1, 10, 100, 9 ]
[ 1, 9, 10, 100 ]
  • Java a compare function with x - y overflows. In the example, Integer.MIN_VALUE - 1 overflows, so the sort puts Integer.MIN_VALUE after 1. Use Integer::compare:
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    Integer[] a = {Integer.MIN_VALUE, 1, 0};
    Arrays.sort(a, (x, y) -> x - y);
    System.out.println(Arrays.toString(a));
    Arrays.sort(a, Integer::compare);
    System.out.println(Arrays.toString(a));
  }
}
Output
[0, 1, -2147483648]
[-2147483648, 0, 1]
  • C# Order makes a new sequence. It does not change the list. "This method is implemented by using deferred execution"7, so a call that drops the result does nothing. Keep the result, or use List.Sort:
var items = new List<int> { 3, 1, 2 };
items.Order();
Console.WriteLine(string.Join(" ", items));
var sorted = items.Order().ToList();
Console.WriteLine(string.Join(" ", sorted));
Output
3 1 2
1 2 3
  • Go slices.Sort changes the shared array. In the example, b := a does not copy the items, so the sort of b also changes a. Sort a clone:
package main

import (
	"fmt"
	"slices"
)

func main() {
	a := []int{3, 1, 2}
	b := a
	slices.Sort(b)
	fmt.Println(a, b)
	c := []int{3, 1, 2}
	d := slices.Clone(c)
	slices.Sort(d)
	fmt.Println(c, d)
}
Output
[1 2 3] [1 2 3]
[3 1 2] [1 2 3]
  • Rust sort of f64 needs total_cmp. "Sorting types that only implement PartialOrd such as f32 and f64 require additional precautions"8. In the example, sort_by(f64::total_cmp) puts NaN at the end:
fn main() {
    let mut xs = [2.5, f64::NAN, -1.0, 0.0];
    xs.sort_by(f64::total_cmp);
    println!("{xs:?}");
}
Output
[-1.0, 0.0, 2.5, NaN]
  • C compare functions must not subtract. The example sorts INT_MIN and INT_MAX with the safe compare function:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

static int compare(const void *a, const void *b) {
    int x = *(const int *)a;
    int y = *(const int *)b;
    return (x > y) - (x < y);
}

int main(void) {
    int a[] = {1, INT_MIN, 0, INT_MAX};
    qsort(a, 4, sizeof a[0], compare);
    printf("%d %d %d %d\n", a[0], a[1], a[2], a[3]);
    return 0;
}
Output
-2147483648 0 1 2147483647
  • C++ std::sort needs random access. "RandomIt must meet the requirements of LegacyRandomAccessIterator"9. A std::list does not have such iterators, so use its member sort. For descending order, give std::greater{}:
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <vector>

int main() {
    std::list<int> l{3, 1, 2};
    // std::sort(l.begin(), l.end()) does not compile: list iterators are not
    // random access. The member function sort works.
    l.sort();
    for (int x : l) {
        std::cout << x << (x == l.back() ? "\n" : " ");
    }
    std::vector<int> v{3, 1, 2};
    std::ranges::sort(v, std::greater{});
    for (int x : v) {
        std::cout << x << (x == v.back() ? "\n" : " ");
    }
}
Output
1 2 3
3 2 1
  • Bash sort without -n compares text. Give -n for numbers. It also sorts negative numbers in the correct order:
printf '%s\n' 10 9 100 1 | LC_ALL=C sort | paste -s -d ' ' -
printf '%s\n' 10 9 100 1 | LC_ALL=C sort -n | paste -s -d ' ' -
printf '%s\n' 5 -10 -1 | LC_ALL=C sort -n | paste -s -d ' ' -
Output
1 10 100 9
1 9 10 100
-10 -1 5

Code

The build runs the native tests of each language in a pinned toolchain.

sort_ints.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://programming.tamnd.com/sort-a-list/.

def sort_ints(items: list[int]) -> list[int]:
    """Return a new list of the items in ascending order."""
    # sorted makes a new list. items.sort() sorts in place and returns None.
    return sorted(items)
test_sort_ints.py 25 lines
# SPDX-License-Identifier: MIT
# Part of https://programming.tamnd.com/sort-a-list/.

import pytest

from sort_ints import sort_ints

CASES = {
    "small": ([3, 1, 2], [1, 2, 3]),
    "five": ([5, 4, 1, 3, 2], [1, 2, 3, 4, 5]),
    "empty": ([], []),
    "single": ([1], [1]),
    "sorted": ([1, 2, 3], [1, 2, 3]),
    "reversed": ([9, 8, 7], [7, 8, 9]),
    "duplicates": ([2, 1, 2, 1], [1, 1, 2, 2]),
    "negative": ([-1, -10, 5], [-10, -1, 5]),
    "digits": ([10, 9, 100, 1], [1, 9, 10, 100]),
    "extremes": ([2147483647, -2147483648, 0], [-2147483648, 0, 2147483647]),
}


@pytest.mark.parametrize("name", CASES)
def test_sort_ints(name):
    items, want = CASES[name]
    assert sort_ints(items) == want
sort_ints.cppI0RawGitHub
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

#include "sort_ints.hpp"

#include <algorithm>
#include <vector>

std::vector<int> sort_ints(std::vector<int> items) {
    std::ranges::sort(items);
    return items;
}
sort_ints.hpp 9 lines
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

#pragma once

#include <vector>

// sort_ints returns the items in ascending order.
std::vector<int> sort_ints(std::vector<int> items);
test_sort_ints.cpp 35 lines
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

#include "sort_ints.hpp"

#include <iostream>
#include <string_view>
#include <vector>

static int failures = 0;

static void check(std::string_view name, std::vector<int> items, const std::vector<int> &want) {
    if (sort_ints(items) != want) {
        std::cerr << "case " << name << " failed\n";
        failures++;
    }
}

int main() {
    check("small", std::vector<int>{3, 1, 2}, {1, 2, 3});
    check("five", std::vector<int>{5, 4, 1, 3, 2}, {1, 2, 3, 4, 5});
    check("empty", std::vector<int>{}, std::vector<int>{});
    check("single", std::vector<int>{1}, {1});
    check("sorted", std::vector<int>{1, 2, 3}, {1, 2, 3});
    check("reversed", std::vector<int>{9, 8, 7}, {7, 8, 9});
    check("duplicates", std::vector<int>{2, 1, 2, 1}, {1, 1, 2, 2});
    check("negative", std::vector<int>{-1, -10, 5}, {-10, -1, 5});
    check("digits", std::vector<int>{10, 9, 100, 1}, {1, 9, 10, 100});
    check("extremes", std::vector<int>{2147483647, (-2147483647 - 1), 0}, {(-2147483647 - 1), 0, 2147483647});
    if (failures > 0) {
        return 1;
    }
    std::cout << "all cases pass\n";
    return 0;
}
SortInts.javaI0RawGitHub
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

import java.util.Arrays;

final class SortInts {
  private SortInts() {}

  /** Returns a new array of the items in ascending order. */
  static int[] sortInts(int[] items) {
    int[] out = items.clone();
    Arrays.sort(out);
    return out;
  }
}
SortIntsTest.java 31 lines
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

class SortIntsTest {
  static Stream<Arguments> cases() {
    return Stream.of(
        Arguments.of("small", new int[] {3, 1, 2}, new int[] {1, 2, 3}),
        Arguments.of("five", new int[] {5, 4, 1, 3, 2}, new int[] {1, 2, 3, 4, 5}),
        Arguments.of("empty", new int[] {}, new int[] {}),
        Arguments.of("single", new int[] {1}, new int[] {1}),
        Arguments.of("sorted", new int[] {1, 2, 3}, new int[] {1, 2, 3}),
        Arguments.of("reversed", new int[] {9, 8, 7}, new int[] {7, 8, 9}),
        Arguments.of("duplicates", new int[] {2, 1, 2, 1}, new int[] {1, 1, 2, 2}),
        Arguments.of("negative", new int[] {-1, -10, 5}, new int[] {-10, -1, 5}),
        Arguments.of("digits", new int[] {10, 9, 100, 1}, new int[] {1, 9, 10, 100}),
        Arguments.of("extremes", new int[] {2147483647, -2147483648, 0}, new int[] {-2147483648, 0, 2147483647}));
  }

  @ParameterizedTest(name = "{0}")
  @MethodSource("cases")
  void sortInts(String name, int[] items, int[] want) {
    assertArrayEquals(want, SortInts.sortInts(items));
  }
}
sort_ints.cI0RawGitHub
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

#include "sort_ints.h"

#include <stdlib.h>

// compare returns a negative value, 0 or a positive value. It does not use
// x - y, because the subtraction can overflow.
static int compare(const void *a, const void *b) {
    int x = *(const int *)a;
    int y = *(const int *)b;
    return (x > y) - (x < y);
}

void sort_ints(int *items, size_t n) {
    if (n > 1) {
        qsort(items, n, sizeof items[0], compare);
    }
}
sort_ints.h 12 lines
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

#ifndef SORT_INTS_H
#define SORT_INTS_H

#include <stddef.h>

// sort_ints sorts items[0..n) in ascending order, in place.
void sort_ints(int *items, size_t n);

#endif
test_sort_ints.c 54 lines
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

#include "sort_ints.h"

#include <stdbool.h>
#include <stdio.h>
#include <string.h>

static int failures = 0;

#define L(...) {__VA_ARGS__}

// Each list has a 0 at the end when it is empty, because C has no empty
// array. The counts n and m give the real lengths.
static void check(const char *name, const int *got, size_t k, const int *want, size_t m) {
    bool ok = k == m;
    for (size_t i = 0; ok && i < m; i++) {
        ok = got[i] == want[i];
    }
    if (!ok) {
        fprintf(stderr, "case %s failed\n", name);
        failures++;
    }
}

#define CHECK(name, n, given, m, want)  \
    do {                                \
        const int g[] = given;          \
        const int w[] = want;           \
        int out[sizeof g / sizeof g[0]]; \
        memcpy(out, g, sizeof g); \
        sort_ints(out, n); \
        size_t k = n; \
        check(name, out, k, w, m);      \
    } while (0)

int main(void) {
    CHECK("small", 3, L(3, 1, 2), 3, L(1, 2, 3));
    CHECK("five", 5, L(5, 4, 1, 3, 2), 5, L(1, 2, 3, 4, 5));
    CHECK("empty", 0, L(0), 0, L(0));
    CHECK("single", 1, L(1), 1, L(1));
    CHECK("sorted", 3, L(1, 2, 3), 3, L(1, 2, 3));
    CHECK("reversed", 3, L(9, 8, 7), 3, L(7, 8, 9));
    CHECK("duplicates", 4, L(2, 1, 2, 1), 4, L(1, 1, 2, 2));
    CHECK("negative", 3, L(-1, -10, 5), 3, L(-10, -1, 5));
    CHECK("digits", 4, L(10, 9, 100, 1), 4, L(1, 9, 10, 100));
    CHECK("extremes", 3, L(2147483647, (-2147483647 - 1), 0), 3, L((-2147483647 - 1), 0, 2147483647));
    if (failures > 0) {
        return 1;
    }
    printf("all cases pass\n");
    return 0;
}
SortInts.csI0RawGitHub
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

using System;

public static class SortInts
{
    // A copy, so items does not change. Array.Sort sorts in place.
    public static int[] Sort(int[] items)
    {
        var output = (int[])items.Clone();
        Array.Sort(output);
        return output;
    }
}
SortIntsTests.cs 29 lines
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

using Xunit;

public sealed class SortIntsTests
{
    public static TheoryData<string, int[], int[]> Cases => new()
    {
        { "small", new int[] { 3, 1, 2 }, new int[] { 1, 2, 3 } },
        { "five", new int[] { 5, 4, 1, 3, 2 }, new int[] { 1, 2, 3, 4, 5 } },
        { "empty", new int[0], new int[0] },
        { "single", new int[] { 1 }, new int[] { 1 } },
        { "sorted", new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 } },
        { "reversed", new int[] { 9, 8, 7 }, new int[] { 7, 8, 9 } },
        { "duplicates", new int[] { 2, 1, 2, 1 }, new int[] { 1, 1, 2, 2 } },
        { "negative", new int[] { -1, -10, 5 }, new int[] { -10, -1, 5 } },
        { "digits", new int[] { 10, 9, 100, 1 }, new int[] { 1, 9, 10, 100 } },
        { "extremes", new int[] { 2147483647, -2147483648, 0 }, new int[] { -2147483648, 0, 2147483647 } },
    };

    [Theory]
    [MemberData(nameof(Cases))]
    public void Check(string name, int[] items, int[] want)
    {
        Assert.NotNull(name);
        Assert.Equal(want, SortInts.Sort(items));
    }
}
sort_ints.jsI0RawGitHub
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

/**
 * @param {number[]} items
 * @returns {number[]}
 */
export function sortInts(items) {
  // A copy, so items does not change. Without the compare function, sort
  // compares the numbers as strings, and 10 comes before 9.
  return [...items].sort((a, b) => a - b);
}
sort_ints.test.js 26 lines
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

import assert from "node:assert/strict";
import { test } from "node:test";

import { sortInts } from "./sort_ints.js";

const cases = [
  ["small", [3, 1, 2], [1, 2, 3]],
  ["five", [5, 4, 1, 3, 2], [1, 2, 3, 4, 5]],
  ["empty", [], []],
  ["single", [1], [1]],
  ["sorted", [1, 2, 3], [1, 2, 3]],
  ["reversed", [9, 8, 7], [7, 8, 9]],
  ["duplicates", [2, 1, 2, 1], [1, 1, 2, 2]],
  ["negative", [-1, -10, 5], [-10, -1, 5]],
  ["digits", [10, 9, 100, 1], [1, 9, 10, 100]],
  ["extremes", [2147483647, -2147483648, 0], [-2147483648, 0, 2147483647]],
];

for (const [name, items, want] of cases) {
  test(name, () => {
    assert.deepEqual(sortInts(items), want);
  });
}
sort_ints.tsI0RawGitHub
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

export function sortInts(items: readonly number[]): number[] {
  // A copy, so items does not change. Without the compare function, sort
  // compares the numbers as strings, and 10 comes before 9.
  return [...items].sort((a, b) => a - b);
}
sort_ints.test.ts 26 lines
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

import assert from "node:assert/strict";
import { test } from "node:test";

import { sortInts } from "./sort_ints.ts";

const cases: [string, number[], number[]][] = [
  ["small", [3, 1, 2], [1, 2, 3]],
  ["five", [5, 4, 1, 3, 2], [1, 2, 3, 4, 5]],
  ["empty", [], []],
  ["single", [1], [1]],
  ["sorted", [1, 2, 3], [1, 2, 3]],
  ["reversed", [9, 8, 7], [7, 8, 9]],
  ["duplicates", [2, 1, 2, 1], [1, 1, 2, 2]],
  ["negative", [-1, -10, 5], [-10, -1, 5]],
  ["digits", [10, 9, 100, 1], [1, 9, 10, 100]],
  ["extremes", [2147483647, -2147483648, 0], [-2147483648, 0, 2147483647]],
];

for (const [name, items, want] of cases) {
  test(name, () => {
    assert.deepEqual(sortInts(items), want);
  });
}
sort_ints.goI0RawGitHub
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

// Package sortints sorts a slice of integers.
package sortints

import "slices"

// SortInts returns a new slice of the items in ascending order. The input
// slice does not change.
func SortInts(items []int) []int {
	out := slices.Clone(items)
	slices.Sort(out)
	return out
}
sort_ints_test.go 42 lines
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

package sortints

import (
	"slices"
	"testing"
)

var cases = []struct {
	name  string
	items []int
	want  []int
}{
	{"small", []int{3, 1, 2}, []int{1, 2, 3}},
	{"five", []int{5, 4, 1, 3, 2}, []int{1, 2, 3, 4, 5}},
	{"empty", []int{}, []int{}},
	{"single", []int{1}, []int{1}},
	{"sorted", []int{1, 2, 3}, []int{1, 2, 3}},
	{"reversed", []int{9, 8, 7}, []int{7, 8, 9}},
	{"duplicates", []int{2, 1, 2, 1}, []int{1, 1, 2, 2}},
	{"negative", []int{-1, -10, 5}, []int{-10, -1, 5}},
	{"digits", []int{10, 9, 100, 1}, []int{1, 9, 10, 100}},
	{"extremes", []int{2147483647, -2147483648, 0}, []int{-2147483648, 0, 2147483647}},
}

func TestSortInts(t *testing.T) {
	for _, c := range cases {
		if got := SortInts(c.items); !slices.Equal(got, c.want) {
			t.Errorf("%s: got %v, want %v", c.name, got, c.want)
		}
	}
}

func TestInputDoesNotChange(t *testing.T) {
	items := []int{3, 1, 2}
	SortInts(items)
	if !slices.Equal(items, []int{3, 1, 2}) {
		t.Errorf("the input changed: %v", items)
	}
}
sort_ints.rsI0RawGitHub
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

/// Returns a new vector of the items in ascending order.
pub fn sort_ints(items: &[i32]) -> Vec<i32> {
    let mut out = items.to_vec();
    // For integers, equal items are the same, so an unstable sort is fine.
    out.sort_unstable();
    out
}
tests/sort_ints_test.rs 23 lines
// SPDX-License-Identifier: MIT
// Part of https://programming.tamnd.com/sort-a-list/.

use algo_sort_a_list::sort_ints;

#[test]
fn cases() {
    let cases: &[(&str, &[i32], &[i32])] = &[
        ("small", &[3, 1, 2], &[1, 2, 3]),
        ("five", &[5, 4, 1, 3, 2], &[1, 2, 3, 4, 5]),
        ("empty", &[], &[]),
        ("single", &[1], &[1]),
        ("sorted", &[1, 2, 3], &[1, 2, 3]),
        ("reversed", &[9, 8, 7], &[7, 8, 9]),
        ("duplicates", &[2, 1, 2, 1], &[1, 1, 2, 2]),
        ("negative", &[-1, -10, 5], &[-10, -1, 5]),
        ("digits", &[10, 9, 100, 1], &[1, 9, 10, 100]),
        ("extremes", &[2147483647, -2147483648, 0], &[-2147483648, 0, 2147483647]),
    ];
    for &(name, items, want) in cases {
        assert_eq!(sort_ints(items), want, "{name}");
    }
}
sort_ints.shI0RawGitHub
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
# Part of https://programming.tamnd.com/sort-a-list/.

# sort_ints ITEM...
# Prints the integer arguments in ascending order, one on each line.
sort_ints() {
  if (($# == 0)); then
    return 0
  fi
  # -n compares the numbers. Without it, sort compares text, and 10 comes
  # before 9.
  printf '%s\n' "$@" | LC_ALL=C sort -n
}
test_sort_ints.bats 36 lines
#!/usr/bin/env bats
# SPDX-License-Identifier: MIT
# Part of https://programming.tamnd.com/sort-a-list/.

bats_require_minimum_version 1.5.0

setup() {
  source "$BATS_TEST_DIRNAME/sort_ints.sh"
}

lines() { # lines ITEM...: prints each item on a line, and nothing for no items
  if (($# > 0)); then printf '%s\n' "$@"; fi
}

check() { # check FUNCTION GIVEN... -- WANT...
  local fn=$1 got want
  shift
  local -a given=()
  while [ "$1" != -- ]; do given+=("$1"); shift; done
  shift
  # The x keeps the trailing newlines, which $(...) removes.
  got=$("$fn" "${given[@]}"; printf x)
  want=$(lines "$@"; printf x)
  [ "$got" = "$want" ] || { printf 'got:  %q\nwant: %q\n' "$got" "$want"; return 1; }
}

@test "small" { check sort_ints 3 1 2 -- 1 2 3; }
@test "five" { check sort_ints 5 4 1 3 2 -- 1 2 3 4 5; }
@test "empty" { check sort_ints --; }
@test "single" { check sort_ints 1 -- 1; }
@test "sorted" { check sort_ints 1 2 3 -- 1 2 3; }
@test "reversed" { check sort_ints 9 8 7 -- 7 8 9; }
@test "duplicates" { check sort_ints 2 1 2 1 -- 1 1 2 2; }
@test "negative" { check sort_ints -1 -10 5 -- -10 -1 5; }
@test "digits" { check sort_ints 10 9 100 1 -- 1 9 10 100; }
@test "extremes" { check sort_ints 2147483647 -2147483648 0 -- -2147483648 0 2147483647; }

References

  1. Java SE 25 API, Class Arrays, sort(int[])
  2. Go standard library, package slices, Sort, Clone
  3. cppreference.com, qsort, qsort_s, Example
  4. Linux manual page, sort(1), -n, --numeric-sort
  5. The Python Standard Library, Built-in Types, list.sort()
  6. MDN Web Docs, Array.prototype.sort(), Description
  7. .NET API reference, Enumerable.Order method, Remarks
  8. Rust standard library, primitive type slice, sort_unstable, sort_by
  9. cppreference.com, std::sort, Type requirements

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (bash)I0 not checked yet
  • the tests did not run in this build
Code (c)I0 not checked yet
  • the tests did not run in this build
Code (cpp)I0 not checked yet
  • the tests did not run in this build
Code (csharp)I0 not checked yet
  • the tests did not run in this build
Code (go)I0 not checked yet
  • the tests did not run in this build
Code (java)I0 not checked yet
  • the tests did not run in this build
Code (javascript)I0 not checked yet
  • the tests did not run in this build
Code (python)I0 not checked yet
  • the tests did not run in this build
Code (rust)I0 not checked yet
  • the tests did not run in this build
Code (typescript)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source