Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
2
votes
1answer
91 views

Simple multithreading C project

I'm fairly new to C, and this is my first time using pthread. I am pretty sure I implemented it correctly, however I'd like to verify it is indeed executing the ...
6
votes
2answers
147 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 ...
4
votes
1answer
163 views

Natural merge: mergesort that uses already sorted subarrays

The code bellow is my implementation for the natural merge exercise in Robert Sedgwick's Algorithms book: Write a version of bottom-up mergesort that takes advantage of order in the array by ...
1
vote
1answer
56 views

A 'slow sort': repeatedly swap the first misordered pair

A 'slow sort' is probably the slowest practical sort. It compares each element with the elements after it, and swaps immediately if one is greater than the other, and goes back to the start of the ...
2
votes
1answer
74 views

Merge sort implementation with various improvements

So I was working on merge sort today, and was trying to speed it up. I would like your opinion on the implementation and if it's possible to make it faster. Besides that I was wondering if there are ...
4
votes
1answer
53 views

Decorator to add key parameter support to an existing sort function

Preface sorted built-in function has key keyword-only parameter. Most of the custom implementations like the following for ...
4
votes
3answers
212 views

Sorting a list of numbers, each with a character label

I am working on a project that requires me to modify an input list as follows: INPUT: List of numbers, each tagged with a label i.e. [[2, 'a'], [3, 'b'], [1, 'c'], ...] DESIRED OUTPUT: Sort the list ...
2
votes
2answers
85 views

Python merge sort

In Algorithms Fourth Edition by Robert Sedgewick and Kevin Wayne, the exercise 2.2.10 states that: Implement a version of merge() that copies the second half of a[] to aux[] in decreasing order ...
6
votes
2answers
157 views

Sorting algorithms with Python project

I wrote my first project in Python and I would like to know if this is correct. I prepared some guidelines I want to ask. (I know that I don't have any documentation yet). In the future I want to ...
4
votes
1answer
155 views

Hackerrank “Almost Equal” solution

I've spent the best part of a day on this question. It is marked as Expert level. There are about fifteen submission test cases and my solution manages to satisfy the first four. However, from there ...
1
vote
0answers
39 views

Sorting an array of numbers in Java with an algorithm

This is pretty much one of my first programs that I have created in Java and I just wanted to ask if anyone sees some obvious errors or mistakes I made. The purpose of this program is to sort numbers ...
4
votes
1answer
156 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 ...
1
vote
1answer
222 views

Bubble sort in Python

I am quite new in Python and I am starting my journey with sorting algorithms, PEP8 and the Zen of Python. I would like to get some feedback about my BubbleSort algorithm which I wrote. Also, is there ...
3
votes
2answers
170 views

My own algorithm to sort integers

I'm studying the C language with a Harvey Deitel book. Trying to solve an exercise that asked to write a good sorting algorithm, I did this: ...
7
votes
1answer
1k views

Sort int array by frequency then value

Problem Take an array of ints as input and run a special sorting algorithm on it. This algorithm must first sort by int frequency, less frequent first, then sort by value. For example ...
1
vote
0answers
276 views

Sorting cards by suit and value using Shell Sort

The question description: put a deck of cards in order by suit (in the order spades, heart, clubs, diamonds) and by rank within each suit, with the restriction that the card must be laid out face down ...
6
votes
2answers
689 views

Writing a class that represents a sorted list of filenames

I have a question: Write a class that represents a sorted list of filenames. The filenames are sorted by most recently added first. Duplicates are not allowed. And I wrote this solution: ...
5
votes
2answers
752 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? ...
2
votes
0answers
33 views

Sort ascending/descending functionally

I've been trying to get into functional programming with javascript and have tried to make some experiments with currying and function composition. What I've played with so far is a sorter function ...
-4
votes
1answer
117 views

Bidirectional bubble sort using goto [closed]

I am aware of Dijkstra's paper on why goto is harmful, but I thought it would still be fun to make a bidirectional bubble sort using ...
3
votes
2answers
682 views

Insert a number in a sorted list and return the list with the number at the correct index

I feel like the title is a bit wordy. What I have defined here is a function that takes two parameters: a list that contains valued sorted from smallest to biggest a number to insert at the right ...
3
votes
1answer
64 views

Selection and Insertion sorts from scratch in Java

I am trying to find a good, basic way to make selection and insertion sorts so that I can manipulate them for other sorting techniques. How do these look? Is there a simpler way to write them? ...
3
votes
2answers
116 views

Evaluation of a variation of quick-sort (pivot-selection)

Here is a variation of quick-sort where-in the pivot selection is based on calculating the average of the values of the highest and lowest numbers. ...
13
votes
3answers
410 views

Building unusual IComparer<T> from expressions

I've needed a couple of very special comparers recenty and didn't want to implement each one of them every time so I created a builder and a couple of supporting classes that do that for me. Example ...
1
vote
1answer
93 views

Verify that each named sequence is strictly increasing

The master launch sequence consists of several independent sequences for different systems. Your goal is to verify that all the individual system sequences are in strictly increasing order. In other ...
1
vote
2answers
116 views

Merge Sort code in C

Below is the code of Merge Sort for sorting an integer array in ascending order of values written by me. By looking at the code, I am thinking I can make the code more compact and secure, but not ...
1
vote
1answer
147 views

Sorting an unsorted array

This is a very simple sorting logic that I have written and it's working also. But Is there anything wrong in the traversing of the array? Or the code is not optimized? The code is simply declaring an ...
4
votes
1answer
65 views

Preparing tabular data to be sent as json

I query a database and pass the data for processing as a DataTable. After all computations (grouping, filtering, formatting etc.) I need to send it to a Web-Service ...
1
vote
1answer
53 views

Quicksort implementation with pivotal calculated as middle element

I read quick sort algorhitm and implemented in like this: ...
0
votes
1answer
3k views

Minimum number of swaps required to sort the array in ascending order

Here is my problem statement. An excerpt: You are given an unordered array consisting of consecutive integers ∈ [1, 2, 3, ..., n] without any duplicates. You are ...
5
votes
2answers
117 views

Computing word-frequency-table

I got a word count assignment and the requirements are shown below: INPUT The input contains words in several lines and in each line there could be more than one word. The input only ...
1
vote
1answer
35 views
1
vote
2answers
67 views

Speed up sorting algorithm in R: make one column “smaller” than the other

Consider the following sorting algorithm: ...
1
vote
2answers
145 views

Bubble and Cocktail sort

Lately, I've been developing sorting algorithms as a kind of exercise to make better code and I've come accross something curious, even though bubble and cocktail sort are known to perform horribly, I ...
1
vote
1answer
321 views

Quicksort Without Recursion

Am teaching myself about sorting techniques and trying my hand at a quicksort implementation. I found a lot of examples based on recursion but didn't want to go that route in a language that doesn't ...
0
votes
1answer
148 views

Sorting table using multiple columns [closed]

I want to sort entries in a given list on Entry column in most efficient way. Here is how my entries look: ...
2
votes
1answer
128 views

Sorting function from a React app to display JSON data in a sortable table

I created this simple react application for a coding challenge, which is described in the linked README file. Basically, I created the components and helper logic to display dynamic JSON data in a ...
0
votes
1answer
97 views

Sorting CSV table in Powershell

I'm wondering if some PS gurus can advise on whether my method for sorting and recombining some data fields by using an array of PSCustomObjects is as efficient as it might be. I'm importing a CSV ...
6
votes
2answers
985 views

Find the intervals which have a non-empty intersection with a given interval

The problem is the following: I have a list intervals which consists of tuples of the form (start, end) [with ...
1
vote
1answer
429 views

C++ - Radix sort implementation

I have the following radix sort implementation: ...
5
votes
2answers
175 views

Deficient Numbers

I have found the following interesting challenge on the web: Deficient Numbers A number is considered deficient if the sum of its factors is less than twice that number. For example: 10 is a ...
5
votes
2answers
1k 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 ...
0
votes
2answers
71 views

ContactsList implementation in Java

I created a ContactsList class in Java that holds Person objects. I consider this is a side project for my GitHub profile. Any ...
2
votes
4answers
73 views

Sort multidimensional array based on another one containg the desired order

I have an array like this: ...
7
votes
3answers
1k views

Sorting three ways

I have implemented function templates that take a begin and end iterator as well as an optional comparator for user-defined types. It was my intention to mimic the usage of ...
2
votes
0answers
226 views

React widget to display, sort and search remote data

I've created a React widget that pulls data on crypto-token pairs from a remote API and makes that data sortable by headers and searchable. You can view a demo of it here. Is this a maintainable way ...
7
votes
2answers
186 views

C++2a comparison-based topological sort algorithm

Here's my attempt at a C++2a Standard Library–friendly "topological sort" algorithm. There are several areas of interest here: The algorithm is comparison-based and in-place, just like ...
4
votes
2answers
1k 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 ...
5
votes
5answers
1k views

Sort an array of 0s, 1s and 2s in Java

Write a program to sort an array of 0's,1's and 2's in ascending order. Input: The first line contains an integer 'T' denoting the total number of test cases. In each test cases, First ...
3
votes
2answers
245 views

Sort digits in Python

Given an integer, sort the digits in ascending order and return the new integer. Ignore leading zeros. Parameters: Input: num {Integer} Output: {Integer} ...