Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
2
votes
2answers
737 views

Merge sort a given list in Python

Problem: Implement merge sort in python. Don’t use Python’s built in sort or sorted. Assume your inputs will be sufficient for the memory you have. How should I make this code better? ...
2
votes
1answer
117 views

Haskell merge sort inversions

I'm working on a task from the one of Coursera programming courses. The purpose is to estimate the amount of inversions that are required to sort the array of numbers. Also I'm trying to learn some ...
-2
votes
1answer
214 views

Selection Sort in python

I'm following the pseudocode as given as a part of an excercise in CLRS. Consider sorting \$n\$ numbers stored in array \$A\$ by first finding the smallest element of \$A\$ and exchanging it with ...
2
votes
2answers
324 views

Generic Merge Sort in C++

I wanted to implement merge sort in a generic way. I was not sure whether I should use the iterator or the container for the template type. I did not find a way to infer the container type using ...
3
votes
1answer
65 views

Fastest way to find if a lot numbers are in multiple intervals

So I have a task. I've been given \$n\$ numbers and \$m\$ intervals and I need to figure out how many numbers are in the \$m\$ \$i\$-th interval. I've written some code with a complexity of \$O(n \...
1
vote
1answer
66 views

Searching and sorting ranges in excel with toggle buttons

I have a spreadsheet containing ±100 named ranges (5 rows, 15 colums) which are sorted based on time. My plan was to make toggle buttons to hide or unhide named ranges when they contain a certain ...
6
votes
4answers
14k views

Sorting dates in this format (DD-MM-YYYY)

Is this the correct and an elegant way to sort the dates in the format (DD-MM-YYYY) in ascending order? ...
1
vote
0answers
331 views

Applies the Largest Remainder Method to a number property in a list of objects

I need to round BigDecimal numbers according to the Largest Remainder Method (LRM). Please reference this SO post with this answer. Here's my version of the algorithm in Java: ...
7
votes
3answers
109 views

Sorting Exclusive of Specified Indexes in List

To further generalize the algorithm (past the doc string): Given a list E(1) E(3) E(2) E(4) _ E(0) _ where E(N + 1) is ahead of <...
5
votes
2answers
2k views

Recursive sorting algorithm

I wrote a recursive sorting algorithm and was wondering what would be the run time complexity of this. The bulk of the work is done in the push_up function, which I ...
2
votes
1answer
1k views

Java generic sorting class

I'm writing a class that will sort multiple types {arrays, arraylist, linkedlist} but I see there's a lot of code duplication. For instance, method to swap in list, and in arrays, method to sort list, ...
8
votes
2answers
2k views

HackerRank leaderboard challenge

I have been doing this challenge in which I need to determine and return one's (i.e. alice) rank on a leaderboard by comparing her scores to the others'. I made ...
4
votes
2answers
479 views

Bottom-up singly linked list merge sort in C++

This is an implementation of a singly linked list merge sort I did on paper during a job interview recently, using a bottom-up approach. The requirement was simply "implement a merge sort for a ...
-1
votes
1answer
1k views

Sort multidimensional array based of the difference in the value

Sort multidimensional array based of the difference in the value, if value is same sort on first column. Constrains: No of rows can be any but fixed no of column ie 2. Example: <...
2
votes
1answer
87 views

Sorting with different Algorithms

I'm new to Go, so I implemented a few sorting algorithms for practice. For example, I have an in-place insertion sort, and a bubble sort: ...
6
votes
2answers
311 views

Bubble sort with counting swaps and comparsions

I have bubble sort code below, which has to count swaps and comparisons of elements. How can it be made more elegant and clear, if it is possible? I think using lots of tuples is not the best way to ...
0
votes
1answer
452 views

Efficiently Sorting File Paths [closed]

I'm trying to print out a list of files in a specific order, with files closer to the root directory sorted first. For example these files: c:\users\steve\foo.txt c:\users\bar.txt c:\users\steve\...
1
vote
2answers
72 views

Implementing multiple custom sorts [closed]

The following code works, but I'm sure there's a cleaner way to implement it and I'd appreciate any guidance on how to go about that. I have a class Result which I want to sort sometimes by score (...
3
votes
1answer
66 views

Multi-field sorting

A function to sort data on multiple fields. I welcome feedback. ...
2
votes
1answer
141 views

Sorting elements in array with a certain condition

This is what I have: function get_create returns a link with new filtered/sorted properties: Code on screenshot ...
1
vote
3answers
644 views

Efficient way to remove duplicate strings after adding their key values

How can I remove duplicate strings from a std::vector<std::pair<std::string,int>> after adding the int members of ...
1
vote
0answers
53 views

Sorting BIGINT column with ascending positive values followed by descending nonpositive values

I have a column flowing in with the full range of integers. Whilst generating keys on the INT column, I need to push the 0's and negatives down (ordered DESC) and ...
1
vote
1answer
724 views

pancake sorting using python

pancake sorting is implemented below. Algorithm is below: Find the largest out of order pancake and flip it to the bottom (you may need to flip it to the top of the stack first).Repeat step one until ...
0
votes
1answer
55 views

Traversing through single large array to convert single array into array of arrays as per condition

I have a huge fileList array where many elements of array start from sub-** pattern. I want to group these files belonging to ...
1
vote
1answer
330 views

Implemented heapsort algorithm in Java

I am learning algorithms by using Java. Here is an implementation of heapsort. Let me know if anything is wrong and and suggestion to improve the algorithm. ...
1
vote
1answer
911 views

Recursive quicksort in Python

I want to find a more elegant way to build a recursive sorting function. The function randomly selects one of the numbers from a given list, k, and splits the list ...
4
votes
1answer
139 views

Recursive Ruby merge sort

I have implemented a merge sort in Ruby. (Yes, I know, there's Array#sort) I would like to get feedback on code quality and optimization. ...
1
vote
2answers
125 views

Is there a better way to write insertion sort?

Background I am doing an insertion sort and I would like it to be as efficient as the algorithm allows. Code After much research, this is what I made: ...
2
votes
1answer
2k views

Making a faster parallel quicksort

I'm trying to take my existing parallel Quicksort and make it execute faster. Below is what I have but half the time the optimized version doesn't give me a faster time. I use them to compare big ...
2
votes
0answers
855 views

Visualization of sorting algorithms

I recently started to learn Qt framework and decided to create an app to visualize the sorting algorithms(for this moment there are 5 sorting algorithms). I just want to know if I'm doing everything ...
1
vote
1answer
136 views

Bubblesort using Writer monad

I still consider myself a newbie to functional programming, and I still have some trouble wrapping my head around a lot of functional concepts. Anyway, here goes: ...
1
vote
1answer
143 views

Making bubble sort pure and efficient

Background I am re-writing a series of algorithms together with a colleague that we are going to later on publish in a packet for the community. To start, I picked the typical bubble sort algorithm....
4
votes
0answers
422 views

Selection sort implementation in C++ iterator approach

I am trying to learn the C++ way of writing code. I was wondering if this code could be improved or if there are any major mistakes. Is it ok how I name the variables? ...
2
votes
1answer
161 views

Selection sort implementation in C++

I was wondering if this implementation of selection sort could be improved. Are there any things that I have done wrong? ...
1
vote
1answer
101 views

Bubble sort implementation in C++

I was wondering if this implementation of bubble sort could be improved. Are there any things that I have done wrong? ...
1
vote
1answer
414 views

C++ insertion sort implementation

I was wondering if this implementation of insertion sort could be improved. Are there any things that I have done wrong? ...
4
votes
2answers
531 views

Quicksort implementation in C++

I was wondering if this implementation of quicksort could be improved. Are there any things that I have done wrong? ...
1
vote
1answer
272 views

Merge sort implementation in C++

I was wondering if this implementation of merge sort could be improved. Are there any things that I have done wrong? ...
3
votes
2answers
5k views

Sort dictionary by increasing length of its values

Given a dictionary where the values are iterables, print the dictionary sorted by the length of its values. ...
2
votes
1answer
208 views

Radix sort implementation in JS (LSD)

I have written LSD radix sort implementation in JavaScript (6 functions in total). It sorts positive and negative integers: ...
5
votes
1answer
2k views

Ranking players in a league by sorting the contents of an OrderedDict

I'm new to python. I'm having second thoughts on my approach to this problem: The LeagueTable class tracks the score of each player in a league. After each game, the player records their score ...
-3
votes
1answer
66 views

MergeSort - Is this actually MergeSort?

Will this code actually work as MergeSort? This is working to sort the array but is it MergeSort? My faculty did approve of this but he probably did not see the code properly and I need some serious ...
38
votes
10answers
59k views

Given an array of integers, return the smallest positive integer not in it

Write a function: function solution(A); that, given an array A of N integers, returns ...
6
votes
4answers
935 views

Generic merge sort in C++ using a user-defined comparison function or the < operator by default

I wanted to implement a generic merge sort algorithm using the same interface as std::sort(). The interface takes first and last iterators to a range of elements in ...
5
votes
1answer
2k views

Iterative in-place quicksort

I have implemented an iterative in-place quicksort algorithm in Python, which by default selects the pivot as the last element: ...
1
vote
0answers
379 views

Last element pivot Quicksort exercise in TypeScript

Never being good in "algorithming", I'm now trying to improve the situation a bit. An algorithm below is the attempt to home brew yet another quicksort. This time, specifically with a "last-element" ...
1
vote
1answer
59 views

Java number sorting algorithm - remove ugly break

I have written a little number sorting algorithm that puts a random array of integers into ascending order, my code is below and it is working fine. However there is the very ugly and unsatisfying ...
4
votes
3answers
17k views

Sort numbers in ascending order using Python

This is Python code to sort the numbers in ascending order (It's my school task). How does it look? It works, but I think that this can be improved. ...
1
vote
1answer
1k views

Sorting linked list with comparator in Java

When I want to sort data on linked list by using collections.sort() and comparator will working after write (...
2
votes
1answer
117 views

Adaptive Mergesort in Java - follow-up

(See the previous and first iteration.) Adaptive Mergesort This algorithm is from a paper "Sublinear Merging and Natural Mergesort" by Svante Carlsson, Christos Levcopoulos and Ola Petersson. It ...