# Questions tagged [sorting]

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

169 questions
17answers
5k 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 ...
20answers
8k 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 ...
38answers
23k 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 ...
28answers
4k views

### Vertically collapse text

Say I have text like this (each word on one line, with no spaces) Programming Puzzles & Code Golf That makes no sense! It totally defies the laws of physics. ...
23answers
11k views

### Contest: fastest way to sort a big array of Gaussian-distributed data

Following the interest in this question, I thought it would be interesting to make answers a bit more objective and quantitative by proposing a contest. The idea is simple: I have generated a binary ...
18answers
3k views

### There are two new sheriffs in town – Identifying DJMcMego pairs!

We have some new sheriffs moderators in town, Mego and DJMcMayhem. We need a challenge to properly honour them for their new positions, so there we go. Here's something that has caught my attention ...
54answers
7k 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 element ...
25answers
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-...
16answers
4k views

### Implement the “Lazy Sort”

I'm supposed to sort a list of numbers, but I'm super lazy. It's really hard to figure how to swap all the numbers around until all of them are in increasing order, so I came up with my own algorithm ...
25answers
5k views

### DropSort it like it's hot

As described in this question: 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 ...
59answers
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 ...
32answers
4k views

### Output numbers up to 2^n-1, “sorted”

Take a positive integer n as input, and output (some of the) decimal numbers that can be created using n bits, ordered in the following way: First list all the numbers that can be created with only ...
37answers
4k 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 ...
9answers
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 ...
69answers
14k 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 ...
9answers
2k views

### Indentation-based Sort

Given an ordered list of same-case letter strings (a-z XOR A-Z) where each string is preceded by 0 or more space ( ) characters, output the same list but with the strings sorted at each level of ...
22answers
3k views

### Sort the climbing grades

My first code golf post, apologies for any mistakes... Context In rock climbing (bouldering specifically), the V/Vermin (USA) climbing grades start at 'VB' (the easiest grade), and then go 'V0', 'V0+...
9answers
4k views

### A “Sorting” algorithm

There is a "sorting algorithm" sometimes called Stalin sort in which in order to sort a list you simply remove elements from the list until it is sorted in increasing order. For example the list <...
16answers
3k views

### This is the sort of challenge that bytes

I need to stop thinking of punny names Your task is to create as many snippets (programs that have input and output built-in), functions or full programs as possible that sorts whatever your language'...
2answers
5k views

### Implement QuickSort in BrainF*** [closed]

As discussed in the Lounge room on Stack Overflow: if you can't implement the Quicksort algorithm given en.wikipedia.org/wiki/Quicksort in any language you have minimal knowledge of, you might want ...
6answers
1k views

### Sort the Textbooks

Sort the Textbooks School is starting soon (if it hasn't already) and so it's time to get our textbooks in order. You need to sort your books in alphabetical order but that takes too long so you ...
8answers
3k views

### Sort these James Bond ratings

Introduction My grandpa is a fan of James Bond, but he is always unsure on how to rank his favourite actors. As such, he is always making lists, which is a lot of work. He asked me to produce a ...
29answers
2k views

### Interleave strings

Inspiration.* I cannot believe we have not had this challenge before: Task Given one or more printable ASCII strings, interleave them by taking one character from each string, cyclically until out ...
17answers
2k views

### Show the Top Five Comment Scores on a SE Post

A Stack Exchange script determines which five comments on questions or answers are initially seen on the main page of sites through the number of upvotes on them; the five comments with the highest ...
8answers
2k views

### Visualize Merge Sort

Merge sort is a sorting algorithm which works by splitting a given list in half, recursively sorting both smaller lists, and merging them back together to one sorted list. The base case of the ...
26answers
4k views

### Arrow those variables!

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 ...
25answers
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 ...
21answers
1k 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 ...
8answers
2k 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 ...
8answers
2k views

### Tell me how to flop

As computer scientists, you're probably all familiar with the basic list operations of pop and push. These are simple operations that modify a list of elements. However, have you ever heard of the ...
37answers
2k 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 ...
12answers
4k views

### Flipping pancakes

In pancake sorting the only allowed operation is to reverse the elements of some prefix of the sequence. Or, think of a stack of pancakes: We insert a spatula somewhere in the stack and flip all the ...
24answers
3k views

### Sort this, quick!

Well... there are 59 (now 60) questions tagged sorting, but no simple quicksorts. That must be fixed. For those unfamiliar with quicksort, here is a breakdown, courtesy of Wikipedia- Pick an ...
36answers
4k views

### Implement Lazy Drop Sort

This challenge already describes dropsort. However, I'm kinda lazy and I really only need my array to be a bit more sorted than before, it doesn't need to be sorted all the way. In Drop Sort, we drop ...
29answers
2k views

### Ordering a list

Summary Given a list of integers, return the index each integer would end up at when sorted. For example, if the list was [0,8,-1,5,8], you should return ...
24answers
2k views

### Post-determined Array Sorting

Input: An array containing three integers: 0, 1 and 2 in any order (i.e. ...
31answers
3k views

### Driftsort an array

Driftsort is a simple way to "sort" an array. It works by "sliding" or "rotating" the elements over in the array until the array is sorted, or until the array fails to be sorted. Let's walk through ...
61answers
7k 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 ...
33answers
7k views

### Sort a list of numbers [closed]

About Code-Bowling: In golf, you try to get the lowest score (smallest application, most elegant, etc). In Bowling, you try to get the highest score. So if you follow, the goal of a Code-Bowling ...
20answers
2k views

### Imitate an ordering

Given two lists of numbers, a source and a pattern, reorder the source to match the relative ordering of the pattern. Any two entries of the reordered source should compare the same way as the entries ...
38answers
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 ...
18answers
5k views

### PronunciationSort™

We all know of different fancy sorting algorithms, but none of these give us numbers in a way that's easy to pronounce. To remedy this, I propose using PronunciationSort™, the most natural way to sort ...
29answers
2k views

### Rearrange a number into alphabetical order

Given a non-negative integer (n), create a function that returns n in alphabetical order, according to the literal spelling of ...
36answers
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 ...
25answers
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 ...
25answers
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 ...
26answers
2k views

### Find The Rank Of A Word

Definition The rank of a word is defined as the position of the word when all the possible permutations (or arrangements) of its letters are arranged alphabetically, like in a dictionary, no matter ...
9answers
2k views

### Sort a nested list

You must write a program or function that sorts a nested list. Here are the rules for sorting a nested list: Let's take this list as an example: ...
11answers
1k views

### Fairly rank values

Task Given an input list of integers x1…xn, compute a list of ranks r1…rn (a permutation of {1…n}) so that xr1 ≤ xr2 ≤ … ≤ xrn. Then, for each xi, replace its rank by ...
3answers
1k views

### The Burnt Pancake Problem

This challenge is related to Flipping Pancakes. You may have heard of pancake sorting, where a stack of pancakes is sorted by size by inserting spatula into the stack and flipping all of the pancakes ...