Questions tagged [sorting]
Use this tag when arranging items in order is the main focus of the code.
1,168 questions
2
votes
2
answers
256
views
Benchmarking in Java some super linearithmic sorting algorithms
Intro
A sort is called super linearithmic if its running time is \$\omega(N \log N)\$. For example, \$f(N) = \omega(g(N))\$ means that \$f(N)\$ grows "faster" than \$g(N)\$. In this post, I ...
7
votes
4
answers
687
views
My BRESort adaptive sorting engine in C
I've created BRESort - an adaptive sorting engine that dynamically selects
optimal algorithms based on data patterns. It achieves 3.6-4.2x speedup over stdlib qsort ...
5
votes
2
answers
421
views
Ordinary insertion sort vs. straight insertion sort in Java (benchmark)
Intro
So this time I wanted to find out which of the two insertion sort flavours are faster:
Code
io.github.coderodde.util.StraightInsertionSort.java:
...
4
votes
3
answers
233
views
Field Sorting , ANSI-C/K&R 2ED, Exercise 5-17 brainstorming
This program is an expansion of the sorting utility built in previous chapters of K&R's 2nd edition of ANSI-C Programming. It parses command-line arguments to determine which fields to sort by and ...
0
votes
3
answers
222
views
C# quicksort implementation (LeetCode Sort an Array)
I implemented quick sort after merge sort as part of LeetCode question for sorting an array. The solution code can be found at https://leetcode.com/problems/sort-an-array/solutions/7180269/i-...
4
votes
2
answers
150
views
Custom RowSorter that sorts a Swing table containing data with unit suffixes
I would like to hear what you say about my custom RowSorter implementation.
The following comparators should be used for the table columns: String, Double and Integer.
The table data has postfixes and ...
5
votes
6
answers
2k
views
Bubble sort with 10 random numbers
I want to make a bubble sorting algorithm of 10 random (non-repeating) numbers from 0-50. I changed many aspects of it while making it and finally got it to work, but I would like to see if there's ...
8
votes
5
answers
1k
views
LeetCode 977: Squares of a sorted array, maintaining non-decreasing order
I tried solving the LeetCode question like many others, trying to incorporate the O(n) time complexity requirement.
Squares of a Sorted Array
Given an integer array ...
5
votes
2
answers
231
views
Sorting file names of a RAR split archive (v3 and v5)
This simple task of sorting a list of related RAR files of a split archive turned out to be more cumbersome than expected, since there are two versions of the RAR naming scheme, v3 and v5.
v3 is like ...
7
votes
6
answers
1k
views
Fast allocation-free alphanumeric comparer used for sorting
I was searching for C# comparers that can be used for natural sorting ("a9" comes before "a11"), but most solutions have a lot of allocations, quite unreadable code or are not ...
2
votes
1
answer
100
views
Multithreaded Merge Sort Using ForkJoin Framework
I've implemented a multithreaded merge sort using Java's ForkJoin framework, and I wanted to gather feedback on its correctness, efficiency, and scalability.
Here's my implementation:
...
-4
votes
1
answer
132
views
Pyramidal (Heap) sorting by D. Knuth
Help me with Pyramidal sorting by D. Knuth algorithm in C++.
I would like to clarify if I followed the algorithm correctly.
The code works.
I have the function, but I'm not sure if my code fully ...
3
votes
0
answers
130
views
Comparing two Tree sort algorithm variations implemented in Java
I have this repository. It contains three variations of a simple sorting algorithm for integer keys.
The idea is that we keep a balanced BST in which each node holds the integer key and its frequency. ...
2
votes
1
answer
82
views
Advent of Code 2022 Day 13 Solution in C: Sorting and parsing nested list
Link to Original Problem: Advent of Code 2022 Day 13
Question Summary:
The task involves sorting and parsing nested lists, with two parts:
Determine pairs in the correct order and calculate the sum ...
3
votes
1
answer
142
views
Sorting the divisors of a given number
I am trying to sort all the divisors of a given number in the most efficient way.
Below is my code for sorting the divisors:
...
4
votes
1
answer
158
views
Efficient least-significant digit (LSD) radix sort for int keys in Java
(This post has a continuation post.)
This one is my attempt at LSD radix sort:
Code
com.github.coderodde.util.LSDRadixsort.java:
...
5
votes
1
answer
174
views
Sorting songs with the ability to save and resume partial sorts
I listen to a lot of music (~4k h/y) and managing thousands of songs is a bit of a challenging.
To improve my listening experience I want to better organize my collection.
One thing I want to do is ...
4
votes
1
answer
340
views
c++ quicksort pivots
Is this code for quicksort using the first index as a pivot correct?
Also if I want to use the last or middle index as a pivot how would the whole code change?
...
2
votes
1
answer
117
views
Sort array of numbers using tree sort - Leetcode 912
Problem statement:
Sort integer array nums in O(N log N) time,
without relying on any library sort routine.
I am trying to use a tree sort method (using the ...
1
vote
0
answers
47
views
Determine top t values with few calls to order(), a given procedure to order k values, Java take 2
My 2nd take of the problem in Determine top t values with few calls to order(), a given procedure to order k values:
Given an array and a procedure to order \$k\$ ...
2
votes
1
answer
277
views
Fraudulent activity notifications problem from hackerrank
There's a problem on hackerrank called fraudulent activity notifications: given an array of integers and a value k, I'm supposed to increment a counter if the median of the numbers up until k , ...
4
votes
1
answer
88
views
Average Pair Sorting algorithm
I had to relax a bit, so I wrote this sorting algorithm. By any means it isn't fast, but I really started to get interested in it, since I haven't seen similar approach yet.
Disclaimer
I do not intend ...
8
votes
2
answers
443
views
Heap sort optimization
I tried to optimize heap sort algorithm. Instead of recursion, I used iteration:
...
3
votes
1
answer
98
views
Creating Worst-Case Scenario for QuickSort using Middle Pivot in Java
I've implemented a solution to generate the worst-case scenario for QuickSort using the middle pivot strategy in Java. The goal is to rearrange an input array to produce the worst performance during ...
5
votes
2
answers
171
views
Print the three most occurring chars in Java using streams
The three characters that occurs most in a given string should be printed by using streams/lambdas (functional programming). The order of characters that occur equally often should be preserved.
My ...
2
votes
1
answer
93
views
Implementing an improved merge sort that uses insertion sort, with "levels" - Would this work correctly?
We were asked to improve the merge sort algorithm by introducing insertion sort to the code. We have been tasked with doing this by utilising a "levels" logic. Here is the exact description ...
8
votes
3
answers
2k
views
Algorithm "sort except zero"
Task:
Sort the integers in sequence in sequence. But the position of zeros should not be changed.
Input: List of integers (int).
Output: List or another Iterable (tuple, generator, iterator) of ...
3
votes
2
answers
253
views
Counting duplicate elements in two sorted arrays
I've been working on an assignment that involves optimizing a duplicate finding algorithm for sorted arrays, and I'd like to get your thoughts on the implementation. Here's the code I've come up with:
...
9
votes
3
answers
2k
views
Sorting array by odd numbers while leaving the even numbers in the same spots
The problem given was: "You will be given an array of numbers. You have to sort the odd numbers in ascending order while leaving the even numbers at their original positions."
I developed a ...
4
votes
4
answers
1k
views
Quick Sort Done Lucidly
I've been away from Programming and the Tech Industry for sometime. I thought I'd look at and try some of the older basic stuff. I bushed up to QuickSort and found most existing implementations of it, ...
1
vote
2
answers
142
views
Sort by parity in rust
I am learning sorting in rust. I want to sort vector as such that even numbers go first
My code is OK. But can it be improved (I want to use sort function)?
...
1
vote
1
answer
130
views
Queue-mergesort: a mergesort that does optimal number of comparisons in the worst case in Java
Here is the code for queue-mergesort by Mordecai J. Golin and Robert Sedgewick:
com.github.coderodde.util.QueueMergesort.java:
...
3
votes
3
answers
209
views
In-place recursive quick sort in C
Here's my second program in C, and my time/space analysis of this code.
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 ...
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, ...
4
votes
5
answers
991
views
Sort a vector containing only three distinct values
I have a program that sorts the vector containing only 0,1,2 in the sequential order so the input array of [0,2,1,2,1] would generate an output ...
2
votes
2
answers
225
views
Quick Sort Program
Quick sort is a sorting algorithm in which we select any random element as pivot from the array and we do the partition of the array around that pivot. Basically we place all the elements smaller than ...
8
votes
3
answers
868
views
Find steps to sort unsorted string list
Let's say that we have a list of strings and we want to find the steps (pair representing swaps to be made) to sort it. Here is my current implementation:
...
2
votes
1
answer
86
views
Merge Sort with Minimum Sufficient Variables, Verbosity and better Space Complexity
The majority of merge sort implementations searched online are provided with unnecessary variables and code lines. Here is an attempt to reduce that. However, does passing back the subArray as return ...
3
votes
1
answer
177
views
rust sort numbered directories (non-lexicographical)
I am very new to rust and have been reading up and playing around to get a better understanding. I was trying to replicate a small task that I have done via bash scripting before, as a way to ...
4
votes
2
answers
209
views
Sort a dict by keys, assuming keys are orderable
I found a suspicious code snipped in an existing project in which I am involved as a developer. The code sorts a dict by its keys, so that if iterated over the dict,...
1
vote
1
answer
182
views
K-Way Partitioning QuickSort
I have implemented the K-Way Partitioning algorithm which is based on the Peter Taylor's solution.
Which works but i want fill the q array which is the borders of ...
6
votes
2
answers
346
views
A selection sort implemented in Python
I'm not proficient at Python. My question is twofold: whether this selection sort algorithm implementation for number lists could be considered clean code (in the sense of being modular and ...
6
votes
2
answers
243
views
Sort todo.txt items by due date in Python 3
I am using the following format for my task management: https://github.com/todotxt/todo.txt
Do some stuff +uni due:2022-12-31
Write some paper +uni due:2023-01-10
...
3
votes
1
answer
63
views
Find first or last argument lexicographically or temporally
I often find I need to find the earliest or latest file matching a given pattern, and sometimes to choose the lowest or highest string from several possibilities.
The following four functions provide ...
3
votes
1
answer
195
views
Optimizing my sorting function
I created my own list sorting function with Python:
...
3
votes
1
answer
383
views
High performance 3 Way Quick Sort Implementation
My implementation of 3 way quick sort for strings.
It supposed to sort very large set of 800,000,000 objects.
This is why I added ...
3
votes
1
answer
171
views
How can I condense my code in node.js for sorting unique values within an array?
Kattis problem - ("I've been everywhere")
I would highly recommend looking at the problem through the link, however I will summarize it a bit here and explain the functionality of each part ...
2
votes
1
answer
407
views
Generic insertion sort
I implemented a generic insertion sort routine that can sort an array of any type. It's similar to the qsort function from the standard library. My goal it to optimize the code for readability above ...
3
votes
2
answers
151
views
Fast portable, parallel MSD radix sort for unsigned keys in C89
Now I have this portable, parallel MSD radix sort for unsigned keys. It exhibits linear speedup on small values of \$P\$ and has a running time of
$$
\Theta(N/P + P)...
2
votes
1
answer
379
views
Sorting visualizer using Java Swing
I programmed a little sorting visualizer a while back and I have been meaning to get some feedback on the overall implementation and on what I could do better to have cleaner code or a "best ...