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
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. ...
Michelfrancis Bustillos's user avatar
26 votes
11 answers
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 ...
daniero's user avatar
  • 18.6k
23 votes
20 answers
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 ...
xnor's user avatar
  • 145k
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 ...
Thomas Dignan's user avatar
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 ...
SuperJedi224's user avatar
26 votes
29 answers
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 ...
Nathan Merrill's user avatar
4 votes
11 answers
2k views

Implement a sorting algorithm with no change. [closed]

In this task, you have to write a program, that reads a string with a length of up to 1024 characters and prints it out sorted. In order to make an interesting task from a boring one, I set a few ...
FUZxxl's user avatar
  • 10.1k
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 ...
Nathan Merrill's user avatar
32 votes
2 answers
7k 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 ...
Ronald's user avatar
  • 431
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 ...
C. K. Young's user avatar
  • 4,937
17 votes
16 answers
2k views

City names game

If you like, write a program which sorts cities according to the rules of city name game. Each name of the city should start from the last letter in the previous city name. E.g. ...
defhlt's user avatar
  • 1,778
7 votes
3 answers
559 views

Swaps to Sort an Array

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
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 ...
Martin Ender's user avatar
86 votes
29 answers
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. ...
Trebuchette's user avatar
  • 1,702
42 votes
24 answers
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 ...
Lord Farquaad's user avatar
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 ...
Hand-E-Food's user avatar
  • 8,750
37 votes
39 answers
3k 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 ...
Adám's user avatar
  • 30.4k
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 ...
Adám's user avatar
  • 30.4k
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 ...
caird coinheringaahin g's user avatar
26 votes
33 answers
11k 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 ...
ircmaxell's user avatar
  • 919
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 (...
Stewie Griffin's user avatar
22 votes
29 answers
3k views

Rank a list of integers

You're given a non-empty list of positive integers, e.g. [6 2 9 7 2 6 5 3 3 4] You should rank these numbers by their value, but as is usual in leaderboards, if ...
Martin Ender's user avatar
21 votes
11 answers
2k views

Sort of numbers

Within the recesses of Unicode characters, there exists a Unicode block of (currently) 63 characters named "Number Forms", which consists of characters that have numerical values such as the roman ...
Jo King's user avatar
  • 47.2k
19 votes
13 answers
3k views

Bubble sorting in progress

Create a function or program that takes two inputs: A list of integers that shall be sorted (less than 20 elements) A positive integer, N, saying how many ...
Stewie Griffin's user avatar
18 votes
3 answers
733 views

Burrows, Wheeler and Back

Background The Burrows–Wheeler transform (BWT) is a reversible permutation of the characters of a string that results in large runs of similar characters for certain types of strings such as plain ...
Dennis's user avatar
  • 210k
18 votes
14 answers
1k views

The Strange Unsorting Machine for Nefarious Purposes

Good Evening Golfers! Your challenge is to completely unsort a series of numbers. Input Exactly 100 integers will be fed to your program. Your program may accept the input either as a file, or via ...
lochok's user avatar
  • 3,189
15 votes
41 answers
4k views

Shortest Program to Sort a List of numbers into Odd and Even Category

In the New Modern Times, when Charlie Chaplin encounters a computer, he is employed in the sorting Yard, as a validator to determine if the workers are correctly sorting the items. The Items in ...
Abhijit's user avatar
  • 2,901
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 ...
user avatar
9 votes
23 answers
2k views

Advent Challenge 5: Move the presents to the transport docks!

<< Prev Next >> Thanks to the PPCG community, Santa has managed to remanufacture all of his presents and after the assembly line, the presents are now ready to be moved into the transport docks!...
hyper-neutrino's user avatar
  • 42.4k
8 votes
6 answers
3k views

Sort all the pixels of an image by the number of occurrences

Input The name of a file in the raster graphics format of your choice. The chosen format must support at least 8 bits per channel and 3 channels. Output A file in the same format, with the same ...
EMBLEM's user avatar
  • 2,381
1 vote
12 answers
4k views

Sort an array only using recursion [closed]

Your task is to write a function that sorts an array. Your function: Cannot call other functions (excluding itself, of course); Must receive an array of integers as input, and it must return the ...
Vereos's user avatar
  • 4,431
-3 votes
4 answers
421 views

The Golf (re)Sort [closed]

The Golf (re)Sort Everyone is sorting arrays these days, and so do we at our day jobs. But we like to go out golfing. When we do, we sure like to take our time. It is important to have a stress free ...
Automatico's user avatar
59 votes
18 answers
4k 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 ...
Mr. Xcoder's user avatar
  • 42.4k
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 ...
hyper-neutrino's user avatar
  • 42.4k
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 ...
PhiNotPi's user avatar
  • 29.1k
34 votes
22 answers
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+...
Chris_Rands's user avatar
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 ...
maxb's user avatar
  • 6,897
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 ...
TheLethalCoder's user avatar
30 votes
8 answers
3k 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 ...
Laikoni's user avatar
  • 26.2k
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 ...
Doggo's user avatar
  • 465
26 votes
19 answers
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 ...
maxb's user avatar
  • 6,897
26 votes
7 answers
3k views

Fix my FizzBuzz

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
  • 38.8k
24 votes
10 answers
2k views

Old Spanish alphabetical order

Before 1994, Spanish dictionaries used alphabetical order with a peculiarity: digraphs ll and ch were considered as if they were ...
Luis Mendo's user avatar
  • 105k
23 votes
3 answers
964 views

Sort these bond ratings

The task The credit rating agencies assign ratings to bonds according to the credit-worthiness of the issuer, and the "Big Three" credit rating agencies use a similar (though not identical) tiered ...
Paul's user avatar
  • 333
23 votes
29 answers
1k views

Reverse odd runs

Inspiration. Task Reverse runs of odd numbers in a given list of 2 to 215 non-negative integers. Examples 0 1 → 0 1 ...
Adám's user avatar
  • 30.4k
22 votes
11 answers
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 ...
lynn's user avatar
  • 68.8k
22 votes
8 answers
1k views

Crazy Librarian's Amazing Sorting System

It's back to school season! So for a part-time job, you're helping out at the school's library. The problem is, the head librarian has never even heard the words "Dewey Decimal," let alone implemented ...
AdmBorkBork's user avatar
  • 43.4k
21 votes
3 answers
2k views

Hungarian alphabetical order

For those who wish a lot more challenge then the old Spanish alphabetical order, let's take a look at how the Hungarian alphabet is ordered. a, á, b, c, cs, d, dz, dzs, e, é, f, g, gy, h, i, í, j, k, ...
vsz's user avatar
  • 8,109
21 votes
29 answers
5k views

Sort-a-number. Sorta [closed]

Inspired by the ill-fated sorting-a-numbers-digits-without-using-an-array, but I thought it made a better code golf than SO question. Given a positive integer, sort the digits in that integer. ...
kojiro's user avatar
  • 738
21 votes
2 answers
1k views

Book Stack Sort

When stacking books you usually want to put the largest ones at the bottom and the smallest ones at the top. However, my latent OCD makes me feel very uneasy if I've got two books where one is shorter ...
Martin Ender's user avatar