Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [sorting]

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

0
votes
0answers
42 views

Sorting CSV table in Powershell

I'm wondering if some PS gurus can advise on whether my method for sorting and recombining some data fields by using an array of PSCustomObjects is as efficient as it might be. I'm importing a CSV ...
2
votes
1answer
29 views

Java 8 Stream, How to get Top N count?

I need your advice to simplify this code below. I have a player list with an ID of the games won. I want to extract the 2 best players from this list (the 2 players who have a better amount of match ...
-1
votes
0answers
28 views

logical error in using compare() in C++ [closed]

To keep the post short, here is the minimal information that you need to understand the code. The program is expected to read in words from command line and output individual words(spaces seperate ...
6
votes
2answers
309 views

Find the intervals which have a non-empty intersection with a given interval

The problem is the following: I have a list intervals which consists of tuples of the form (start, end) [with ...
1
vote
1answer
72 views

C++ - Radix sort implementation

I have the following radix sort implementation: ...
5
votes
2answers
157 views

Deficient Numbers

I have found the following interesting challenge on the web: Deficient Numbers A number is considered deficient if the sum of its factors is less than twice that number. For example: 10 is a ...
5
votes
2answers
631 views

Sort characters in a Python string by frequency

I wrote a solution to the leetcode problem Sort characters by frequency and the solution passes 34/35 test cases. On the last test case, there is a "time limit exceeded" error with an input string of ...
0
votes
2answers
60 views

ContactsList implementation in Java

I created a ContactsList class in Java that holds Person objects. I consider this is a side project for my GitHub profile. Any ...
2
votes
4answers
67 views
7
votes
3answers
940 views

Sorting three ways

I have implemented function templates that take a begin and end iterator as well as an optional comparator for user-defined types. It was my intention to mimic the usage of ...
1
vote
0answers
19 views

React widget to display, sort and search remote data

I've created a React widget that pulls data on crypto-token pairs from a remote API and makes that data sortable by headers and searchable. You can view a demo of it here. Is this a maintainable way ...
7
votes
2answers
114 views

C++2a comparison-based topological sort algorithm

Here's my attempt at a C++2a Standard Library–friendly "topological sort" algorithm. There are several areas of interest here: The algorithm is comparison-based and in-place, just like ...
4
votes
1answer
80 views

Minimum swaps algorithm terminated due to timeout

I have been trying to solve this question. Given an unordered array consisting of consecutive integers [1, 2, 3, …, n], find the minimum number of two-element swaps to sort the array. I was able ...
5
votes
5answers
252 views

Sort an array of 0s, 1s and 2s in Java

Write a program to sort an array of 0's,1's and 2's in ascending order. Input: The first line contains an integer 'T' denoting the total number of test cases. In each test cases, First ...
3
votes
2answers
58 views

Sort digits in Python

Given an integer, sort the digits in ascending order and return the new integer. Ignore leading zeros. Parameters: Input: num {Integer} Output: {Integer} ...
4
votes
1answer
45 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
37 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
81 views

String comparator as a lambda

I am writing a Comparator for my TreeMap. It looks like this: ...
3
votes
1answer
45 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
70 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
198 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 ...
6
votes
3answers
540 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
1answer
102 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
40 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 ...
8
votes
1answer
63 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 ...
5
votes
3answers
99 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 accomplish this is allocate the size ...
4
votes
3answers
132 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 ...
0
votes
0answers
70 views

Badminton doubles automatic game-making GUI (Python 3, Tkinter)

This is a program I created for my social badminton club. I've used it for the last few weeks and it's worked fairly well so far. The full code is on Github here. We operate with three courts (each ...
1
vote
1answer
51 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
753 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
122 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
99 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
103 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
165 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
67 views

Compares two Log Lines

I am trying to compare two Log lines based on some conditions, in order to sort them: Code: ...
4
votes
0answers
85 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
41 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
167 views

Implement Insertion sort in Python

Task : Implement Insertion Sort in Python. ...
3
votes
3answers
76 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 ...
1
vote
2answers
107 views

Python Bubble sort code

I have written the below bubble sort code in Python. ...
2
votes
2answers
97 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
2k 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 ...
10
votes
1answer
95 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
43 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
141 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
387 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
99 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
44 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
168 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
566 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 ...