Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
6
votes
2answers
8k views

Stable custom alphabetical order using List<T>.Sort

Background This is a problem from a programming contest. The Sharonians of planet Sharon, at the far end of our galaxy, have discovered various samples of English text from our electronic ...
6
votes
5answers
2k views

Takes in two sorted lists and outputs a sorted list that is their union

Write a function that takes in two sorted lists and outputs a sorted list that is their union Could anyone help me to improve this code and make it more user-friendly? We are not supposed to use ...
6
votes
2answers
585 views

Shell sort seems inefficient

I am testing various sorting algorithms. Right now I am testing shell sort, insertion sort and selection sort. I ran all three algorithms on a randomly-generated list of 1000 integers. The selection ...
6
votes
2answers
569 views

Bubble sort with counting swaps and comparsions

I have bubble sort code below, which has to count swaps and comparisons of elements. How can it be made more elegant and clear, if it is possible? I think using lots of tuples is not the best way to ...
6
votes
2answers
10k views

Sort array of objects with hierarchy by hierarchy and name

I have an array of nested objects: ...
6
votes
2answers
10k views

Sorting an array in alphabetical order

I'm looking to get some feedback on this program I wrote - ways I could make it simpler or best practices methods. Chapter 7: Let's write a program which asks us to type in as many words as we want (...
6
votes
2answers
436 views

Sorted grep in Haskell

This program implements a sorted grep, that is, a specialized version of grep for sorted files. It uses binary search for the lines of a file that begin with a ...
6
votes
3answers
1k views

Bubble sort algorithm in Java

I have written this code which basically sorts the array using bubble sort, however, I feel it is not very readable. Please let me know me how I can improve this code. ...
6
votes
3answers
3k views

Quicksort for strings in Java

I have this implementation of Quicksort for strings. The algorithm sorts the requested range by first character, then by second, third, and so on. (Please, do not confuse this with radix sort; it is ...
6
votes
2answers
799 views

Ascending Order Algorithm

I wrote a simple function to sort an array of integers in ascending order. Here is the code - ...
6
votes
2answers
10k views

Merge sorting linked lists

This works as expected and I want to get this code reviewed so that I can improve my coding ability. I am concerned about readability and the quality of the code ...
6
votes
2answers
1k views

Testing quicksort user input

I am testing a quicksort implementation. Arrays of random size with random data are passed to quicksort and the return time is averaged out. The user is prompted to enter values for minimum length of ...
6
votes
2answers
433 views

Lexicographically largest string from allowed swaps

Problem: Given a string str and array of pairs that indicates which indices in the string can be swapped, return the lexicographically largest string that results from doing the allowed swaps. You ...
6
votes
1answer
285 views

Natural merge sort - follow-up

The previous iteration at Natural merge sort Now my code relies on C++14 and looks like: natural_merge_sort.h: ...
6
votes
1answer
3k views

Adding ranks to objects in an array

This works, and I personally find it elegant and kind of "neat," but then I worry it's a bit too tricky. Comments welcome: ...
6
votes
1answer
218 views

Merge sort could work 10 times faster

I am implementing merge sort in Java and I have a performance problem in this method: ...
6
votes
2answers
284 views

Quicksort with Insertion Sort and Improved Pivot Selection

Here's a Quicksort I had fun writing and improving, so I thought I'd post it here. In my (brief) testing it's about 15% to 20% faster than Java's Arrays.sort(). ...
6
votes
2answers
3k 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 ...
6
votes
2answers
674 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 ...
6
votes
1answer
2k views

Sorting ListView Columns

I use these codes to re-order columns in a ListView in ascending or descending order: ...
6
votes
1answer
108 views

Merge sort practice

Never written a merge sort for huge files before. So after this mornings question I though I would give it a go. ...
6
votes
1answer
279 views

Speech Recognition Part 2: Classifying Data

Now that I have generated training data, I need to classify each example with a label to train a TensorFlow neural net (first building a suitable dataset). To streamline the process, I wrote this ...
6
votes
1answer
274 views

Comb Sort in VBA

Playing off Robust Bubble Sort in VBA and as suggested by @Henrik, I took a look at comb sort and tried to create an algorithm based on the documentation solely on Wikipedia. Basically, the first ...
6
votes
3answers
2k views

Bubble Sort in Objective-C

Following is Objective-C method implementation I did for one of the most simplest sorting algorithms, Bubble Sort to sort an array of integers. Note:- I have defined it as a static method in the ...
6
votes
1answer
112 views

Bash command helper in C++

I'm writing a program to help with remembering complex bash commands. On invoking the program, it asks for a description of the desired operation, e.g., "increase volume" or "find orphaned packages", ...
6
votes
1answer
477 views

Timeout Error in Fraudulent Activity Notification HackerRank

I am solving this problem: Fraudulent Activity Notifications on HackerRank. I am done with my code and is working, but it is inefficient as well for very large inputs. I don't know but after all ...
6
votes
1answer
77 views

Sorting words by frequency

I'm doing a simple task in Rust after reading the Rust book: read a text file split it at whitespace sanitize words by removing non-letter characters sort the words by their frequency of occurrence ...
6
votes
2answers
90 views

Ensuring Sequence of Events in an Array

The Problem I'm asking to see if there are methodically better solutions to this problem. The situation is as follows, I am receiving an array of events, they are marked by timestamp and there is a ...
6
votes
1answer
185 views

Sort all the characters according to their brightness

I wrote a little fractal noise generator the other day, and to test it I wanted to display the dark values with a darker character and the bright values with bright characters (I use a dark background ...
6
votes
1answer
143 views

Multiprocess Bogosort

I only program by hobby, and I'm trying to improve with Python, so along with practicing coding I decided to venture into multiprocessing. This is a command line and will sort a variable number of ...
6
votes
1answer
10k views

Counting inversions

Inversion Count for an array indicates – how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion ...
6
votes
1answer
244 views

Animation of sorting algorithms

I have created a visualisation of sorting algorithms using tkinter and matplotlib. Please let me know what you think and if you have any suggestions or find errors. The aim of this code is to create ...
6
votes
1answer
222 views

Counting sort implementation in C

I've just implemented counting sort in C and would like to hear your opinions on it. As I'm usually being rather picky when rating other people's code, please go ahead and be picky, too -- I will try ...
6
votes
1answer
1k views

Topological sort using recursive DFS

I am currently learning Graph algorithms by solving questions on online judges. The below code is for implementing Topological sort, using recursive DFS. Also, it is my first time with C++ STL. ...
6
votes
3answers
227 views

Merge Sort implementation in Java

Can you check my merge sort implementation? From all of my testing it seems like it works, but I just wanted to make sure I am doing it as good as possible, and it couldn't be optimized any further. ...
6
votes
1answer
342 views

Algorithm for finding inversions in Merge Sort

Please help in code reviews for readability, optimizing it, functionality etc. ...
6
votes
1answer
5k views

Stable radix sort in-place using O(1) space

I am trying to implement a stable radix sort in place with O(1) space. I've seen Wikipedia, and I notice the C implementation is using a temporary variable to hold the sorted value for each pass, and ...
6
votes
1answer
994 views

Sorting almost sorted array with a minimum heap

I'm currently looking into the quickest way to sort an almost sorted array: Given an array of \$n\$ elements, where each element is at most \$k\$ away from its target position, devise an ...
6
votes
3answers
984 views

Insertion sort in Scheme

Inspired by my merge sort in Scheme, I thought I'd try my hand at implementing other sorting algorithms, starting with simple ones (like insertion sort) and working my way up. So, here we go: ...
6
votes
1answer
769 views

Sorting colors by the name of the color

I was in need to sort Colors, so the first thing that I tried was this: ...
6
votes
1answer
337 views

Finding most frequent words from Google N-Gram dataset

I have written a program to process Google 1-Gram dataset in order to obtain the list of most frequent words. This is my second more or less working program in Haskell, so I really want to know if it ...
6
votes
1answer
1k views

Merge sort in Golang

I am just starting out with golang. I implemented the merge sort algorithm just to practice. Any suggestions or criticisms regarding the coding style? ...
6
votes
1answer
620 views

Bubble sort algorithms and unittest in Python

I am quite new in Python and I am starting my journey with sorting algorithms, PEP8 and the Zen of Python. So far i wrote a post BubbleSort and I drew conclusions and followed the advices. I ...
6
votes
1answer
43 views

Average of three biggest parameters out of five without using sorting algorithm or vector/array

I'm trying to create a function with has 5 input parameters and returns the average of the 3 biggest. However I'm not supposed to use a sorting algorithm, vector/arrays or function libraries and still ...
6
votes
0answers
177 views

Selection sort with reduced comparison count: Python iteration 2

Follow up to Selection sort with reduced comparison count - semi-final Iteration? My goal (and excuse not to tag reinventing…) is to have presentable code to argue the viability of reducing the number ...
5
votes
4answers
3k views

Sorting Algorithms

A group of basic sorting algos. Based on Algorithms, 4th Edition - Robert Sedgewick | Kevine Wayne. Just making sure all of my logic and everything is correct. ...
5
votes
3answers
3k 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 ...
5
votes
2answers
2k 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? ...
5
votes
2answers
600 views

Partitioning and sorting even and odd numbers within an array

I solved a problem where it was told to sort an array of numbers such that the even numbers are kept first and the odd numbers should follow. Moreover, the even numbers must be sorted in ascending ...
5
votes
2answers
2k views

Which is better code for implementing quicksort?

This is how I used to implement quicksort: ...

1
3 4
5
6 7
21