Questions tagged [sorting]

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

Filter by
Sorted by
Tagged with
4
votes
1answer
66 views

Sorting software version list using Python 2.7 (no modules)

I am participating in a challenge and one of the challenges was to write a script to sort a list of software versions (ex: 1.0, 1.3.2, 12, 1.3.0). Here is the my solution: ...
-2
votes
1answer
38 views

Is there a better way to sort this items?

I already have my code working, but I believe that could be improved. This is basically the main part, where I re-order a list of items; ...
3
votes
3answers
93 views

String comparator as a lambda

I am writing a Comparator for my TreeMap. It looks like this: ...
3
votes
1answer
103 views

Counting sort in C, revised

This is a revised follow-up to this question. I have implemented most of the suggestions in the accepted answer. I am interested to see what I could improve in my code and what I could do to make it ...
3
votes
2answers
161 views

Counting sort in C

EDIT: I have posted a follow-up to this question. I have implemented counting sort in C. This program takes its input as integers from command line arguments, sorts the integers with counting sort, ...
4
votes
5answers
701 views

Sort string's characters into alphabetic order

Any improvements that I can make with this code? Also, is there another way to convert a vector to a string variable without using a for ranged-based loop? From the posts I've seen for converting, I ...
3
votes
2answers
2k views

Bubble sort in Haskell

The following code is part of my practice in implementing algorithms in Haskell. I'm aware that bubble sort is a bad choice for sorting sequences in real applications. ...
6
votes
3answers
550 views

C++ code to read, sort, and write data

I had written code in C++ to create transformation filters to get input, sort (custom_sorting and existing sorting routine) and write the output to stdin and file. When I presented it to my teacher, ...
1
vote
2answers
143 views

Checking whether two lists contain the same elements

I have two lists of numbers: $$\{i_1, i_2, i_3,i_4\}$$ and $$\{j_1,j_2,j_3,j_4\}$$ The problem I want to solve is: Inside a loop, if the following two conditions are satisfied, then it will do ...
2
votes
0answers
53 views

Mergesort in Python optimization

I am currently writing mergesort in Python and have implemented it to work. I have noticed, though, that it is very slow compared to the standard sorted() method. I understand it will always be faster ...
7
votes
1answer
101 views

Implementing and benchmarking Radix Sort on unsigned integers

Introduction I implemented a restricted version of the sorting algorithm, the one that works only with unsigned integers. I wanted to try using ...
6
votes
3answers
182 views

Print strings in order with dynamic arrays

I accomplished the following task to practice allocating on the heap. reading words from stdin sorting them in lexicographical order printing the sorted words to stdout To accomplish this is ...
4
votes
3answers
137 views

Sort a linked-list with only three distinct values

I am trying to solve this problem. The problem is to sort a linked list containing only 0s, 1s and 2s by changing their links. Sort a linked list of 0s, 1s and 2s by changing links Given a linked ...
1
vote
1answer
136 views

Implementation of counting sort

I'm trying to learn more about counting sort and I just implemented the example given in CLRS, my question is: How can I improve this code? ...
11
votes
2answers
787 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\$ ...
-1
votes
2answers
151 views

BubbleSort - checking if bubblesort is decreasing the iterations through the array

What I'm hoping my code is doing is instead of making 99 comparisons on every pass of a 100 element array, it will now make 98 comparisons on the second pass, 97 on the third pass and so on and thus ...
3
votes
2answers
309 views

Mergesort linked list in C++

My teacher gave us an exercise. We have to sort as quickly as possible. The fastest one gets a reward. On my own system, I am sorting 125000 words within 1.2 seconds. On my teacher's system that is 0....
3
votes
2answers
149 views

Check whether array is sorted recursively

I'm trying to implement isSorted method checking whether given array is sorted recursively. I've written two types, one is similar to merge sort logic, the another ...
0
votes
2answers
363 views

Pancake sort inteview using java

Given an array of integers arr: Write a function flip(arr, k) that reverses the order of the first k elements in the array <...
3
votes
2answers
70 views

Compares two Log Lines

I am trying to compare two Log lines based on some conditions, in order to sort them: Code: ...
5
votes
1answer
332 views

Multi-threaded in-place Mergesort on Java

What is an efficient way to implement the Mergesort algorithm in Java such that it meets the following criteria: Must be multi-threaded. Must retain the Mergesort time complexities. Must be in-place ...
4
votes
2answers
324 views

compareTo method for names

I'm implementing the compareTo method to set the natural order of an object. My object has three fields that make up a name. fName, lName, middleInitial. The natural ordering should be by lName, fName,...
4
votes
4answers
587 views

Implement Insertion sort in Python

Task : Implement Insertion Sort in Python. ...
3
votes
3answers
82 views

Selection and Bubble sorts

Just to enjoy, I want to implement selection and bubble sorts algorithms without looking any implementation by my own efforts 💪💪. Is there any redundant/lack part(s)? After improved their ...
2
votes
2answers
556 views

Python Bubble sort code

I have written the below bubble sort code in Python. ...
3
votes
3answers
157 views

Angular, sort objects by their keys

I had to sort a list of objects - {name:'name', email:'email} by their keys, ex. by email, so they all be rendered with ...
17
votes
2answers
3k views

Pancake sort interview with Python

I got asked this question during my interview. You can find information about this question here. Pancake Sort Given an array of integers arr: Write a function flip(arr, k) that reverses ...
11
votes
1answer
125 views

Composable by-member comparison

Motivation Quite often, I find myself creating types that must be compared memberwise. For example, consider this simple Person: ...
3
votes
1answer
52 views

Sorting by checking 2 distance away elements in a list, and check the method's accuracy

This code works as follows : It accepts number of test case T. For each test case, it accepts length of sequence of integers N and the sequence (or list) itself L. The goal is to sort the list by ...
8
votes
2answers
282 views

Shortest distance for points in a plane

I was wondering how I can make this code more pythonic. More professonal-looking and more readable. The code runs perfectly fine. ...
5
votes
2answers
390 views

Sorting items in list based on position

I wrote code which sorts items, based on parameters (IconConfiguration). I have a map of items which have index and item so basically a predefined list on which index item should be, and maxPositions ...
5
votes
1answer
249 views

Optimizing priority queue streaming algorithm in C++

This is a sorting algorithm that I wrote for finding the maximum \$n\$ elements at any time in a stream of IDs and integers with unknown length. I primarily do my coding in Python, so I'm not too ...
1
vote
2answers
69 views

Mergesort, instrumented for counting reads and writes

I have just finished an implementation of mergesort, and I was wondering if I can further reduce the amount of reads and writes. I have looked at it for a while, and to my eyes, I cannot see anything ...
4
votes
1answer
397 views

Nearest Neighbor Algorithm (general nearest, forward-nearest, or backward-nearest)

I am trying to write an algorithm that can find user-specified nearest neighbors. By user-specified, I mean that the user can specify whether it's a general nearest neighbor, a forward-nearest ...
2
votes
0answers
2k views

Bubble sort using user inputted numbers and size in MIPS assembly

I made this code to take user input, making a specific-sized array and printing user-inputted numbers after being sorted. It works well but I was wondering if there was a way to "shrink" it down. It ...
3
votes
2answers
430 views

“Almost Sorted” coding challenge

The "Almost Sorted" problem from HackerRank: Given an array with \$n\$ elements, can you sort this array in ascending order using only one of the following operations? Swap two elements. ...
5
votes
2answers
681 views

Sort an iterable in Python 3.6

I was looking through some popular questions on SO, and one of them was How do I sort a dictionary by value?. Because Python 3.6 now gives us dicts that maintain ...
3
votes
1answer
227 views

Sorting a 2-dimensional array with counting sort

The task I'm solving is: Sort a list by its keys Print out the corresponding values on one line separated by whitespace Replace the values of the first half of the input to "-" Use Counting Sort The ...
-2
votes
1answer
91 views
10
votes
1answer
1k 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 ...
0
votes
1answer
43 views

Create numerical-key array from existing string-key array, with a given string-key as first item

I have an array of available languages for my website, with a short ISO country code as the key for each language. I need to create a new array that contains the same info, but with numerical keys and ...
5
votes
2answers
187 views

Counting adjacent swaps to sort an array with 3 different values

This is the Kindergarten Excursion problem from kattis.com: The kindergarten teachers had finally managed to get all the kids in a line for the walk to the bus station and the weekly excursion. ...
1
vote
0answers
63 views

Sorting a list of IComparables

I have a DateTime class (my own, not System.DateTime) which implements IComparable. Here is ...
1
vote
1answer
161 views

Pre Java 8 a lot of Comparators implementation

Looking for critiques on my implementation of a list of countries that offers many ways to sort it in a useful and easy way for a client. The main concern is about the design of the enum that holds ...
1
vote
1answer
95 views

Custom sorting algo / optimized bubble sort

I'm a novice and started diving into data structures and algorithms recently. I was taught bubble sort recently and I found that the original bubble sort algorithm is so inefficient, so I wrote a ...
1
vote
1answer
62 views

Copying and separating data by Excel Field

I created this code that's meant to pull a specific report that's been saved in a folder clean up (delete) the data and add new titles, divide the information up by one of the fields, then copy the ...
1
vote
0answers
210 views

Merge two unsorted array of numbers into sorted array using divide and conquer

I have done an implementation of merging two unsorted array of numbers into single sorted array. I have used the concept of divide an conquer. The code is as follows ...
2
votes
3answers
106 views

Quick sort with a predicate

I want to pass a predicate into quick sort. The issue I'm running into is that the predicate must have two arguments, the pivot and the element of the list's tail. As far as I know though, predicates ...
3
votes
2answers
608 views

Merge the two sorted array of numbers into single array

I have written a code were I pass two sorted array of numbers from command line, and out is one array with numbers sorted. the code is as follows, ...
2
votes
2answers
795 views

Implement bubble sort in Python

I have written the following code for bubble sort. How can it be optimised? ...