Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
7 votes
1 answer
358 views

Comb Sort in VBA

Playing off Robust Bubble Sort in VBA and as suggested by @Henrik, I took a look at comb sort and tried to create an algorithm based on the documentation solely on Wikipedia. Basically, the first ...
Raystafarian's user avatar
  • 7,139
11 votes
3 answers
12k views

Bubble sort algorithm in Python

I'm starting to implement basic sorting algorithms. Criticisms on the implementation is welcome. ...
TravMatth's user avatar
  • 505
8 votes
2 answers
1k views

Selection Sort Algorithm (Python)

Selection Sort The selection sort algorithm sorts a list by finding the minimum element from the right unsorted part of the list and putting it at the left sorted part of the list. The ...
Emma's user avatar
  • 3,517
7 votes
4 answers
632 views

MergeSort performance

Do you see any inefficient parts with the following code? On internet I see 30 ms for 100.000 integers but I can sort 100.000 integers in 300ms. The data is random. I am using late 2013 macbook pro so ...
EralpB's user avatar
  • 173
26 votes
1 answer
3k views

Merge sort in Scheme

...
C. K. Young's user avatar
  • 2,342
15 votes
1 answer
4k views

Merge n sorted iterators

Over the weekend I was curious about efficiently merging multiple sorted iterators together, and for them to be in sorted order. This is quite like a challenge on HackerRank: You’re given the ...
Peilonrayz's user avatar
  • 43.3k
14 votes
4 answers
3k views

Shell Sort, Insertion Sort, Bubble Sort, Selection Sort Algorithms (Python)

There is a follow-up question available: shell-sort-insertion-sort-bubble-sort-selection-sort-algorithms-python. Selection Sort The selection sort algorithm sorts a list (array) by finding the ...
Emma's user avatar
  • 3,517
9 votes
4 answers
3k views

C++ Merge Sort Implementation

Edit: A follow up question can be found here. My first try at mergesort, without resorting to any references: ...
user avatar
5 votes
3 answers
5k views

Natural merge sort

Note: the next iteration at Natural merge sort - follow-up I have compiled natural merge sort algorithm from Java to C++. Natural merge sort sacrifices at most \$2N\$ amount of work in order to ...
coderodde's user avatar
  • 28.2k
5 votes
1 answer
4k views

Selection sort algorithm in Python

I'm learning basic algorithms and implementing them in Python. Critiques needed/welcome. ...
TravMatth's user avatar
  • 505
5 votes
3 answers
325 views

Local heapsort in C++

Suppose we need to sort a sequence and we know that every sequence component is within \$d\$ steps from its correct position. In such a case we can use a local heapsort: we take \$d\$ as an argument ...
coderodde's user avatar
  • 28.2k
5 votes
3 answers
960 views

Quicksort application

What do you think about this code? ...
user avatar
4 votes
1 answer
88 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 ...
t3chb0t's user avatar
  • 44.3k
4 votes
0 answers
111 views

Comb Sort Multi-dimensional Array on Key

Previously: Comb Sort in VBA I've been running two different comb sorts (on arrays) depending on whether I want to sort numbers or strings and I figured I would give using ...
Raystafarian's user avatar
  • 7,139
4 votes
1 answer
2k views

Natural mergesort

This is a problem from Robert Sedgwick's Algorithms book: Write a version of bottom-up mergesort that takes advantage of order in the array by proceeding as follows each time it needs to find two ...
coder_bro's user avatar
  • 419
2 votes
3 answers
400 views

Remove duplicates from array and save it to another one

So I have been tasked to make an array without duplicated values from another existing array. So I did it, but I want to know is there some other better way to do that. Example input/output: ...
Lubomir Stankov's user avatar
2 votes
2 answers
168 views

A package for sort algorithms - v2

Looking for general feedback and praise. This is for learning an not implementation as I would expect the built is sort algos to be much faster. Addressed issues here: A package for sort algorithms ...
user avatar
2 votes
1 answer
286 views

Sorting Algorithms (Python)

Selection Sort The selection sort algorithm sorts a list (array) by finding the minimum element from the right (unsorted part) of the list and putting it at the left (sorted part) of the list. The ...
Emma's user avatar
  • 3,517
1 vote
0 answers
1k 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 ...
teddy's user avatar
  • 11
38 votes
4 answers
7k views

"How are you spending your time on the computer?"

I've made a Windows Forms application to track all of the processes running on my machine and it also saves the time an application is "active", an active application is the one that is on focus ...
Denis's user avatar
  • 8,458
32 votes
10 answers
17k views

Sorting large 1GB file with 100 millions numbers using merge sort

My task is to sort 1GB file with 100 million numbers using merge sort without recursion. My idea is to split file into 4 pieces, then to 2 and at the end to one file. ...
Adrian's user avatar
  • 459
26 votes
5 answers
80k views

Java Implementation of Quick Sort

This is my implementation of quicksort (algorithm taken from Cormen book). This is an in place implementation. Please let us know issues with this or any ideas to make it better. It performs at logN. ...
user830818's user avatar
26 votes
1 answer
16k views

Ford-Johnson merge-insertion sort

The Ford-Johnson algorithm, also known as merge-insertion sort (the name was probably given by Knuth) is an in-place sorting algorithm designed to perform as few comparisons as possible to sort a ...
Morwenn's user avatar
  • 20k
23 votes
5 answers
6k views

Sorting algorithms - Bubble sort

For pure practice purposes, I started implementing different sorting algorithms in modern C++ in a standard library style way (i.e. using templates on iterators). This is my version of bubble sort. ...
Ben Steffan's user avatar
  • 5,228
20 votes
1 answer
660 views

QuickMergesort — The power of internal buffering

Amongst the lesser-known unstable sorting algorithms is the QuickXsort family of algorithms, as described in QuickXsort: Efficient Sorting with n log n - 1.399n +o(n) Comparisons on Average by Stefan ...
Morwenn's user avatar
  • 20k
19 votes
1 answer
2k views

Sorting algorithm visualizer in C++ and SDL2

I finished a sorting algorithm visualizer (for now only uses bubble sort) and this is the code. I'd like some suggestions to improve the memory usage or time elapsed. Built with Cmake and Ninja on ...
Nadpher's user avatar
  • 889
17 votes
5 answers
142k views

Sorting an HTML table with JavaScript

The following code sorts an HTML table with JavaScript (without using any external libraries like jQuery). Are there any shortcomings or possible improvements I could make? ...
Sharanya Dutta's user avatar
15 votes
2 answers
1k views

Is this C++ quicksort program okay?

I wrote a quicksort program in C++. Is it okay to implement it this way? Am I using pointers correctly? Is my style okay? Any ideas to speed it up or save memory? ...
Kolt's user avatar
  • 253
15 votes
3 answers
661 views

Allowing the user to alphabetize or randomize the contents of a text file

As a newbie programmer, I decided to try my hand at making a program whose function actually might be useful to someone, somewhere. This utility takes a text file, reads it out to the user, displays ...
Gabe Evans's user avatar
15 votes
4 answers
9k views

Sorting 10,000 unique randomly-generated numbers

I have to write this program in java. Write a program name sorting.java that will use an array to store 10,000 randomly generated numbers (ranging from 1 to 10,000 no repeat number). Here is what ...
Fernando Martinez's user avatar
14 votes
2 answers
8k views

Parallel Merge Sort

Parallism is achieved by using a simple call to tbb::parallel_invoke(). However I can't break a speedup factor of x2 (after ~3-4 parallel HW threads), although 8 ...
inf's user avatar
  • 564
13 votes
7 answers
6k views

Deck of cards with shuffle and sort functionality

Going through Head First C#'s Chapter 8: Enums and Collections, I learned about List<T>, as well as IComparable<T> ...
Phrancis's user avatar
  • 20.4k
12 votes
4 answers
33k views

Number aware string sorting with comparator

I have this class for use in sorting strings such that if strings have a number in the same position it will order the numbers in increasing order. Alphabetical gives: file1 file10 file2 What I'm ...
pbible's user avatar
  • 419
12 votes
4 answers
43k views

Merge Sort an integer array

I've implemented merge sort an integer array. Is it okay to use i,j,k as variable name for looping? Should I change them to more meaningful names? Overall, any further suggestions on this code? ...
Arun Prakash's user avatar
  • 1,483
11 votes
2 answers
943 views

Milking order compatible with maximum number of observations

I'm using some old USA Computing Olympiad (USACO) problems to help teach me. This is the "Milking order" problem: Farmer John has \$N\$ cows (\$1≤N≤10^5\$), numbered \$1…N\$. He has made \$M\$ ...
silverware's user avatar
11 votes
2 answers
11k views

Sorting a collection

Related to, but not exactly a follow up of this question. After fixing some issues discovered in the last review, I added a little more functionality to the ...
RubberDuck's user avatar
  • 30.8k
11 votes
6 answers
4k views

Mergesort Criticism in C

I have written a merge sort in C and would like any advice on how to improve it. Any advice helps to make the code better! ...
Reggad's user avatar
  • 419
10 votes
3 answers
814 views

In place quicksort

I implemented my own sorting in VBA because it doesn't provide its own, and it's healthy to test yourself. It was surprisingly difficult and I ended up make a few tweaks that I didn't expect to make ...
cheezsteak's user avatar
  • 2,401
9 votes
3 answers
932 views

Quicksort with Insertion Sort and Improved Pivot Selection

Here's a Quicksort I had fun writing and improving, so I thought I'd post it here. In my (brief) testing it's about 15% to 20% faster than Java's Arrays.sort(). ...
markspace's user avatar
  • 522
9 votes
3 answers
5k views

Multi-tiered sorting using custom IComparer

I have this custom IComparer I use to sort a list of SurveyResponse objects, but it seems really inefficient, both in terms of ...
mo.'s user avatar
  • 245
8 votes
3 answers
1k views

Quick Sort Implementation

I have written the below code for quick sort in Java: ...
aj123's user avatar
  • 81
8 votes
3 answers
24k views

Merge sort algorithm implementation using C++

I'm trying to learn proper C++ and algorithms at the same time. I particularly feel weird about my iterators usage in merge function. Is this a good way to handle ...
Inoryy's user avatar
  • 257
8 votes
1 answer
757 views

Constructing an odd-even mergesort sorting network

I was searching for a non-recursive odd-even mergesort implementation and found this one: Sedgewick R. The resulting sorting network is not an odd-even-merge sort network. When one draws a diagram ...
Paebbels's user avatar
  • 587
8 votes
4 answers
142k views

Quick Sort - implementation

Can anything be improved in this code? ...
Software Engineer's user avatar
8 votes
3 answers
2k views

Reorder a list of courses, given their prerequisites

I recently graduated from college and applied for a position with a company. I was sent a coding exercise as part of the process. After completing the exercise and submitting it I was told that the ...
user1331369's user avatar
8 votes
3 answers
2k views

Selection sort algorithm with increasing/decreasing sort options

I am running through some traditional algorithms in Go, and I was hoping to get feedback on efficiency and optimization. Below is a basic selection sort algorithm that takes a parameter to dictate ...
fr00z1's user avatar
  • 514
8 votes
5 answers
31k views

Checking if an array is sorted

I've come up with two ways to check to see if array is sorted and return true if it is. ...
Celeritas's user avatar
  • 911
8 votes
2 answers
3k views

String merge sort in Java

This snippet is about sorting a set of strings using merge sort. However, this version is tailored for strings as it relies on lcp values: Given two strings \$A\$ ...
coderodde's user avatar
  • 28.2k
7 votes
3 answers
896 views

Checking an array is sorted in either order efficiently

Here is my code, it works fine, just wondering if there any anything to make it even better. ...
User_67128's user avatar
7 votes
3 answers
35k views

Performing insertion sort in C#

This is my code in C# for performing insertion sort: ...
Rohit's user avatar
  • 1,066