Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

Questions tagged [sorting]

This challenge is intended to be solved by sorting, ordering, or otherwise organizing some set of data.

Filter by
Sorted by
Tagged with
17 votes
18 answers
1k views

Task Given an unsorted list of integers, order it in such a way that the absolute difference of every two adjacent elements will always be equal to 1: \$|dx| = 1\$ There will be guaranteed one or more ...
Glory2Ukraine's user avatar
8 votes
8 answers
820 views

You have a Tower of Hanoi-esque set of 3 pegs. There is a stack of unsorted disks labelled from 1 to n (inclusive) on one of these pegs. The other two pegs are ...
bigyihsuan's user avatar
  • 11.4k
8 votes
6 answers
1k views

Let N = [0,1,2,...n-1] be the initial segment of the natural numbers of length n, then all permutations of N can be sorted lexicographically, starting with the identity. The index into this sorted ...
Sophia Antipolis's user avatar
37 votes
17 answers
2k views

Background Recently, I was installing updates on my pc with Pacman (I use arch btw) and noticed the order it downloaded files was like this Start with the largest 4 downloads When a download ...
mousetail's user avatar
  • 14.4k
3 votes
1 answer
226 views

Given a list of strings in any convenient format display the amount of comparisons between individual characters that a merge sort algorithm would perform. The merge-sort algorithm is a divide & ...
Kamila Szewczyk's user avatar
11 votes
5 answers
594 views

In this challenge Bubbler describes sorting a list by inserting its elements left to right into a double ended queue or "deque". Take a number from the front of the array, and push it into ...
Wheat Wizard's user avatar
  • 103k
24 votes
16 answers
3k views

You're given an array of positive integers. Your job is to sort them using an initially empty deque (double-ended queue) by the following procedure: Take a number from the front of the array, and ...
Bubbler's user avatar
  • 79.3k
9 votes
8 answers
1k views

Given array \$A\$ \$(a_0, a_1,... , a_n) \$ as a permutation of \$(0, 1,...,n)\$ where \$n ≥ 2\$. Sort array \$A\$ into array \$(0, 1,...,n\$) with \$2\$ caveats: \$1\$ swap is counted when swapping ...
TruongVi's user avatar
  • 121
8 votes
3 answers
513 views

The following problem is taken from the real world — but indubitably code-golf! In this puzzle, the programming language is fixed, and your job is to write the most ...
Quuxplusone's user avatar
20 votes
32 answers
3k views

Given a list of Integers greater than zero, Sort each of the unique values in ascending order, then repeat the process on the remaining duplicate values and append. Example: ...
pacman256's user avatar
  • 5,395
3 votes
6 answers
377 views

Pseudo-Quantum Bogosort Quantum Bogosort is as follows: Quantumly randomise the list, such that there is no way of knowing what order the list is in until it is observed. This will divide the ...
pacman256's user avatar
  • 5,395
15 votes
17 answers
2k views

Carry sort is an \$O(n)\$ "sorting" algorithm. Here's how it works. The algorithm moves left to right along a list. As it traverses a list it "carries" a single item, the largest ...
Wheat Wizard's user avatar
  • 103k
19 votes
19 answers
2k views

Inspired by one of many bugs I ran into while implementing selection sort in trilangle. Given a non-empty list of non-negative integers, "sort" the list using the following procedure: Find ...
Bbrk24's user avatar
  • 3,863
14 votes
8 answers
2k views

Sometimes I feel like the conversation is going down the wrong path and take it upon myself to steer it in another direction. And this conversation is going wrong real fast, so I need to change the ...
noodle person's user avatar
14 votes
10 answers
530 views

Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details. Inspired by https://xkcd.com/835 In the midst of his mid-life crisis, Santa has impulsively purchased a Sports ...
Ginger's user avatar
  • 6,098
13 votes
14 answers
1k views

Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details. Santa likes to sort his presents in a special way. He keeps "uninterleaving" the pile of presents into ...
math scat's user avatar
  • 9,528
26 votes
7 answers
3k views

FizzBuzz is where a range of positive integers is taken, and numbers divisible by 3 are replaced with "Fizz", divisible by 5 with "Buzz" and divisible by 15 with "FizzBuzz&...
emanresu A's user avatar
  • 46.2k
1 vote
1 answer
296 views

I have recently been on a quest to create really really slow sorting algorithms that make Bogosort seem like it is the best. The task is simple: Sort an array of integers in as long average time as ...
Eknoma's user avatar
  • 27
5 votes
0 answers
271 views

I have a set of colored plastic cups. They come in four colors: green, yellow, pink, and blue. When I put them on my shelf, I like to stack them in a certain pattern. Your job is, given a list of any ...
Ginger's user avatar
  • 6,098
13 votes
9 answers
3k views

Related: Deck Names Friends or Foes Paying for Spells The Magic: the Gathering card game has five colours of magical mana: white (W), blue (U), black (B), red (R), and green (G). Cards can be any of ...
Matthew Jensen's user avatar
9 votes
2 answers
395 views

A few days ago I made a puzzle about moving people on an airplane. Now I am interested in the general version of this puzzle and the shortest code golf for it. I will briefly summarise the puzzle here....
Dmitry Kamenetsky's user avatar
21 votes
14 answers
2k views

Given a ragged list, e.g. [[4, 7], [5, 3, [], [6, [2]]]] Your challenge is to sort only the numbers in it. For example, with the above, the result would be ...
emanresu A's user avatar
  • 46.2k
10 votes
9 answers
640 views

Given an unordered list of musical pitches, write the shortest program/function (scored in bytes) to sort the list from lowest pitch to highest. Pitches will be given in scientific pitch notation, ...
Dingus's user avatar
  • 11.5k
20 votes
20 answers
2k views

Given a multidimensional, rectangular array of nonnegative integers, sort it at every depth (lexicographically), starting from the innermost. For example, with this array: ...
emanresu A's user avatar
  • 46.2k
28 votes
30 answers
3k views

Inspired by this StackOverflow post Given a list where each element appears a maximum of 2 times, we can define it's "sortedness" as the sum of the distances between equal elements. For ...
caird coinheringaahing's user avatar
17 votes
25 answers
2k views

Your task is to input a sequence of integers and chunk sort it. You may assume the input list can always be chunked into groups of 1, 2, and 3. Definition Chunk sorting (a term I made up for the ...
Alan Bagel's user avatar
  • 4,269
2 votes
1 answer
1k views

The event scheduler is a recurrent theme in event-driven architectures: something triggers an event that needs to be checked in the future one or more times. I has particular use in trading where you ...
user avatar
17 votes
13 answers
4k views

Background Slowsort is an in-place, stable sorting algorithm that has worse-than-polynomial time complexity. The pseudocode for Slowsort looks like this: ...
Bubbler's user avatar
  • 79.3k
12 votes
2 answers
775 views

I was trying to write a short MIPS32 code for computing the median of three registers. The rules: Assume that some values are pre-loaded into $t0, ...
M A's user avatar
  • 221
42 votes
17 answers
6k views

There is a "sorting" algorithm often called Stalin Sort, where instead of sorting an array, you just remove any items that are out of order. In this challenge, you'll implement part of a ...
rydwolf's user avatar
  • 19.3k
21 votes
16 answers
3k views

Inspired by the problem with the same name on Puzzling SE by our very own Dmitry Kamenetsky. You are to find the largest number that only uses every digit pair once, in a given base. For example, in ...
AviFS's user avatar
  • 2,211
8 votes
8 answers
659 views

Input: Ten unique integer coordinates, between (0,0) and (100,100). Output: The coordinates arranged in the order/an order such ...
qazwsx's user avatar
  • 1,194
19 votes
14 answers
2k views

TLDR: Sort your input according to a new English alphabet somewhat based on Chinese stroke count methods. Background: In a Chinese glossary/index, finding terms that are contained within the book is ...
Sumner18's user avatar
  • 1,926
13 votes
19 answers
3k views

You are James and four of your friends (Bjarne, Eich, Rossum, Ada) are called for an interview. There are n interviewers, and they can each serve one person at a time, alphabetically. Each round of ...
vrintle's user avatar
  • 3,000
3 votes
3 answers
338 views

The problem You just got a new job, hurray! You expect to be given various interesting tasks, and be able to learn a lot in the next few months. Good news, your boss gave you your first work this ...
Erel's user avatar
  • 155
12 votes
14 answers
1k views

The problem over here introduces an new type of strings: if you split the string into equal halfs and swap the pairs, it produces the same output as sorting the string. We call that a half-sort. ...
user avatar
34 votes
37 answers
5k views

My dog ate my calendar, and now my days are all mixed up. I tried putting it back together, but I keep mixing up the days of the week! I need some help putting my calendar back together, with the days ...
maxb's user avatar
  • 7,017
21 votes
30 answers
3k views

Given a string as an input (which can be any acceptable/convenient format in your language), implement pendulum encoding. The test cases are split into individual items (which aren't quoted) for a ...
user avatar
8 votes
3 answers
631 views

You are given an array \$A\$, which may contain duplicate elements. In each swap, you may swap the value of any two indices \$i, j\$ (i.e. switch the values of \$A_i\$ and \$A_j\$). What is the least ...
user avatar
7 votes
2 answers
355 views

This challenge is inspired by the same task I read some time ago in CACM. There are many ways to sort stuff. Most attempt to sort in as few operations as possible (= as fast as possible). However, it ...
Zizy Archer's user avatar
20 votes
17 answers
3k views

Roll for Initiative! Introduction In tabletop games like Dungeons and Dragons, when you begin a battle, all involved parties roll for initiative. In DnD 5e, this is ...
bigyihsuan's user avatar
  • 11.4k
28 votes
41 answers
2k views

In as few bytes as possible, sort the input, a delimited string OR list/vector/array of words, words contain printable ASCII except space (ASCII values 33-126) into a numbered list, table, or ...
Sumner18's user avatar
  • 1,926
12 votes
4 answers
898 views

The objective Given a string of Hangul syllables, sort the characters in North Korean dictionary order. Introduction to Hangul syllables Hangul(한글) is the Korean writing system invented by Sejong ...
Dannyu NDos's user avatar
  • 7,371
8 votes
7 answers
313 views

This is my first attempt at a Code Golf question. If it needs obvious edits, please feel free to make them. Below is a description of the basic Dropsort algorithm. Dropsort is run on a list of ...
James's user avatar
  • 183
34 votes
20 answers
3k views

Given a string x, output the characters in x sorted according to the order of appearance in your source code. Examples ...
tjjfvi's user avatar
  • 7,510
-6 votes
1 answer
580 views

Write a sorting algorithm for a list of integers that has an average performance of O(n^2). This is code-golf, so the shortest code in bytes wins. RULES: The characters wasted for the declaration of ...
Bip's user avatar
  • 531
21 votes
23 answers
3k views

Challenge: Input: A sorted list of positive integers. Output: The amount of integers which are still at the exact same index, after rotating the digits in each integer its index amount of times ...
Kevin Cruijssen's user avatar
30 votes
28 answers
4k views

Challenge Robin likes having his variables declaration in the shape of an arrow. Here's how he does it: Input any number of strings Order them by ascending length Output them ordered by the middle to ...
Teleporting Goat's user avatar
102 votes
47 answers
33k views

The sorting algorithm goes like this: While the list is not sorted, snap half of all items (remove them from the list). Continue until the list is sorted or only one item remains (which is sorted by ...
vrwim's user avatar
  • 2,509
14 votes
3 answers
633 views

Lets define a non-empty, unsorted and finite matrix with unique numbers as follow: $$N = \begin{Bmatrix} 4&5&7\\1&3&6 \end{Bmatrix}$$ Lets define 4 matrix moves as: ↑* (up): Moves a ...
Luis felipe De jesus Munoz's user avatar

1
2 3 4 5