# Questions tagged [sorting]

Use this tag when arranging items in order is the main focus of the code.

1,158 questions
Filter by
Sorted by
Tagged with
226k views

### Grouping elements in array by multiple properties

During work, I was given this task: to group elements with similar properties in the array. In general, the problem is as follows: ...
• 833
177k views

### Given an array of integers, return the smallest positive integer not in it

Write a function: function solution(A); that, given an array A of N integers, returns ...
11k views

### Sorting millions of integers

Last Friday I was hit with a sorting interview question that I never really had to deal with. Develop a your own sorting algorithm. It cannot use any other Classes for help. It needs to ...
• 593
10k views

### Efficiently squaring each element in a sorted array, keeping it sorted

The other day I was asked the following question during an interview: Given a sorted array, how would you square each element of this array, while keeping it sorted? I froze up and wrote a ...
• 551
8k views

### Quicksort implementation seems too slow with large data

I've written a program to compare different sorting algorithms with the array size being 10, 1,000, 100,000, 1,000,000 and 10,000,000. I, of course, expect Insertion to win out on 10 and merge heap ...
• 471
7k views

### "How are you spending your time on the computer?"

I've made a Windows Forms application to track all of the processes running on my machine and it also saves the time an application is "active", an active application is the one that is on focus ...
• 8,458
4k views

### Sorting strings by length - functional Python

I'm trying to port this little F# snippet while staying pythonic: ...
17k views

### Sorting large 1GB file with 100 millions numbers using merge sort

My task is to sort 1GB file with 100 million numbers using merge sort without recursion. My idea is to split file into 4 pieces, then to 2 and at the end to one file. ...
• 459
80k views

### Java Implementation of Quick Sort

This is my implementation of quicksort (algorithm taken from Cormen book). This is an in place implementation. Please let us know issues with this or any ideas to make it better. It performs at logN. ...
• 433
16k views

### Ford-Johnson merge-insertion sort

The Ford-Johnson algorithm, also known as merge-insertion sort (the name was probably given by Knuth) is an in-place sorting algorithm designed to perform as few comparisons as possible to sort a ...
• 20k
3k views

...
• 2,342
6k views

### Sorting algorithms - Bubble sort

For pure practice purposes, I started implementing different sorting algorithms in modern C++ in a standard library style way (i.e. using templates on iterators). This is my version of bubble sort. ...
• 5,228
21k views

### QuickSort C# Implementation

It has been a while since I've implemented a QuickSort and I wanted to write it down like I remembered. ...
• 303
660 views

### QuickMergesort — The power of internal buffering

Amongst the lesser-known unstable sorting algorithms is the QuickXsort family of algorithms, as described in QuickXsort: Efficient Sorting with n log n - 1.399n +o(n) Comparisons on Average by Stefan ...
• 20k
2k views

### Sorting algorithm visualizer in C++ and SDL2

I finished a sorting algorithm visualizer (for now only uses bubble sort) and this is the code. I'd like some suggestions to improve the memory usage or time elapsed. Built with Cmake and Ninja on ...
• 889
10k views

### Insertion Sorting in C++

From Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, ...
3k views

### Pancake sort interview with Python

I got asked this question during my interview. You can find information about this question here. Pancake Sort Given an array of integers arr: Write a function flip(arr, k) that reverses ...
• 2,469
143k views

### Finding repeating numbers in an array

I want to search through an array of n numbers and find the numbers that are repeated. So far I have this code, which does the job, but I find it to be a rather cumbersome method, but I can't seem to ...
• 273
142k views

### Sorting an HTML table with JavaScript

The following code sorts an HTML table with JavaScript (without using any external libraries like jQuery). Are there any shortcomings or possible improvements I could make? ...
52k views

### Sort three numbers using only if-statements

Beginner here, trying to make a small program that sorts three numbers from smallest to largest only by using ifs. Any thoughts on how to improve this? ...
• 163
5k views

### Least significant digit (LSD) radix sort of strings

I am learning string algorithms and wrote this C example based on a Java example I found online. Any feedback relating to style, code clarity, whether it is generic enough to be re-used, interface, ...
• 2,449
517 views

### Exact sort - sorting with few move operations

A while ago, I found a somewhat interesting sorting algorithm named Exact-Sort, which is introduced with the following somewhat bold claim: No sort algorithm can sort an array with less position ...
• 20k
9k views

### Sorting 10,000 unique randomly-generated numbers

I have to write this program in java. Write a program name sorting.java that will use an array to store 10,000 randomly generated numbers (ranging from 1 to 10,000 no repeat number). Here is what ...
3k views

### Count word frequencies, and print them most-frequent first

I'm writing a comparison of a simple word-counting program in various languages. The task is to count the frequencies of unique, space-separated words in the input, and output the results most ...
• 629
1k views

### Is this C++ quicksort program okay?

I wrote a quicksort program in C++. Is it okay to implement it this way? Am I using pointers correctly? Is my style okay? Any ideas to speed it up or save memory? ...
• 253
661 views

### Allowing the user to alphabetize or randomize the contents of a text file

As a newbie programmer, I decided to try my hand at making a program whose function actually might be useful to someone, somewhere. This utility takes a text file, reads it out to the user, displays ...
• 391
4k views

### Merge n sorted iterators

Over the weekend I was curious about efficiently merging multiple sorted iterators together, and for them to be in sorted order. This is quite like a challenge on HackerRank: You’re given the ...
• 43.3k
523 views

### Melsort — Sorting with encroaching lists

For the nth time already I come back with yet another alien sorting algorithm. Today I will present Melsort, implemented from the description I found in the paper Encroaching Lists as a Measure of ...
• 20k
502 views

### Building unusual IComparer<T> from expressions

I've needed a couple of very special comparers recenty and didn't want to implement each one of them every time so I created a builder and a couple of supporting classes that do that for me. Example ...
• 44.3k
3k views

### Shell Sort, Insertion Sort, Bubble Sort, Selection Sort Algorithms (Python)

There is a follow-up question available: shell-sort-insertion-sort-bubble-sort-selection-sort-algorithms-python. Selection Sort The selection sort algorithm sorts a list (array) by finding the ...
• 3,517
53k views

### Creating a list containing the rank of the elements in the original list

I don't really know how to explain what I'm looking for in a way that makes sense, but here goes: Say I have a list $$L=(4,7,9,10,6,11,3)$$ What I want to produce is a corresponding list  K = (1,3,...
• 243
962 views

### A Lisp version of the Schwartzian transform

Randal Schwartz stole Lisp's decorate-sort-undecorate idiom when he came up with what we'd later call the Schwartzian transform. All of my Lisp knowledge disappeared through nine half lives, but I ...
• 271
2k views

### IP sorting program

The purpose of this little program is to: Separate IPv4 addresses and IPv6 addresses Will be able to sort them even if the ...
• 375
8k views

### Parallel Merge Sort

Parallism is achieved by using a simple call to tbb::parallel_invoke(). However I can't break a speedup factor of x2 (after ~3-4 parallel HW threads), although 8 ...
• 564
6k views

### Deck of cards with shuffle and sort functionality

Going through Head First C#'s Chapter 8: Enums and Collections, I learned about List<T>, as well as IComparable<T> ...
• 20.4k
2k views

### Python3 - merge sort, O(n) space efficiency

Any critique of my implementation of Merge sort would be much appreciated! I tested it using a driver function (shown below), and everything works. However, it still feels unwieldy, I am a beginner ...
325 views

### Vergesort — A runs-adaptive layer to enhance sorting algorithms

More than a full-fledged sorting algorithm, vergesort is a Runs-adaptive algorithm that falls back most of the time to another sorting algorithm to sort a sequence of elements, but is able to identify ...
• 20k
14k views

### Bubble sort algorithm in JavaScript

I'm new to JavaScript (and programming) and taking a course. I was tasked with creating a bubble sort on any array using JavaScript. Of course, since this is an algorithm challenge, no sort method ...
• 123
2k views

### Recursive merge sort in C

Here's my first program in C, which is not my first language. I have been reading Modern C and I attempted this challenge on page 26. The prior pages are pretty much the only exposure I have had to C, ...
• 151
43k views

### Merge Sort an integer array

I've implemented merge sort an integer array. Is it okay to use i,j,k as variable name for looping? Should I change them to more meaningful names? Overall, any further suggestions on this code? ...
• 1,483
33k views

### Number aware string sorting with comparator

I have this class for use in sorting strings such that if strings have a number in the same position it will order the numbers in increasing order. Alphabetical gives: file1 file10 file2 What I'm ...
• 419
580 views

### Attempt at a sorting algorithm

I've been playing around and reading up on some sorting algorithms, and I've decided to try writing my own. It turned out to be quite fast (compared to the others shown in the image below). I'm very ...
• 397
6k views

### Always sorted list

In this list, each newly inserted element is put in its place in order using IComparer<T>. The implementation uses a ...
• 12.4k
9k views

### Return characters in a string in alphabetical order

Problem Take a string and return all input characters in alphabetical order Assume that no numbers or punctuation will be used For the same character, uppercase characters should be returned ...
• 1,845
6k views

### Natural Order Comparator

I've been looking for an implementation of Comparator that provides a "natural order". I found a couple but they were buggy or poorly designed. I wrote my own with ...
3k views

### Bubble Sort in C: Array vs Linked List

A few weeks ago I posted a code review of a linked list so I decided to implement bubble sort on my journey of learning C. Here is the referenced linked list library, though this isn't really what's ...
• 558
583 views

### Forecast maintenance interval for fleet of aircraft

Being new to VBA, and many years removed from college programming courses in Java, I'm seeking opinions about design, implementation, readability and general better practices using VBA to tackle ...
• 221
5k views

### Parallel Quicksort

As my parallel mergesort implementation was very memory dependent, I wanted to write a parallel quicksort one. Here it comes: ...
• 564
198 views

### Composable by-member comparison

Motivation Quite often, I find myself creating types that must be compared memberwise. For example, consider this simple Person: ...
• 75.1k