# Questions tagged [sorting]

Sorting is the process of applying some order to a collection of items.

964 questions
Filter by
Sorted by
Tagged with
1k views

### Sort int array by frequency then value

Problem Take an array of ints as input and run a special sorting algorithm on it. This algorithm must first sort by int frequency, less frequent first, then sort by value. For example ...
426 views

### Sorting cards by suit and value using Shell Sort

The question description: put a deck of cards in order by suit (in the order spades, heart, clubs, diamonds) and by rank within each suit, with the restriction that the card must be laid out face down ...
695 views

### Writing a class that represents a sorted list of filenames

I have a question: Write a class that represents a sorted list of filenames. The filenames are sorted by most recently added first. Duplicates are not allowed. And I wrote this solution: ...
870 views

### sorting a map by converting it to vector

I have a map that I want to print out sorted by value, I convert it to vector and sort the vector. Is this code correct? ...
36 views

### Sort ascending/descending functionally

I've been trying to get into functional programming with javascript and have tried to make some experiments with currying and function composition. What I've played with so far is a sorter function ...
148 views

### Bidirectional bubble sort using goto [closed]

I am aware of Dijkstra's paper on why goto is harmful, but I thought it would still be fun to make a bidirectional bubble sort using ...
702 views

### Insert a number in a sorted list and return the list with the number at the correct index

I feel like the title is a bit wordy. What I have defined here is a function that takes two parameters: a list that contains valued sorted from smallest to biggest a number to insert at the right ...
66 views

### Selection and Insertion sorts from scratch in Java

I am trying to find a good, basic way to make selection and insertion sorts so that I can manipulate them for other sorting techniques. How do these look? Is there a simpler way to write them? ...
118 views

### Evaluation of a variation of quick-sort (pivot-selection)

Here is a variation of quick-sort where-in the pivot selection is based on calculating the average of the values of the highest and lowest numbers. ...
422 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 ...
99 views

### Verify that each named sequence is strictly increasing

The master launch sequence consists of several independent sequences for different systems. Your goal is to verify that all the individual system sequences are in strictly increasing order. In other ...
131 views

### Merge Sort code in C

Below is the code of Merge Sort for sorting an integer array in ascending order of values written by me. By looking at the code, I am thinking I can make the code more compact and secure, but not ...
197 views

### Sorting an unsorted array

This is a very simple sorting logic that I have written and it's working also. But Is there anything wrong in the traversing of the array? Or the code is not optimized? The code is simply declaring an ...
67 views

### Preparing tabular data to be sent as json

I query a database and pass the data for processing as a DataTable. After all computations (grouping, filtering, formatting etc.) I need to send it to a Web-Service ...
56 views

### Quicksort implementation with pivotal calculated as middle element

I read quick sort algorhitm and implemented in like this: ...
6k views

### Minimum number of swaps required to sort the array in ascending order

Here is my problem statement. An excerpt: You are given an unordered array consisting of consecutive integers ∈ [1, 2, 3, ..., n] without any duplicates. You are ...
138 views

### Computing word-frequency-table

I got a word count assignment and the requirements are shown below: INPUT The input contains words in several lines and in each line there could be more than one word. The input only ...
35 views

### Return a data frame with all possible pairs/triplets/quadruplets of a value column based on two identifiers, and order the value columns

Consider the following dummy dataset: ...
67 views

### Speed up sorting algorithm in R: make one column “smaller” than the other

Consider the following sorting algorithm: ...
154 views

### Bubble and Cocktail sort

Lately, I've been developing sorting algorithms as a kind of exercise to make better code and I've come accross something curious, even though bubble and cocktail sort are known to perform horribly, I ...
366 views

### Quicksort Without Recursion

Am teaching myself about sorting techniques and trying my hand at a quicksort implementation. I found a lot of examples based on recursion but didn't want to go that route in a language that doesn't ...
183 views

### Sorting table using multiple columns [closed]

I want to sort entries in a given list on Entry column in most efficient way. Here is how my entries look: ...
140 views

### Sorting function from a React app to display JSON data in a sortable table

I created this simple react application for a coding challenge, which is described in the linked README file. Basically, I created the components and helper logic to display dynamic JSON data in a ...
109 views

### Sorting CSV table in Powershell

I'm wondering if some PS gurus can advise on whether my method for sorting and recombining some data fields by using an array of PSCustomObjects is as efficient as it might be. I'm importing a CSV ...
1k views

### Find the intervals which have a non-empty intersection with a given interval

The problem is the following: I have a list intervals which consists of tuples of the form (start, end) [with ...
484 views

### C++ - Radix sort implementation

I have the following radix sort implementation: ...
176 views

### Deficient Numbers

I have found the following interesting challenge on the web: Deficient Numbers A number is considered deficient if the sum of its factors is less than twice that number. For example: 10 is a ...
2k views

### Sort characters in a Python string by frequency

I wrote a solution to the leetcode problem Sort characters by frequency and the solution passes 34/35 test cases. On the last test case, there is a "time limit exceeded" error with an input string of ...
73 views

### ContactsList implementation in Java

I created a ContactsList class in Java that holds Person objects. I consider this is a side project for my GitHub profile. Any ...
73 views

### Sort multidimensional array based on another one containg the desired order

I have an array like this: ...
1k views

### Sorting three ways

I have implemented function templates that take a begin and end iterator as well as an optional comparator for user-defined types. It was my intention to mimic the usage of ...
263 views

### React widget to display, sort and search remote data

I've created a React widget that pulls data on crypto-token pairs from a remote API and makes that data sortable by headers and searchable. You can view a demo of it here. Is this a maintainable way ...
203 views

### C++2a comparison-based topological sort algorithm

Here's my attempt at a C++2a Standard Library–friendly "topological sort" algorithm. There are several areas of interest here: The algorithm is comparison-based and in-place, just like ...
2k views

### Minimum swaps algorithm terminated due to timeout

I have been trying to solve this question. Given an unordered array consisting of consecutive integers [1, 2, 3, …, n], find the minimum number of two-element swaps to sort the array. I was able ...
2k views

### Sort an array of 0s, 1s and 2s in Java

Write a program to sort an array of 0's,1's and 2's in ascending order. Input: The first line contains an integer 'T' denoting the total number of test cases. In each test cases, First ...
447 views

### Sort digits in Python

Given an integer, sort the digits in ascending order and return the new integer. Ignore leading zeros. Parameters: Input: num {Integer} Output: {Integer} ...
75 views

### Sorting software version list using Python 2.7 (no modules)

I am participating in a challenge and one of the challenges was to write a script to sort a list of software versions (ex: 1.0, 1.3.2, 12, 1.3.0). Here is the my solution: ...
38 views

### Is there a better way to sort this items?

I already have my code working, but I believe that could be improved. This is basically the main part, where I re-order a list of items; ...
96 views

### String comparator as a lambda

I am writing a Comparator for my TreeMap. It looks like this: ...
103 views

### Counting sort in C, revised

This is a revised follow-up to this question. I have implemented most of the suggestions in the accepted answer. I am interested to see what I could improve in my code and what I could do to make it ...
167 views

### Counting sort in C

EDIT: I have posted a follow-up to this question. I have implemented counting sort in C. This program takes its input as integers from command line arguments, sorts the integers with counting sort, ...
855 views

### Sort string's characters into alphabetic order

Any improvements that I can make with this code? Also, is there another way to convert a vector to a string variable without using a for ranged-based loop? From the posts I've seen for converting, I ...
2k views

The following code is part of my practice in implementing algorithms in Haskell. I'm aware that bubble sort is a bad choice for sorting sequences in real applications. ...
550 views

### C++ code to read, sort, and write data

I had written code in C++ to create transformation filters to get input, sort (custom_sorting and existing sorting routine) and write the output to stdin and file. When I presented it to my teacher, ...
145 views

### Checking whether two lists contain the same elements

I have two lists of numbers: $$\{i_1, i_2, i_3,i_4\}$$ and $$\{j_1,j_2,j_3,j_4\}$$ The problem I want to solve is: Inside a loop, if the following two conditions are satisfied, then it will do ...
56 views

### Mergesort in Python optimization

I am currently writing mergesort in Python and have implemented it to work. I have noticed, though, that it is very slow compared to the standard sorted() method. I understand it will always be faster ...
110 views

### Implementing and benchmarking Radix Sort on unsigned integers

Introduction I implemented a restricted version of the sorting algorithm, the one that works only with unsigned integers. I wanted to try using ...
208 views

### Print strings in order with dynamic arrays

I accomplished the following task to practice allocating on the heap. reading words from stdin sorting them in lexicographical order printing the sorted words to stdout To accomplish this is ...