Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
5
votes
1answer
2k views

Count array inversions merge sort

How does it look? Comments, code correctness, etc. ...
3
votes
2answers
2k views

Method for sorting numbers from user input

I have an input with numbers 1, 2, 3, 4 and I want to change the order of this number dynamically according to users input. You can find my code below and also a running example here. HTML ...
3
votes
1answer
497 views

Sort by value in multidimensional array

I have a multidimensional array of house properties: ...
4
votes
3answers
1k views

Sorting a flattened array

I'm using C# in .NET 4. The class holds an array of double that represents a 2-D rectangular matrix but (due to business requirements) must be stored flattened (i.e....
3
votes
3answers
772 views

Merge Sorting in Python

This my implementation of the Merge Sorting Algorithm: ...
2
votes
2answers
2k views

Merge sort algorithm implementation using C++ (redux)

This is a follow up question to the one I asked a couple of days ago. I reviewed the suggestions & did some research (specifically, read relevant info on CLRS). The new version is 3 times faster (...
3
votes
3answers
319 views

Speed up counting sort algorithm in Ruby

I'm attempting this simple HackerRank counting sort problem but testcase #3, which has 100,000 test cases, is timing out. Is there something particularly inefficient about this code? ...
6
votes
2answers
10k views

Sorting an array in alphabetical order

I'm looking to get some feedback on this program I wrote - ways I could make it simpler or best practices methods. Chapter 7: Let's write a program which asks us to type in as many words as we want (...
8
votes
3answers
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 ...
4
votes
2answers
2k views

How might we make this Python Merge Sort implementation more Pythonic?

I've implemented a (version of) Merge Sort algorithm in Python. My goal here is two-fold: Improve understanding of Merge Sort and recursion in a language agnostic way. Improve understanding of Python ...
2
votes
2answers
136 views

Is this reimplemented quicksort correct?

...
6
votes
2answers
8k views

Stable custom alphabetical order using List<T>.Sort

Background This is a problem from a programming contest. The Sharonians of planet Sharon, at the far end of our galaxy, have discovered various samples of English text from our electronic ...
1
vote
3answers
85 views

Activity by length

I was having a hard time finding out how to make this any faster. I broke down the code that I am playing with into more methods than necessary to try and see where things could increase. Can anyone ...
3
votes
1answer
550 views

Sort 3x3 grid by rotating 2x2 subgrids

This question originates from this post. Anyway, I'm trying to solve the following problem: Given a 3x3 grid with numbers 1-9, for example: 2 8 3 1 4 5 7 9 6 I ...
4
votes
3answers
20k views

Implementing the merge sort with only arrays, out of place

This has been asked a few times here, but I was wondering what I could do to improve the efficiency and readability of my code. My programming skill has gotten rusty, so I would like to elicit all ...
3
votes
3answers
2k views

Quicksort implementation in C#

Okay, so I'm a self-taught programmer and have been brushing up on all the 'boring' bits that CS students do that most of us self-taught ignore. Things like Algorithms and Data Structures (...
2
votes
5answers
450 views

An acceptable way of sorting List

I had a problem where I needed to find the largest amount of passengers in multiple cars/vans etc, so I got the totals from each vehicle and added them to a list, I then created this method to sort ...
2
votes
1answer
555 views

Merge Sort optimization

I was looking at other people code and I realized that mine is not so neat after all. In this version, I included a base case for the merge part (when there is only 2 elements to merge, just sort it ...
3
votes
1answer
225 views

Is there a better way to return values with merge sort?

I have been attempting to learn Scala lately and so have produced a couple of different sorting algorithms to build up my basics. I think I have been lucky with the methods returning exactly what I ...
9
votes
2answers
40k views

Count frequency of words in a given file

Write a function that takes two parameters: a String representing a text document an integer providing the number of items to return Implement the ...
2
votes
1answer
780 views

Quick sort implementation in JavaScript in prototype style

I have implemented quickly the quick sort algorithm in JavaScript using prototype based style. The code is rather short (http://jsfiddle.net/b8mfY/): ...
4
votes
1answer
777 views

Insertion Sort implementation in Java

I have implemented Insertion sort in Java. Please review it. ...
10
votes
3answers
756 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 ...
8
votes
1answer
229 views

Sort function to sort item numbers separated by colon

Below is my code. This is a function that will be applied as a sorting method to a grid based data set. The data in the column to be sorted can be stuff like: 123456...
4
votes
2answers
3k views

Multicriteria sort array

I have an array of objects that I must sort based on two criteria. I've come up with a solution but I'd like to know if there is a better way to do it. Assume a list of devices: ...
17
votes
3answers
9k views

Insertion Sorting in C++

From Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, ...
20
votes
1answer
20k views

QuickSort C# Implementation

It has been a while since I've implemented a QuickSort and I wanted to write it down like I remembered. ...
12
votes
1answer
2k views

Generic radix sort

This started out with my answer to Radix Sort on an Array of Strings?. Since I intend to write a generic radix sort for my own purposes anyway, I continued a little bit, and here is a version tested ...
15
votes
4answers
4k views

Least significant digit (LSD) radix sort of strings

I am learning string algorithms and wrote this C example based on a Java example I found online. Any feedback relating to style, code clarity, whether it is generic enough to be re-used, interface, ...
4
votes
1answer
165 views

Is this Shell sort?

Trying to learn shell sort, I kinda understood what wikipedia was trying to say, but I wanted a bit of confirmation, so I went on youtube, here, but this code does not work and does not look like what ...
12
votes
1answer
3k views

Bubble Sort in C: Array vs Linked List

A few weeks ago I posted a code review of a linked list so I decided to implement bubble sort on my journey of learning C. Here is the referenced linked list library, though this isn't really what's ...
3
votes
1answer
3k views

Google Maps JavaScript API v3: Sorting Markers with Check Boxes

Recently I built a map with custom markers (pins) and a sorting function to display each by type (Member, Supplier, Prospect, New Member). The HTML (check boxes wired up with ...
6
votes
1answer
218 views

Merge sort could work 10 times faster

I am implementing merge sort in Java and I have a performance problem in this method: ...
5
votes
2answers
466 views

How to verify if HTML table sort is stable or not?

I have implemented a HTML table sorting. I actually implemented a sort first using jQuery and then modified it based on the answer to this question for performance as I had done the same mistake. My ...
8
votes
1answer
152 views

Sorting a PHP array in a discontinuous manner

I start with an array containing some 'ids' and some 'values'. What I need is to organize the 'ids' in a new array to be, if possible, apart when the 'values' they are associated with are alike. So ...
0
votes
1answer
1k views

Why is my Bubblesort implementation faster than my Quicksort code? [closed]

Here is my Bubblesort code: ...
3
votes
1answer
1k views

Time optimization of counting shifts in insertion sort

Is there any way of counting how many shifts would it take to count shifts while doing insertion sort? This is a Hackerrank problem. Here's my solution, but it takes more than 16 seconds. ...
5
votes
3answers
681 views

How can I make my isSorted function faster? [closed]

I made an isSorted function in Scala and Java as well and when I measured the time of functions' run I saw that the Scala version was very slow. It ran about 3.2 ...
3
votes
1answer
186 views

Sorting possibly unstable and general jsfiddle/jquery problem

I have a simple sorting algorithm that I was told is possibly unstable. I'm wondering what in the sort is unstable. I have test cases written to test all fields with no issues. A secondary problem is ...
5
votes
1answer
1k views

External “bucket” sort

I'm working on a CTags plugin for Sublime Text. One of the open issues revolves around the fact that an in-memory sort of a tag file can cause some form of memory or buffer overflow errors (the stack ...
8
votes
2answers
747 views

Quicksort implementation

I put together this implementation after playing around with various partitioning- and pivot-strategies, and it runs fine. I wrote this off a version which seemed a bit different from the majority of ...
7
votes
4answers
550 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 ...
7
votes
1answer
627 views

How can I speed up my shell sort?

I'm trying to implement a bunch of sorting algorithms in JavaScript, and I can't figure out why my shell sort is so slow. It's 6x slower than my merge sort, and only slightly faster than my insertion ...
4
votes
2answers
4k views

Produce a nearly sorted (or K sorted) array

Given an array of n elements, where each element is at most k away from its target position, devise an algorithm that sorts in O(n log k) time. For example, let us consider k is 2, an element at ...
5
votes
1answer
453 views

Is this merge sort code good enough?

...
3
votes
1answer
6k views

Insertion sort with binary search in Python

I have a bit of trouble simplifying the binary search code. This is different from the traditional binary search because we effectively want to find the location for which ...
7
votes
2answers
197 views

Is this quicksort efficient and acceptable?

In order to complete one of my assignment I needed to include a quicksort so I was wondering if this is correct and acceptable. The code: ...
2
votes
1answer
3k views

Approximating/Sorting groups of dates into buckets

I'm working on a directed study project with a professor of mine helping him build out a Django based website. The website is a historical account of the people, events, projects, and organization in ...
8
votes
5answers
19k views

Merge Sort Algorithm in Python

I'm implementing basic sorting algorithms to learn them, and coding, better. Criticisms and possible optimizations welcome. ...
2
votes
2answers
8k views

How to sort two array of objects using Underscore.js? [closed]

I am sorting 2 arrays of object like this, but I know this is very bad. Any good approach, like using each method like so? ( I tried, but using each, I am unable to return the object ) ...