Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [sorting]

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

2
votes
0answers
36 views

Efficient counting sort for large byte arrays in Java

I have this counting sort for byte values running in linear time with respect to array length: Arrays.java ...
1
vote
2answers
84 views

New Year Chaos JavaScript, needs to be sped up

Similar to this question but this is for Python Original problem with description on hacker rank I am currently trying to iterate through a large number of arrays and count how many times numbers ...
1
vote
1answer
38 views

Bubble sort in C (for strings)

What do you think of this bubble sorting function in C, for a string array? I would like to improve the readability, performance, and any good practices. ...
9
votes
5answers
248 views

Sort a list of pairs representing an acyclic, partial automorphism

I have this sample data: let trips = [ { from: "DEN", to: "JFK" }, { from: "SEA", to: "DEN" }, { from: 'JFK', to: 'SEA' }, ]; ...
5
votes
3answers
109 views

Repeatedly keep removing 2 smallest elements and add its sum back to the list and return newly inserted elements' total

I have a list of unsorted numbers. Each time, I need to remove 2 smallest numbers and insert back its sum into the list. Repeatedly do this till the 1 final element is remaining in the list. Return ...
4
votes
2answers
252 views

Possibly bubble sort algorithm

I'm trying to figure out what to call this sorting algorithm: ...
5
votes
0answers
51 views

Engineering an even faster qsort

I understand that C++ STL template sort runs faster than qsort in C because ...
2
votes
0answers
75 views

OrderBy and Take in LINQ [migrated]

I was watching some videos on Youtube with programmers interviews. One of the questions is to write a function that returns n-th smallest element of an array. In the video I watched some lady tried ...
6
votes
2answers
223 views

C++ parallel merge sort

I'm a first year computer engineering student who's learning about algorithms and data structures. I've implemented a parallel merge sort algorithm in C++ and would like constructive criticism. This ...
1
vote
0answers
23 views

Sort an array of strings using bubble sort

I have this following code to sort an array of strings order by ASCII value (alphabetically) using bubble sort. ...
3
votes
1answer
99 views

Implementation of Radix Sort algorithm for sorting integers in c++

I wrote the following code for Radix Sort algorithm for number sorting. Any review is highly appreciated. You can also view and run/test the code here Radix Sort ...
0
votes
0answers
48 views

Median of three partitioning taking more time than standard quicksort in java

I implemented a standard quicksort and I have a project where I need to improve it. I am trying to make quicksort faster by implementing median of 3 partitioning. I copied codes from trusted ...
1
vote
0answers
34 views

Median of 3 partitioning taking more time than quicksort in java

I have a quicksort code in java which I want to improve. The improved code should take less time than the quicksort code. However when using my improved code which implement median of 3 partitioning, ...
2
votes
1answer
72 views

Finding maximum homework grade ECOO 2018

I'm trying to solve ECOO 2018 round 2(regionals) question 2 to prepare for the upcoming contest. Basically, in the question George has to achieve the maximum possible grade by completing the right ...
2
votes
1answer
26 views

Using deftype to create a wrapper class for byte arrays

I want to create a wrapper class for byte arrays so that I can use them as keys in fastutil's Object2LongAVLTreeMap. I have ...
1
vote
0answers
35 views

Sorts (merge, quick, bubble) in Python

I'm new to python, and I wrote the following code. Please review, critique and enhance. This project compares quicksort, mergesort and bubblesort write/test the partition routine for ...
0
votes
2answers
127 views

Counting sort in Swift

I have this sorting algorithm which takes an array of dictionary values: guard var imageUrlString = anyImage.value as? [String:AnyObject] else { return } I then ...
0
votes
0answers
22 views

Priority URL queue that does not block on put()

I am trying to build a priority queue that never blocks on put(), and discards the lowest priority item if it is full and user attempts to ...
2
votes
1answer
57 views

Sorting an assortment of strings and integers together, while keeping them separate

I put a solution to this coding problem together. The problem is this: Create a function that takes an array, finds the most often repeated element(s) within it and returns it/them in an array. The ...
0
votes
1answer
52 views

Time complexity of radix sort

I read time complexity of radix sort is O(wn) or O(n log n), if all n numbers are distinct. After implementation it look to me as if I have implemented radix sort which has time complexity of O(n ^2). ...
5
votes
1answer
62 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 ...
1
vote
1answer
66 views

The quicksort algorithm in Haskell

I am learning Haskell programming language mainly from this source. And there I have encouraged with "an elegant" realization of the quicksort sorting algorithm (the Quick, sort! section). Here it is: ...
3
votes
1answer
107 views

Adaptive Merge Sort in C++

As the title says, I'm trying to implement a merge sort algorithm in C++ that's also adaptive. This is a personal exercise, and I don't have any specific application in mind. My main goal is to ...
0
votes
1answer
46 views

Insertion sort algorithm in Java

I have tested this code and it yields the expected outputs, but I want to verify if I have applied insertion sort algorithm correctly or not. ...
2
votes
2answers
55 views

Chained Sort Python Implementation

So I've run into a few occasions where I'd want to sort a list according to a criteria, then for all loosely sorted sublists (a sequence of equal elements), I'd want to sort those sublists according ...
7
votes
2answers
82 views

Rearrage sequence so that adjacent numbers add to perfect squares

Consider the array: original_array1[17] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17} I want to sort it in such a way that: Sum of adjacent numbers should be a ...
0
votes
2answers
74 views

Select nth smallest distinct integer from inputs

Kata in question Given a list of integers, return the nth smallest integer in the list. Only distinct elements should be considered when calculating the answer. n will always be positive (n > 0) ...
0
votes
0answers
55 views

Bucket sort algorithm in JavaScript

I was asked to implement bucket sort recently from my friend. Bucket sort is mainly useful when input is uniformly distributed over a range. For example, consider the following problem. Sort a ...
4
votes
1answer
60 views

SplitSort — An adaptive algorithm to handle collections with few inversions

SplitSort SplitSort is a rather simple Inv-adaptive and Rem-adaptive sorting algorithm described in Splitsort — an adaptive sorting algorithm by Christos Levcopoulos and Ola Petersson. The paper ...
2
votes
1answer
119 views

Shuffle deck of cards in JavaScript

I know randomness in programming can be a deep topic, and searching the topic seems to result in Fisher-Yates being the best method, but I'm curious what people would say about this method. Basically,...
1
vote
2answers
74 views

Insertion Sort and Selection Sort Implementation

I wanted to practice using templates since I have no experience with them, so I implemented these sorting algorithms. Selection Sort: ...
2
votes
1answer
60 views

Most efficient way to find spatial order from a list of tuples (Python)

I have a circle-growth algorithm (line-growth with closed links) where new points are added between existing points at each iteration. The linkage information of each point is stored as a tuple in a ...
4
votes
2answers
112 views

Sorting an array of numbers by descending frequency

Somebody asked me to create a code that orders unique numbers of an array according to their frequency, ie: {1,3,3,4,4,4,4,4,2,2,5,5,5,5} to ...
0
votes
2answers
36 views

Can this be called an insertionSort?

Can following code be called insertion sort? I tried implementation according to my underdtanding... ...
2
votes
1answer
72 views

Sorting algorithms in Kotlin - Bubble, Insertion, Selection, Merge and Quick sort

Introduction I decided to try Kotlin because of its forgiving nature to long-time Java users. I implemented a few introductory sorting algorithms and would like to have them reviewed, not just to ...
2
votes
0answers
60 views

Generate automatically new groups in a tournament

I'm trying to model a tournament with Python (Django) and I've got a function that assigns teams of the previous stage to the groups of the new one. The teams in the first stage are assigned manually....
4
votes
1answer
144 views

Sorting an array in C++

This started as a simple exercise in sorting an array. I wanted to make it a bit more complex which is why I made it a 2D array. I then noticed that each pass that checked and swapped values, it was ...
2
votes
1answer
62 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
126 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
116 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
55 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
73 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
51 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
206 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
83 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
144 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 ...
3
votes
1answer
128 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
30 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
112 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
146 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 ...