Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
2 votes
1 answer
42 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 ...
Lydia Yuan's user avatar
4 votes
2 answers
131 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 ...
Tobias Grothe's user avatar
2 votes
1 answer
39 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 ...
Preatorius's user avatar
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 ...
Глеб's user avatar
  • 183
2 votes
2 answers
131 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: ...
Bryan C's user avatar
  • 21
0 votes
0 answers
45 views

Finding the pivot point in a rotated array which has been shifted from kth element

I'm trying to solve the following problem: Joker has a deck of n cards, each with a value written on it. The cards are arranged in ascending order from top to bottom. Joker's master performs a hidden ...
driver's user avatar
  • 212
0 votes
0 answers
69 views

Tried to come up with a novel sorting algorithm on my own

As said in the title, I tried to come up with a sorting algorithm on my own to practice. It seems to work, even though it's not in place. Is it "new" or does it already exist? ...
Lorenzo Salmonese's user avatar
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 ...
Logan James's user avatar
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, ...
Amal Krishnan's user avatar
1 vote
2 answers
92 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)? ...
mascai's user avatar
  • 397
1 vote
1 answer
110 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: ...
coderodde's user avatar
  • 27k
3 votes
3 answers
148 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 ...
user14464173's user avatar
12 votes
4 answers
1k 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
4 votes
5 answers
731 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 ...
xyf's user avatar
  • 508
2 votes
2 answers
173 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 ...
Strange Alchemist's user avatar
8 votes
3 answers
767 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: ...
Clément Jean's user avatar
2 votes
1 answer
57 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 ...
Thomas Mathew's user avatar
3 votes
1 answer
96 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 ...
Kyle Price's user avatar
3 votes
2 answers
125 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,...
Looser User's user avatar
1 vote
1 answer
126 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 ...
user avatar
6 votes
2 answers
266 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 ...
Piovezan's user avatar
  • 257
4 votes
2 answers
146 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 ...
Tomas's user avatar
  • 43
2 votes
1 answer
31 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 ...
Toby Speight's user avatar
3 votes
1 answer
150 views

Optimizing my sorting function

I created my own list sorting function with Python: ...
U13-Forward's user avatar
3 votes
1 answer
306 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 ...
Nick's user avatar
  • 1,480
3 votes
1 answer
117 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 ...
doroshm's user avatar
  • 33
2 votes
1 answer
284 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 ...
Panic's user avatar
  • 287
3 votes
2 answers
105 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)...
coderodde's user avatar
  • 27k
2 votes
1 answer
186 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 ...
morloq's user avatar
  • 119
3 votes
3 answers
598 views

Sort array of associative arrays by the key of the subarrays

I am trying to sort an array of associative arrays in reverse order by the key of the subarrays. I solved it with a bubble sort algorithm which worked just fine in my test. I was wondering if there is ...
Black's user avatar
  • 243
1 vote
1 answer
51 views

Bash script to count word occurrences in file.txt version 2

This is version 2. As time allotted some improvements from version 1 suggestions. Question 1 In this script for Kubuntu 22.04.1, how to set the Terminal scroll-back ...
joseph22's user avatar
6 votes
3 answers
128 views

Simple selection_sort in C

I study algorithms, and I try to implement each algorithm in C (Perhaps it would be better to choose C++, but okay) Here is my implementation of sorting by choice. How would you rate it? To be honest, ...
Karlos Margaritos's user avatar
3 votes
0 answers
94 views

Destructive quick and merge sort

I've implemented a destructive merge and quick sort in common lisp, but the code feels verbose and highly imperative. I was wondering if anyone could offer guidance on idioms that could make the code ...
bfair's user avatar
  • 131
1 vote
1 answer
120 views

Minimum number of swaps to sort a array, but swaps only allowed with elements 2 apart

I wanted to solve a problem where I sort an array of any size and return the minimum number of swaps. The explanation for the distance 2 is that I choose 3 neighboring elements ABC and then swap them ...
programmerNoob1234's user avatar
2 votes
2 answers
226 views

Bogo Sort Algorithm

I am a beginner programmer and have written a program that sorts a number of integers of your choosing using bogo sort. Are there any improvements that could be made to make this code more efficient (...
David Xu's user avatar
3 votes
1 answer
82 views

Function that sorts nestedArray[][]'s

I'm fairly new to coding and am trying to learn more about best practices. I wanted to have an nestedArray of objectArrays, and I wanted to be able to sort the elements in each objectArray based on ...
WyreZ's user avatar
  • 31
7 votes
4 answers
203 views

Sum and bubble-sort student scores

I am a beginner in C language. I studied all night and did my assignment. But I wonder if there is a more efficient way. The original structure is this. ...
cozy_hn's user avatar
  • 71
4 votes
3 answers
228 views

Introsort implementation

I recently decided to make my own implementation of the Introsort sorting algorithm for educational purposes. Here's what I ended up with (apologies for the lack of comments): ...
Famiu's user avatar
  • 196
0 votes
2 answers
61 views

A Sorting Algorithm

I've written this sorting algorithm in C. It looks for the element with the max value every iteration and adds it to the end of the array, swapping it with other smaller elements. This way, by almost <...
user13387446's user avatar
0 votes
1 answer
215 views

bubble sort algorithm with nested loops performs better than with one loop JS

Here are 2 bubble sort algorithms: 1: ...
Ben_Sven_Ten's user avatar
1 vote
6 answers
191 views

Find first and last matches on a sorted list

I needed a function to find the first and last indices into a sorted list corresponding to a given value (roughly similar to C++'s equal_range()). This is the ...
ajax333221's user avatar
2 votes
2 answers
161 views

Partitioning a list so that even numbers appear before odd numbers

I started learning Rust today and figured, I could ask for some code reviews to learn to make my code idiomatic and learn about its code generation. I spend my days writing highly optimized Java and C+...
eof's user avatar
  • 193
1 vote
1 answer
444 views

Sort and search vectors by different criteria

Class class Player { std::string name; int score; int id; }; I thought about how to write a code to sort and search vector ...
MeoH's user avatar
  • 11
9 votes
2 answers
961 views

Multithreaded bogosort in Rust

For some quick context, I'm fairly new to Rust and decided to give myself a difficult-but-attainable task of implementing a sorting algorithm. I chose bogosort (or stupid-sort, shuffle-sort, whatever ...
LambBrainz's user avatar
3 votes
2 answers
222 views

C++ merge sort, logging, unit test, performance check

I wrote a merge sort implementation in C++ today in C++20 way. ...
frozenca's user avatar
  • 1,693
0 votes
1 answer
300 views

Sorting nested dictionary by price - without extra memory

I am working on parsing an orders.csv file with products and prices by different vendors which is kind of a time-series data. The output of that process looks something like below, with order total by ...
Prashanth kumar's user avatar
1 vote
1 answer
2k views

HackerRank CountingSort 1 in C#

Comparison Sorting Quicksort usually has a running time of n x log(n), but is there an algorithm that can sort even faster? In general, this is not possible. Most sorting algorithms are comparison ...
wayne.blackmon's user avatar
0 votes
1 answer
102 views

Sorting in linear time using Trie Data Structure

I have written an article on a new way of non comparison sorting and I want it to get peer reviewed. The sorting algorithm uses Trie data structure to store digits of numbers as nodes and iterating ...
Jomy Sebastian's user avatar
5 votes
2 answers
1k views

Sort a Python list of strings where each item is made with letters and numbers

I have a set of strings that all consist of one or two letters followed by one or two digits. I need to sort by the letters then by the numeric value of the digits. My solution is quite inefficient ...
Pitto's user avatar
  • 799
0 votes
1 answer
128 views

Six different, concise (and hopefuly readable), sorting algorithms using ES6+ idioms, with some basic unit testing

I'm practicing js, unit testing, and algorithms, so implementing some of the common sorting algorithms and doing some basic unit testing on them seemed like a good exercise. I'm also trying to use ...
NPN328's user avatar
  • 751

1
2 3 4 5
24