Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
66 votes
4 answers
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: ...
Saike's user avatar
  • 833
62 votes
10 answers
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 ...
Philip Kirkbride's user avatar
49 votes
3 answers
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 ...
Xjasz's user avatar
  • 593
42 votes
12 answers
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 ...
carbon_ghost's user avatar
39 votes
2 answers
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 ...
Ethan's user avatar
  • 471
38 votes
4 answers
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 ...
Denis's user avatar
  • 8,458
37 votes
5 answers
4k views

Sorting strings by length - functional Python

I'm trying to port this little F# snippet while staying pythonic: ...
Mauricio Scheffer's user avatar
32 votes
10 answers
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. ...
Adrian's user avatar
  • 459
26 votes
5 answers
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. ...
user830818's user avatar
26 votes
1 answer
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 ...
Morwenn's user avatar
  • 20k
26 votes
1 answer
3k views

Merge sort in Scheme

...
C. K. Young's user avatar
  • 2,342
23 votes
5 answers
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. ...
Ben Steffan's user avatar
  • 5,228
20 votes
1 answer
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. ...
Max's user avatar
  • 303
20 votes
1 answer
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 ...
Morwenn's user avatar
  • 20k
19 votes
1 answer
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 ...
Nadpher's user avatar
  • 889
18 votes
3 answers
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, ...
matheussilvapb's user avatar
18 votes
2 answers
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 ...
NinjaG's user avatar
  • 2,469
17 votes
7 answers
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 ...
Hatori Sanso's user avatar
17 votes
5 answers
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? ...
Sharanya Dutta's user avatar
16 votes
4 answers
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? ...
liquid3's user avatar
  • 163
16 votes
4 answers
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, ...
arcomber's user avatar
  • 2,449
16 votes
2 answers
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 ...
Morwenn's user avatar
  • 20k
15 votes
4 answers
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 ...
Fernando Martinez's user avatar
15 votes
3 answers
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 ...
Ben Hoyt's user avatar
  • 629
15 votes
2 answers
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? ...
Kolt's user avatar
  • 253
15 votes
3 answers
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 ...
Gabe Evans's user avatar
15 votes
1 answer
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 ...
Peilonrayz's user avatar
  • 43.3k
15 votes
3 answers
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 ...
Morwenn's user avatar
  • 20k
14 votes
3 answers
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 ...
t3chb0t's user avatar
  • 44.3k
14 votes
4 answers
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 ...
Emma's user avatar
  • 3,517
14 votes
3 answers
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,...
L1meta's user avatar
  • 243
14 votes
2 answers
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 ...
brian d foy's user avatar
14 votes
2 answers
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 ...
Dan's user avatar
  • 375
14 votes
2 answers
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 ...
inf's user avatar
  • 564
13 votes
7 answers
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> ...
Phrancis's user avatar
  • 20.4k
13 votes
3 answers
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 ...
Joseph Gutstadt's user avatar
13 votes
1 answer
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 ...
Morwenn's user avatar
  • 20k
12 votes
5 answers
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 ...
Launa's user avatar
  • 123
12 votes
4 answers
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, ...
user14464173's user avatar
12 votes
4 answers
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? ...
Arun Prakash's user avatar
  • 1,483
12 votes
4 answers
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 ...
pbible's user avatar
  • 419
12 votes
2 answers
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 ...
BjarkeCK's user avatar
  • 397
12 votes
2 answers
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 ...
IEatBagels's user avatar
  • 12.4k
12 votes
3 answers
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 ...
Jae Bradley's user avatar
  • 1,845
12 votes
3 answers
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 ...
NonlinearFruit's user avatar
12 votes
1 answer
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 ...
dysruption's user avatar
12 votes
2 answers
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 ...
Zephyr Mays's user avatar
12 votes
2 answers
5k views

Parallel Quicksort

As my parallel mergesort implementation was very memory dependent, I wanted to write a parallel quicksort one. Here it comes: ...
inf's user avatar
  • 564
12 votes
1 answer
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: ...
Toby Speight's user avatar
  • 75.1k
12 votes
1 answer
2k views

Generic radix sort

This started out with my answer to Radix Sort on an Array of Strings?. Since I intend to write a generic radix sort for my own purposes anyway, I continued a little bit, and here is a version tested ...
iavr's user avatar
  • 2,284

1
2 3 4 5
24