Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
3
votes
1answer
283 views

Adaptive Mergesort in Java

(See the next iteration.) This natural merge sort here is from "Sublinear Merging and Natural Mergesort" by Svante Carlsson, Christos Levcopoulos and Ola Petersson. The funky part is that it may ...
4
votes
3answers
2k views

python (optimizing) bubble sort

I saw in Wikipedia (the first under Optimizing bubble sort) a pseudo-code to bubble sort and I implemented it in Python. I am posting my implementation here because I'm new to Python, and I'm looking ...
5
votes
1answer
106 views

Author class, with constructor validations and rich comparisons

I wrote the following Author class in Python for a project I'm working on. I wanted to make sure that the rich comparisons and the methods I used to validate the constructor parameters were done ...
3
votes
1answer
641 views

Measuring the time for the bubble sort algorithm

I created a program that measures the time for the bubble sort algorithm to sort a list of numbers: ...
1
vote
0answers
454 views

Sorting directed graph topological using Kahn's algorithm

For a course I'm doing I need to get up to speed with Java, but unfortunately only Java 1.6 - here is a topological sorting algorithm in said Java version, for practice reasons: ...
1
vote
2answers
1k views

Selection Sort in C++

I wanted feedback/review on my implementation of a selection sort in c++ (for ints only, no templates). This is my first attempt in c++ making anything beyond simple projects/hello world! Q: Should ...
3
votes
2answers
74 views

Sort a file on the first two columns, with optional filtering

I am a newbie to Rust and low-level coding. I come from data science and I have always used scripting languages so I may have done weird or useless things (with lifetimes and references). This is ...
2
votes
0answers
187 views

Comparing speed of common sorting algorithms in an extensible way

I have written a program with the goal of comparing the speed of some common sorting algorithms. It works with many data types. ...
5
votes
3answers
891 views

Sorting an array of objects by a property which may be missing

Problem I am sorting an array of objects. Each element may have a property called reference or it may be undefined. When ...
1
vote
2answers
486 views

Common Sorting Algorithms

Here I've prepared my solutions for the following algorithms, and was curious if there were a way to optimize any of them. Any input is welcome and appreciated! ...
6
votes
1answer
218 views

Finding added keys, removed keys, and unchanged keys in ordereddict

I'm comparing two OrderedDict objects and I need to find added keys, removed keys, and keys that are present in both (the intersection). Sets are designed for ...
6
votes
2answers
85 views

Ensuring Sequence of Events in an Array

The Problem I'm asking to see if there are methodically better solutions to this problem. The situation is as follows, I am receiving an array of events, they are marked by timestamp and there is a ...
3
votes
1answer
94 views

Algorithm to sort a list in Python

Today I've been working on this algorithm to sort a list. It works with duplicated numbers. What do you think about it? How can I improve it? Does it already exist? If it does, what is its name? <...
3
votes
2answers
114 views

Alphabetically sorted numbers

This exercise involves taking an array of numbers and sorting them alphabetically. For example, [1,2,3,4] would return as [4,1,3,2] I mapped the array first, returning an array of words, then sorted ...
3
votes
1answer
80 views

Find all identical data in a Column and filter it to another sheet

I have spent hours on this code, and truthfully need some better expert opinion. Column A on Sheet 1 has dynamic list of data, typically IP address, but for this it is simply a number. There can be ...
2
votes
3answers
1k views

Topological sort of a graph

Please refer following graph. Please review following code for topological sort. I can sense one odd thing in my code, TS() is calling ...
1
vote
1answer
298 views

Segment sorting

This is a question from careercup.com. You are given an alphanumeric string. Complete the function sortSegments that will segment the string into substrings of consecutive letters or numbers and then ...
2
votes
1answer
783 views

Merge K Sorted List

Merge k sorted linked lists and return it as one sorted list. The definition of the ListNode class and the signature of the ...
2
votes
1answer
63 views

Parallel curvesort in Java

What? I have designed curvesort, an algorithm that adapts to "smoothness" of data. For example, if the data resembles a sine wave, it is likely that curvesort will sort it fast. This post is about a ...
5
votes
0answers
136 views

Selection sort with reduced comparison count: Python iteration 2

Follow up to Selection sort with reduced comparison count - semi-final Iteration? My goal (and excuse not to tag reinventing…) is to have presentable code to argue the viability of reducing the number ...
4
votes
2answers
287 views

Selection sort with reduced comparison count

This is an attempt to show how to use fewer comparisons in selection sorting a sequence of \$n\$ items than the \$(n²-n)/2\$ naive selection sort uses. This is a first Python re-iteration of a Java ...
4
votes
2answers
77 views

Find the best pairs of elements based on match weight

While answering this question on SO I came up with a very simple solution and I wanted to know if there were improvements that could be made to it. The function takes an array of participants as ...
1
vote
3answers
2k views

Topological sort using STL

I am trying to implement topological sort using STL. I have used vector of lists to store adjacency list. Indegree is an array which stores indegree of vertices. ...
1
vote
1answer
208 views

Selection sort algorithm in Swift

I have written selection sort in Swift. Sort([2,4,23,268,9,0]).bySelection() I am looking for ways to further improve this programme using Swift language ...
3
votes
2answers
137 views

Shell-sort algorithm complexity

I tried to implement the shell-sort algorithm using this Java code: ...
2
votes
2answers
383 views

Selection Sort implementation using modern C++

I have written this code for Selection sort. I want to use more C++11, C++14 or C++17. Please sugest some better alternatives. ...
0
votes
1answer
335 views

C++: Merge Sort Implementation

I have this code for Merge Sort. I want to improve this code. I have learned Here that we should use Iterartor so that we can use any container. How to use iterator in this code. ...
11
votes
3answers
7k views

Return characters in a string in alphabetical order

Problem Take a string and return all input characters in alphabetical order Assume that no numbers or punctuation will be used For the same character, uppercase characters should be returned ...
1
vote
2answers
701 views

Python bubble sort algorithm

How can this algorithm be improved? I mean, a list with 3000 items takes about 5 seconds to sort, according to Sublime Text. Using Python's sorted function is ...
6
votes
1answer
1k views

Sorting ListView Columns

I use these codes to re-order columns in a ListView in ascending or descending order: ...
2
votes
2answers
135 views

Given an arbitrarily large file containing integers, return the largest N numbers, highest first

I've written a top_n program in Ruby, which does pretty much exactly what the title says, as part of a coding exercise. I'm trying to learn about how to sort ...
3
votes
4answers
7k views

Sorting dictionary alphabetically

I have this method which successfully sorts the incoming dictionary alphabetically: ...
7
votes
3answers
2k views

Sorting elements according to frequency of occurence in C++

I am sorting the elements in an array according to their frequency of occurrence in decreasing order and if two elements have same frequency then they are sorted in increasing order. I have used ...
2
votes
3answers
381 views

Naive, bubble, insert and quick sort in C

I've created a program that shows the time needed to sort a random array of integers. An example is: ...
1
vote
5answers
1k views

Implementation of Counting Sort using C++ [duplicate]

Please review my code. If any improvement needed please point out. I have used std::map. Is there any better alternative? ...
2
votes
3answers
2k views

Sorting an array of strings using pointers (followup)

This is a follow-up to this post. Things I've changed: The number of strings is checked. I'm using pointers now. I'll use fgets in more mission critical ...
3
votes
1answer
4k views

Sorting an array of strings in C using insertion sort

I am a Java programmer who has C this semester. Needless to say, my writing style is heavily influenced by Java, which is probably not a great thing. I'd be grateful for a few nudges in the right ...
-1
votes
1answer
90 views

Sorting elements of array in ascending order

I made this program which sorts the elements of an array in an ascending order and then prints out the sorted array. ...
5
votes
0answers
170 views

Object-Oriented Sorting Algorithms

I would like a review of a library I've recently published. I realize this is a rather large request, but having looked on meta, it seems this is perfectly on-topic. I'll include a few choice samples ...
2
votes
2answers
153 views

Recursive and Non-recursive SelectionSort

This is a naive attempt of writing a recursive and non-recursive versions for SelectionSort(). My goal is mainly to present an elegant, easy-to-understand, ...
2
votes
2answers
169 views

Sorting an array of bands by omitting articles

I've written a simple script that takes an array, sorts it (by omitting the articles) and displays it on a list. But I feel like this code is really huge, bulky and messy and could be simplified! ...
3
votes
1answer
4k views

Finding the minimum number of swaps to sort a list

I can handle large numbers with this code as well but I need to improve my execution time. ...
4
votes
2answers
422 views

Count sort implementation

Is this a proper Count sort implementation? Code and style improvements? It does not support negative and that should be improved. A large input could cause out of memory. Some of the array index ...
3
votes
1answer
291 views

Sort an array by value and index in C

Your task is to sort an array of integer numbers by the product of the value and the index of the positions. For sorting the index starts at 1, NOT at 0! The sorting has to be ascending. The ...
-5
votes
1answer
196 views

Category of sorting algorithm and complexity [closed]

Just for fun, I wrote a sorting algorithm. I did not compare this with existing sorting algorithms.Can someone tell me what class of sorting algorithm this is and it's complexity? The algorithm ...
10
votes
2answers
1k views

In-Place Insertion Sort

Purpose Implementation for in-place Insertion Sort. Discussion The idea is to start with the second element in the input array (in the single element case, it's already sorted). Moving backwards, ...
1
vote
1answer
97 views

Large switch statement bucketing system

I've finally got a solution for a "weekly streak" bucketing system I've been working on but unfortunately it's resulted in a very large switch statement. I'm running through a handful of dates and ...
7
votes
3answers
81 views

Select member for comparison in sort or search

This code is predicated on the notion that when sorting (or using a sorted collection of items, such as with std::lower_bound) you're usually comparing a single ...
1
vote
1answer
251 views

Insertion Sort in Python3.6

Can this insertion sort program be improved in any way? Please suggest something in terms of efficiency and lines of code as well. ...
3
votes
1answer
2k views

Calculating the most profit from an array of stock prices

I'm a newbie learning how to code, and I am trying to find the most efficient way to calculate the most profit from an array of stock prices. For a given array,I have a list of stock prices over n ...