Skip to main content

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
40 votes
12 answers
3k views

Perform gravity sort

Challenge Given a list of integers, show how gravity sort would be done. Gravity Sort In gravity sort, imagine the numbers as rows of asterisks. Then, everything falls, and the new rows will be ...
0 votes
3 answers
291 views

Output in the Correct Order

Ok lets say we get a input the following csv file (all entries are quoted strings) ...
114 votes
20 answers
6k views

Xorting an array

Conceptually, this challenge is really simple. You're given a list of non-negative integers ai. If possible, find a non-negative integer ...
34 votes
32 answers
3k views

Sort a string, sort of

If you sort a string you'll typically get something like: ':Iaaceeefggghiiiiklllllmnnooooprrssstttttuuyyyy Yes, that was the first sentence sorted. As you ...
31 votes
35 answers
5k views

Help, I've mixed my week up!

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 ...
11 votes
3 answers
655 views

North Korean dictionary order

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 ...
40 votes
13 answers
5k views

Eating Candy in the Correct Order

When it comes to eating candy, I hold myself to higher standards than the typical layperson. There is a delicate balance between "mixing it up" and "saving the best for last." In this challenge, you ...
115 votes
27 answers
9k views

Sort a list and write some English!

Your boss managed to read the secret hidden message. He didn't end up firing you, though, he just made you a secretary and forbade you from writing code. But you're a programmer. You need to write ...
25 votes
27 answers
2k views

Sort by Largest Digit(s)

Challenge: Given a list of integer, sort descending by their single largest digit(s). The order for numbers with the same largest digit are then sorted by second largest digit, etc. We ignore ...
49 votes
35 answers
3k views

Sever-sort an array

Challenge Given a non-empty array of integers, e.g.: [5, 2, 7, 6, 4, 1, 3] First sever it into arrays where no item is larger than the previous (i.e. non-...
8 votes
3 answers
444 views

Stably sort N elements in this restricted "cmov" language

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 ...
20 votes
32 answers
3k views

Iteratively sort a list

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: ...
24 votes
19 answers
2k views

Sort useless characters

This challenge is inspired by this very nice answer by TidB. In TidB's answer, every eight character is in the correct order: gnilwoB edoC (...
10 votes
9 answers
538 views

Sort musical pitches

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, ...
27 votes
66 answers
9k views

Find the longest word in an array

Your challenge is to take an array of strings and output the longest string in the array. So for ["tiny", "small", "bigger", "biggest"] the output would be ...
16 votes
20 answers
1k views

Sorting rectangular matrices

Given a matrix A, we can say that it is "sorted" if each row and column of A is sorted (ascending left-to-right for ...
30 votes
33 answers
3k views

Numbers Increase While Letters Decrease

Inspired by this Stack Overflow question: Sorting a list: numbers in ascending, letters in descending. Your task is to solve the following problem and, as this is code-golf, you should do so in as few ...
30 votes
25 answers
2k views

Swap indices and values

The task Write a program or function whose input is a list/array X of integers, and whose output is a list of sets of integers Y, such that for each element e in each set Y[i], X[e] = i, and such ...
30 votes
58 answers
7k views

Sort an Integer List

The Challenge It's quite simple really, sort a list of numbers. Details You must sort a list of numbers in ascending order, without using any built-in sorting functions/libraries/etc (i.e. ...
46 votes
50 answers
5k views

The Rien Number

The Champernowne constant is a number that is constructed by concatenating the first n numbers, with n tending to infinity. It ...
4 votes
6 answers
277 views

Pseudo-Quantum Bogosort [closed]

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 ...
13 votes
14 answers
1k views

CGAC2022 Day 10: Help Santa sort presents!

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 ...
21 votes
34 answers
2k views

Count My Change

Your task is to sort an array containing the strings "quarter", "dime", "nickel", and "penny" any number of times in no specific order and sort them so that they are in this order: ...
23 votes
35 answers
4k views

Help me do my washing

Help! My Completely Automated Lights From Darks Separator V3001.01 broke! :( Guidelines Task Write a program that will take an input of an array (or a list in some languages) of any amount of strings ...
25 votes
37 answers
4k views

Reliably Broken Sort

Given a list of positive integers that contains at least 3 distinct entries, output a permutation of that list that isn't sorted in ascending or descending order. Examples ...
14 votes
39 answers
2k views

Odd'em out: letters

Objective Write a program or function (or equivalent) that sorts out and returns the odd letter in the matrix of random size. Details You will be passed a matrix (as a string) as input of random ...
14 votes
46 answers
5k views

Sign that word!

What is a Word Signature? The signature of a word are all of it's letters put in order - the signatures of this, hist and ...
30 votes
49 answers
2k views

Enumerate each series of identical numbers in-place

Given a list of strictly positive integers, go through each distinct number and replace all occurrences of it with successive indices (zero or one based) of a new series. Examples ...
14 votes
4 answers
954 views

Un-Merge a List

Introduction Most of you are familiar with the merge sort algorithm for sorting a list of numbers. As part of the algorithm, one writes a helper function called ...
28 votes
30 answers
3k views

Unsort an array

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 ...
25 votes
65 answers
6k views

Sort odd numbers first

Rearrange a given list such that all the odd numbers appear before all the even numbers. Besides for this requirement, the output list may be in any order. The input will only contain integers, but ...
18 votes
27 answers
3k views

Sort digits by their first occurrence in pi

Given a non-negative number n, sort the digits of n by their first occurrence in pi. Input can be taken via function cli ...
47 votes
69 answers
8k views

List of possible birth years of living humans

The Challenge Output a list of years that starts with the current year and ends 120 years ago. The birth year of every living human would be included in this list. Details The list should be in ...
68 votes
69 answers
10k views

Lossy Sorting (Implement Dropsort)

Dropsort, designed by David Morgan-Mar, is an example of a linear-time "sorting algorithm" that produces a list that is, in fact, sorted, but contains only some of the original elements. Any ...
39 votes
77 answers
22k views

Sort numbers by binary 1's count

Goal Write a function or program sort an array of integers in descending order by the number of 1's present in their binary representation. No secondary sort condition is necessary. Example sorted ...
20 votes
30 answers
2k views

Pendulum Encoding

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 ...
8 votes
6 answers
1k views

Shortest code to sort walking points

The challenge is to, given a list of points, sort in a way that, when they are connected in this order, they never intersect. Input format (read from stdin): ...
34 votes
20 answers
3k views

A quine of sorts

Given a string x, output the characters in x sorted according to the order of appearance in your source code. Examples ...
102 votes
47 answers
32k views

Implement the Thanos sorting algorithm

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 ...
15 votes
17 answers
2k views

How long to carry sort?

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 ...
30 votes
47 answers
3k views

Implement Bogosort

Is solving Sudoku too hard? Even the brute force version? Here's a coding exercise that's a little easier. I hope. :-P Write the shortest function to implement bogosort. In specific, your function ...
19 votes
19 answers
1k views

"Sort" by element duplication

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 ...
28 votes
39 answers
2k views

Sort and Table a Sentence by Word Lengths

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 ...
18 votes
28 answers
2k views

Sign that word 2!

Sign that word 2! Not that long ago, I posted a challenge called Sign that word!. In the challenge, you must find the signature of word, which is the letters put in order (e.g. The signature of ...
16 votes
8 answers
1k views

Code Golf: Mix the nuts so that none of the same kind are touching

Input: Input is a randomized array of nuts (in your language), the possible nuts follow. Your program must have a way of representing each kind of nut, such as an integer code. Program must be able ...
21 votes
21 answers
3k views

Sort the months of the year

Write a function or program that takes string inputs, fully-spelled, English month names in title case: January, February, ...
14 votes
8 answers
2k views

Change the subject

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 ...
10 votes
1 answer
527 views

Help me fill my wallet!

A while ago I purchased a new wallet which is able to hold 8 cards (4 on both side). However, I seem to have way more cards than that and I need to make choices on which ones I want to carry with me. ...
8 votes
13 answers
2k views

Alternating sort

Given a list of pairwise distinct integers, sort it in alternating order: The second number is larger than the first, the third is smaller than the second, the fourth is larger than the third etc. (...
11 votes
3 answers
803 views

Sort some apples!

Problem Imagine 7 buckets lined up in a row. Each bucket can contain at most 2 apples. There are 13 apples labeled 1 through 13. They are distributed among the 7 buckets. For example, ...

1
2 3 4 5