# Questions tagged [sorting]

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

964 questions
Filter by
Sorted by
Tagged with
796 views

### Milking order compatible with maximum number of observations

I'm using some old USA Computing Olympiad (USACO) problems to help teach me. This is the "Milking order" problem: Farmer John has $N$ cows ($1≤N≤10^5$), numbered $1…N$. He has made $M$ ...
154 views

### BubbleSort - checking if bubblesort is decreasing the iterations through the array

What I'm hoping my code is doing is instead of making 99 comparisons on every pass of a 100 element array, it will now make 98 comparisons on the second pass, 97 on the third pass and so on and thus ...
322 views

### Mergesort linked list in C++

My teacher gave us an exercise. We have to sort as quickly as possible. The fastest one gets a reward. On my own system, I am sorting 125000 words within 1.2 seconds. On my teacher's system that is 0....
158 views

### Check whether array is sorted recursively

I'm trying to implement isSorted method checking whether given array is sorted recursively. I've written two types, one is similar to merge sort logic, the another ...
389 views

### Pancake sort inteview using java

Given an array of integers arr: Write a function flip(arr, k) that reverses the order of the first k elements in the array <...
79 views

### Compares two Log Lines

I am trying to compare two Log lines based on some conditions, in order to sort them: Code: ...
402 views

### Multi-threaded in-place Mergesort on Java

What is an efficient way to implement the Mergesort algorithm in Java such that it meets the following criteria: Must be multi-threaded. Must retain the Mergesort time complexities. Must be in-place ...
445 views

### compareTo method for names

I'm implementing the compareTo method to set the natural order of an object. My object has three fields that make up a name. fName, lName, middleInitial. The natural ordering should be by lName, fName,...
632 views

### Implement Insertion sort in Python

Task : Implement Insertion Sort in Python. ...
86 views

### Selection and Bubble sorts

Just to enjoy, I want to implement selection and bubble sorts algorithms without looking any implementation by my own efforts 💪💪. Is there any redundant/lack part(s)? After improved their ...
678 views

### Python Bubble sort code

I have written the below bubble sort code in Python. ...
191 views

### Angular, sort objects by their keys

I had to sort a list of objects - {name:'name', email:'email} by their keys, ex. by email, so they all be rendered with ...
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 ...
131 views

### Composable by-member comparison

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

### Sorting by checking 2 distance away elements in a list, and check the method's accuracy

This code works as follows : It accepts number of test case T. For each test case, it accepts length of sequence of integers N and the sequence (or list) itself L. The goal is to sort the list by ...
355 views

### Shortest distance for points in a plane

I was wondering how I can make this code more pythonic. More professonal-looking and more readable. The code runs perfectly fine. ...
390 views

### Sorting items in list based on position

I wrote code which sorts items, based on parameters (IconConfiguration). I have a map of items which have index and item so basically a predefined list on which index item should be, and maxPositions ...
269 views

### Optimizing priority queue streaming algorithm in C++

This is a sorting algorithm that I wrote for finding the maximum $n$ elements at any time in a stream of IDs and integers with unknown length. I primarily do my coding in Python, so I'm not too ...
71 views

### Mergesort, instrumented for counting reads and writes

I have just finished an implementation of mergesort, and I was wondering if I can further reduce the amount of reads and writes. I have looked at it for a while, and to my eyes, I cannot see anything ...
455 views

### Nearest Neighbor Algorithm (general nearest, forward-nearest, or backward-nearest)

I am trying to write an algorithm that can find user-specified nearest neighbors. By user-specified, I mean that the user can specify whether it's a general nearest neighbor, a forward-nearest ...
2k views

### Bubble sort using user inputted numbers and size in MIPS assembly

I made this code to take user input, making a specific-sized array and printing user-inputted numbers after being sorted. It works well but I was wondering if there was a way to "shrink" it down. It ...
524 views

### “Almost Sorted” coding challenge

The "Almost Sorted" problem from HackerRank: Given an array with $n$ elements, can you sort this array in ascending order using only one of the following operations? Swap two elements. ...
734 views

### Sort an iterable in Python 3.6

I was looking through some popular questions on SO, and one of them was How do I sort a dictionary by value?. Because Python 3.6 now gives us dicts that maintain ...
243 views

### Sorting a 2-dimensional array with counting sort

The task I'm solving is: Sort a list by its keys Print out the corresponding values on one line separated by whitespace Replace the values of the first half of the input to "-" Use Counting Sort The ...
95 views

### What's a cleaner way to pass a function an array of values and then sort my object by those values?

The working code: ...
1k 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 ...
46 views

### Create numerical-key array from existing string-key array, with a given string-key as first item

I have an array of available languages for my website, with a short ISO country code as the key for each language. I need to create a new array that contains the same info, but with numerical keys and ...
208 views

### Counting adjacent swaps to sort an array with 3 different values

This is the Kindergarten Excursion problem from kattis.com: The kindergarten teachers had finally managed to get all the kids in a line for the walk to the bus station and the weekly excursion. ...
95 views

### Sorting a list of IComparables

I have a DateTime class (my own, not System.DateTime) which implements the IComparable ...
175 views

### Pre Java 8 a lot of Comparators implementation

Looking for critiques on my implementation of a list of countries that offers many ways to sort it in a useful and easy way for a client. The main concern is about the design of the enum that holds ...
99 views

### Custom sorting algo / optimized bubble sort

I'm a novice and started diving into data structures and algorithms recently. I was taught bubble sort recently and I found that the original bubble sort algorithm is so inefficient, so I wrote a ...
65 views

### Copying and separating data by Excel Field

I created this code that's meant to pull a specific report that's been saved in a folder clean up (delete) the data and add new titles, divide the information up by one of the fields, then copy the ...
255 views

### Merge two unsorted array of numbers into sorted array using divide and conquer

I have done an implementation of merging two unsorted array of numbers into single sorted array. I have used the concept of divide an conquer. The code is as follows ...
114 views

### Quick sort with a predicate

I want to pass a predicate into quick sort. The issue I'm running into is that the predicate must have two arguments, the pivot and the element of the list's tail. As far as I know though, predicates ...
798 views

### Merge the two sorted array of numbers into single array

I have written a code were I pass two sorted array of numbers from command line, and out is one array with numbers sorted. the code is as follows, ...
866 views

### Implement bubble sort in Python

I have written the following code for bubble sort. How can it be optimised? ...
784 views

### Merge sort a given list in Python

Problem: Implement merge sort in python. Don’t use Python’s built in sort or sorted. Assume your inputs will be sufficient for the memory you have. How should I make this code better? ...
133 views

I'm working on a task from the one of Coursera programming courses. The purpose is to estimate the amount of inversions that are required to sort the array of numbers. Also I'm trying to learn some ...
231 views

### Selection Sort in python

I'm following the pseudocode as given as a part of an excercise in CLRS. Consider sorting $n$ numbers stored in array $A$ by first finding the smallest element of $A$ and exchanging it with ...
394 views

### Generic Merge Sort in C++

I wanted to implement merge sort in a generic way. I was not sure whether I should use the iterator or the container for the template type. I did not find a way to infer the container type using ...
65 views

### Fastest way to find if a lot numbers are in multiple intervals

So I have a task. I've been given $n$ numbers and $m$ intervals and I need to figure out how many numbers are in the $m$ $i$-th interval. I've written some code with a complexity of \\$O(n \...
70 views

### Searching and sorting ranges in excel with toggle buttons

I have a spreadsheet containing ±100 named ranges (5 rows, 15 colums) which are sorted based on time. My plan was to make toggle buttons to hide or unhide named ranges when they contain a certain ...
16k views

### Sorting dates in this format (DD-MM-YYYY)

Is this the correct and an elegant way to sort the dates in the format (DD-MM-YYYY) in ascending order? ...
373 views

### Applies the Largest Remainder Method to a number property in a list of objects

I need to round BigDecimal numbers according to the Largest Remainder Method (LRM). Please reference this SO post with this answer. Here's my version of the algorithm in Java: ...
111 views

### Sorting Exclusive of Specified Indexes in List

To further generalize the algorithm (past the doc string): Given a list E(1) E(3) E(2) E(4) _ E(0) _ where E(N + 1) is ahead of <...
2k views

### Recursive sorting algorithm

I wrote a recursive sorting algorithm and was wondering what would be the run time complexity of this. The bulk of the work is done in the push_up function, which I ...
1k views

### Java generic sorting class

I'm writing a class that will sort multiple types {arrays, arraylist, linkedlist} but I see there's a lot of code duplication. For instance, method to swap in list, and in arrays, method to sort list, ...
3k views

I have been doing this challenge in which I need to determine and return one's (i.e. alice) rank on a leaderboard by comparing her scores to the others'. I made ...