{ }codelper
sorting listsP0036

Sort the numbers

The first line gives the count n, and the second line gives n integers.
Print them in ascending order on one line, separated by single spaces.
Repeated numbers are printed as many times as they appear.

Input
1 ≤ n ≤ 1000, -2,000,000,000 ≤ each integer ≤ 2,000,000,000
Output
the n sorted numbers on one line
Example 1 · stdin
4
10 9 2 -5
stdout
-5 2 9 10
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

In JS and TS, a.sort() without a comparison function turns numbers into strings and sorts them in dictionary order, so [10, 9, 2] becomes 10 2 9.
Always write a.sort((x, y) => x - y) (JS numbers are doubles, so subtracting is safe here).
In C, by contrast, returning a - b from a qsort comparator overflows int for 2·10^9 - (-2·10^9) and flips the order, so write (a > b) - (a < b).
The others sort by numeric size by default: Python sorted(a), Java Arrays.sort(a), C# Array.Sort(a), C++ std::sort, Craykon a.Sort().

Pythonmain.py · 3 lines
n = int(input())a = sorted(map(int, input().split()))print(*a)
JavaScriptmain.js · 3 lines
const [n, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);a.sort((x, y) => x - y);console.log(a.join(" "));
TypeScriptmain.ts · 4 lines
import { readFileSync } from "node:fs";const [n, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);a.sort((x, y) => x - y);console.log(a.join(" "));
JavaMain.java · 16 lines
import java.util.Arrays;import java.util.Scanner;import java.util.StringJoiner; public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        int[] a = new int[n];        for (int i = 0; i < n; i++) a[i] = sc.nextInt();        Arrays.sort(a);        StringJoiner out = new StringJoiner(" ");        for (int x : a) out.add(String.valueOf(x));        System.out.println(out);    }}
C#Main.cs · 13 lines
using System;using System.Linq; class Program{    static void Main()    {        Console.ReadLine();        int[] a = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();        Array.Sort(a);        Console.WriteLine(string.Join(" ", a));    }}
C++main.cpp · 13 lines
#include <algorithm>#include <iostream>#include <vector> int main() {    int n;    std::cin >> n;    std::vector<int> a(n);    for (int& x : a) std::cin >> x;    std::sort(a.begin(), a.end());    for (int i = 0; i < n; i++) std::cout << (i ? " " : "") << a[i];    std::cout << "\n";}
Cmain.c · 17 lines
#include <stdio.h>#include <stdlib.h> static int ascending(const void *pa, const void *pb) {    int a = *(const int *)pa, b = *(const int *)pb;    return (a > b) - (a < b);} int main(void) {    int n, a[1000];    scanf("%d", &n);    for (int i = 0; i < n; i++) scanf("%d", &a[i]);    qsort(a, n, sizeof a[0], ascending);    for (int i = 0; i < n; i++) printf(i ? " %d" : "%d", a[i]);    putchar('\n');    return 0;}
Craykonmain.cray · 4 lines
n = Console.ReadLine().ToNumber();a = Console.ReadNumbers();a.Sort();Console.WriteLine(a);