Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
2 votes
1 answer
52 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 ...
  • 123
3 votes
2 answers
77 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)...
  • 26.4k
2 votes
1 answer
72 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 ...
  • 119
3 votes
3 answers
432 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 ...
  • 237
1 vote
0 answers
54 views

Another quicksort implementation

I have created a program in python using the quicksort algorithm. However, I am unsure if I have properly done so. Is the following program an accurate representation of quicksort? ...
  • 11
1 vote
1 answer
48 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 ...
0 votes
0 answers
56 views

LeetCode Merge k sorted lists Javascript

https://leetcode.com/problems/merge-k-sorted-lists You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list ...
6 votes
3 answers
114 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, ...
3 votes
0 answers
55 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 ...
  • 131
1 vote
1 answer
91 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 ...
2 votes
2 answers
136 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 (...
3 votes
1 answer
80 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 ...
  • 31
7 votes
4 answers
198 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. ...
  • 71
4 votes
3 answers
155 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): ...
  • 196
0 votes
2 answers
56 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 <...
0 votes
1 answer
57 views

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

Here are 2 bubble sort algorithms: 1: ...
1 vote
6 answers
165 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 ...
2 votes
2 answers
93 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+...
  • 163
1 vote
1 answer
73 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 ...
  • 11
8 votes
2 answers
723 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 ...
1 vote
1 answer
133 views

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

I wrote a merge sort implementation in C++ today in C++20 way. ...
  • 1,643
0 votes
1 answer
112 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 ...
1 vote
1 answer
634 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 ...
0 votes
1 answer
64 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 ...
5 votes
2 answers
727 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 ...
  • 799
0 votes
1 answer
69 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 ...
  • 731
1 vote
1 answer
58 views

Insertion Sorting Algorithm

I am new to data structures and algorithms. I have just implemented an insertion sorting algorithm. I just want to make sure if my code is alright. ...
  • 13
3 votes
1 answer
83 views

Optimize mergesort algorithm

I just started Stanford Coursera algorithms course and I wrote this mergesort based on what was shown: ...
0 votes
1 answer
40 views

Custom sorting algorithm in Salesforce Apex

I had a scenario where I needed to sort a list of EmailTemplate, but didn't have anything to sort the list by (other than a common number in the ...
  • 798
-3 votes
3 answers
233 views

CodeForce 230 Dragon Fighting, Sorting without using array

Problem: https://codeforces.com/problemset/problem/230/A Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all \$n\$ dragons that live on this ...
2 votes
2 answers
943 views

Sorting table rows visually with animation while keeping them intact in the DOM

I have a table which i want to sort the rows visually according to a column while the rows remain intact in the DOM structure. So nodes aren't moving in the DOM or getting replaced with eachother at ...
  • 811
0 votes
1 answer
201 views

A New Sorting Algorithm

I have designed an Algorithm for sorting numbers. This algorithm works by sorting an array of any length with random, non-repeating whole numbers in a linear manner by ascending order. Under the ...
2 votes
1 answer
138 views

Counting Radix Sort: A C++ version

Follow up to original question: This is Radix Sort, using a counting implementation. For numbers that are N bytes in length, we use an N pass counting approach. Starting with the least significant ...
  • 92.3k
1 vote
1 answer
88 views

Ordered unique union function

I made a user-defined function union in VBA, such that: it could take variable parameters each parameter is a one-column range like ...
  • 113
2 votes
2 answers
100 views

Radix Sort: A C++ version

Just realized I have never implemented Radix Sort (spurred about watching a YouTube video about it). So I thought I would give it a go. This is Radix Sort, using a counting implementation. For numbers ...
  • 92.3k
0 votes
1 answer
85 views

Simple sort function

I am learning Python and for practice I have written this function which sorts a list. Can you please provide feedback on how I have structured it and what better ways could be there to write it for ...
0 votes
1 answer
43 views

Quicksort Invariant and steps to determine correcteness

I am having trouble with applying to 3 steps needed to proof correctness, most likely because I did not formally study CS or Mathematics and am still learning about loop and class invariants. The ...
  • 41
2 votes
4 answers
376 views

Sorting based on multiple fields using IComparer

I use the below code to sort List<DataAccessViewModel> list. Here is the sort order : PriorityScore ...
2 votes
2 answers
77 views

Function to find substring in a list of strings and add them to the resulting list

I am trying to look for a substring in a list of strings. My end result is an array that contains the presence of substring first based on course.name and then ...
2 votes
3 answers
123 views

Sort one array base on another array

The problem is as below: Two archery teams A and B compete with the same number of members. If the score of team A[i] is higher than that of team B[i], the total score of team A will be +1, so given ...
  • 123
5 votes
3 answers
588 views

Counting swaps in a sequence sorting algorithm

I have been working on this question, https://open.kattis.com/problems/sequences. You are given a sequence, in the form of a string with characters ‘0’, ‘1’, and ‘?’ only. Suppose there are n ‘?’s. ...
3 votes
1 answer
529 views

Quicksort using generics in java

The goal is to have a function that can sort a series of all object types that implement the comparable interface. I'm not trying to improve the performance of the sort. I'm only trying to optimize my ...
  • 131
0 votes
1 answer
58 views

custom array sorting algorithm

So I came up with a "new" sorting algorithm: ...
3 votes
1 answer
79 views

Sorting algorithm in the element cost model Python

1. Task Using standard input I read four rows of data: number of elements (elements are numbered from 1 to n) n numbers corresponding to cost of moving each element n numbers corresponding to current ...
0 votes
2 answers
680 views

Sort table with ascending or descending order without a library

This is my first day coding, and I need help with sorting a table in ascending/descending order. The idea is that when a column is clicked it will sort in ascending order, and then it is clicked again ...
1 vote
1 answer
136 views

Find Kth Element in the merged two sorted arrays?

We have been given two sorted arrays and a number K . We have to merge the two sorted arrays in the sorted manner and return the element at the Kth position. My approach is to use two variables ...
2 votes
2 answers
239 views

Sort array of dates

I have this array with dates. All the dates are checked before creating the array with preg_match and check_dates. GoodDates ( ...
4 votes
1 answer
230 views

recursive bin packing

For the company I work for we spec lengths of tube of varying lengths. For a given project let's say there is a list like this 100', 115', 105', 205', 195', 240', 240', 130', 180', 140', 225', 85' ...
  • 41
2 votes
2 answers
772 views

Using JavaScript, given a JSON value, recursively find all JSON objects, then sort keys in-place

Some background: JavaScript is not my primary language, so I'm looking to get some constructive criticism here. I built a tiny single page HTML app that pretty prints JSON text. It's very useful when ...
  • 133
2 votes
2 answers
160 views

Sort three numbers

So, I wrote this function to sort three numbers in Julia. Any feedback on readability, performance, and Julian-ness would be appreciated. ...
  • 333

1
2 3 4 5
23