sample

Sql code posted
created at 17 Dec 18:51

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
WITH RECURSIVE bubble_sort AS (
    SELECT
        id,
        value,
        ROW_NUMBER() OVER (ORDER BY id) AS rn,
        FALSE AS swapped
    FROM numbers
    UNION ALL
    SELECT
        n1.id,
        n1.value,
        n1.rn,
        CASE WHEN n1.value > n2.value THEN TRUE ELSE bubble_sort.swapped END
    FROM bubble_sort
    JOIN numbers n1 ON bubble_sort.rn = n1.rn
    JOIN numbers n2 ON bubble_sort.rn + 1 = n2.rn
    WHERE bubble_sort.swapped = TRUE
)
SELECT id, value
FROM bubble_sort
ORDER BY rn;
531 Bytes in 2 ms with coderay