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
1answer
33 views

Insertion sort algorithm in Java

I have tested this code and it yields the expected outputs, but I want to verify if I have applied insertion sort algorithm correctly or not. ...
2
votes
2answers
51 views

Chained Sort Python Implementation

So I've run into a few occasions where I'd want to sort a list according to a criteria, then for all loosely sorted sublists (a sequence of equal elements), I'd want to sort those sublists according ...
6
votes
2answers
62 views

Rearrage sequence so that adjacent numbers add to perfect squares

Consider the array: original_array1[17] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17} I want to sort it in such a way that: Sum of adjacent numbers should be a ...
0
votes
2answers
62 views

Select nth smallest distinct integer from inputs

Kata in question Given a list of integers, return the nth smallest integer in the list. Only distinct elements should be considered when calculating the answer. n will always be positive (n > 0) ...
0
votes
0answers
38 views

Bucket sort algorithm in JavaScript

I was asked to implement bucket sort recently from my friend. Bucket sort is mainly useful when input is uniformly distributed over a range. For example, consider the following problem. Sort a ...
3
votes
1answer
46 views

SplitSort — An adaptive algorithm to handle collections with few inversions

SplitSort SplitSort is a rather simple Inv-adaptive and Rem-adaptive sorting algorithm described in Splitsort — an adaptive sorting algorithm by Christos Levcopoulos and Ola Petersson. The paper ...
2
votes
1answer
73 views

Shuffle deck of cards in JavaScript

I know randomness in programming can be a deep topic, and searching the topic seems to result in Fisher-Yates being the best method, but I'm curious what people would say about this method. Basically,...
1
vote
2answers
71 views

Insertion Sort and Selection Sort Implementation

I wanted to practice using templates since I have no experience with them, so I implemented these sorting algorithms. Selection Sort: ...
2
votes
1answer
49 views

Most efficient way to find spatial order from a list of tuples (Python)

I have a circle-growth algorithm (line-growth with closed links) where new points are added between existing points at each iteration. The linkage information of each point is stored as a tuple in a ...
4
votes
2answers
107 views

Sorting an array of numbers by descending frequency

Somebody asked me to create a code that orders unique numbers of an array according to their frequency, ie: {1,3,3,4,4,4,4,4,2,2,5,5,5,5} to ...
0
votes
2answers
35 views

Can this be called an insertionSort?

Can following code be called insertion sort? I tried implementation according to my underdtanding... ...
1
vote
1answer
41 views

Sorting algorithms in Kotlin - Bubble, Insertion, Selection, Merge and Quick sort

Introduction I decided to try Kotlin because of its forgiving nature to long-time Java users. I implemented a few introductory sorting algorithms and would like to have them reviewed, not just to ...
2
votes
0answers
52 views

Generate automatically new groups in a tournament

I'm trying to model a tournament with Python (Django) and I've got a function that assigns teams of the previous stage to the groups of the new one. The teams in the first stage are assigned manually....
4
votes
1answer
142 views

Sorting an array in C++

This started as a simple exercise in sorting an array. I wanted to make it a bit more complex which is why I made it a 2D array. I then noticed that each pass that checked and swapped values, it was ...
1
vote
1answer
42 views

Simple multithreading C project

I'm fairly new to C, and this is my first time using pthread. I am pretty sure I implemented it correctly, however I'd like to verify it is indeed executing the ...
6
votes
2answers
117 views

Lexicographically largest string from allowed swaps

Problem: Given a string str and array of pairs that indicates which indices in the string can be swapped, return the lexicographically largest string that results from doing the allowed swaps. You ...
4
votes
1answer
90 views

Natural merge: mergesort that uses already sorted subarrays

The code bellow is my implementation for the natural merge exercise in Robert Sedgwick's Algorithms book: Write a version of bottom-up mergesort that takes advantage of order in the array by ...
1
vote
1answer
50 views

A 'slow sort': repeatedly swap the first misordered pair

A 'slow sort' is probably the slowest practical sort. It compares each element with the elements after it, and swaps immediately if one is greater than the other, and goes back to the start of the ...
2
votes
1answer
48 views

Merge sort implementation with various improvements

So I was working on merge sort today, and was trying to speed it up. I would like your opinion on the implementation and if it's possible to make it faster. Besides that I was wondering if there are ...
4
votes
1answer
50 views

Decorator to add key parameter support to an existing sort function

Preface sorted built-in function has key keyword-only parameter. Most of the custom implementations like the following for ...
4
votes
3answers
202 views

Sorting a list of numbers, each with a character label

I am working on a project that requires me to modify an input list as follows: INPUT: List of numbers, each tagged with a label i.e. [[2, 'a'], [3, 'b'], [1, 'c'], ...] DESIRED OUTPUT: Sort the list ...
2
votes
2answers
55 views

Python merge sort

In Algorithms Fourth Edition by Robert Sedgewick and Kevin Wayne, the exercise 2.2.10 states that: Implement a version of merge() that copies the second half of a[] to aux[] in decreasing order ...
6
votes
2answers
135 views

Sorting algorithms with Python project

I wrote my first project in Python and I would like to know if this is correct. I prepared some guidelines I want to ask. (I know that I don't have any documentation yet). In the future I want to ...
3
votes
1answer
122 views

Hackerrank “Almost Equal” solution

I've spent the best part of a day on this question. It is marked as Expert level. There are about fifteen submission test cases and my solution manages to satisfy the first four. However, from there ...
1
vote
0answers
22 views

Sorting an array of numbers in Java with an algorithm

This is pretty much one of my first programs that I have created in Java and I just wanted to ask if anyone sees some obvious errors or mistakes I made. The purpose of this program is to sort numbers ...
4
votes
1answer
85 views

Bubble sort algorithms and unittest in Python

I am quite new in Python and I am starting my journey with sorting algorithms, PEP8 and the Zen of Python. So far i wrote a post BubbleSort and I drew conclusions and followed the advices. I ...
1
vote
1answer
131 views

My function that combines strverscmp() with strcoll() [closed]

I'm writing a file manager which uses the ncurses-library in C. It aims to be a bit like the programs of the "suckless" community. To make it short, I needed to write my own comparison function for ...
1
vote
1answer
93 views

Bubble sort in Python

I am quite new in Python and I am starting my journey with sorting algorithms, PEP8 and the Zen of Python. I would like to get some feedback about my BubbleSort algorithm which I wrote. Also, is there ...
2
votes
2answers
140 views

My own algorithm to sort integers

I'm studying the C language with a Harvey Deitel book. Trying to solve an exercise that asked to write a good sorting algorithm, I did this: ...
7
votes
1answer
698 views

Sort int array by frequency then value

Problem Take an array of ints as input and run a special sorting algorithm on it. This algorithm must first sort by int frequency, less frequent first, then sort by value. For example ...
1
vote
0answers
81 views

Sorting cards by suit and value using Shell Sort

The question description: put a deck of cards in order by suit (in the order spades, heart, clubs, diamonds) and by rank within each suit, with the restriction that the card must be laid out face down ...
6
votes
2answers
688 views

Writing a class that represents a sorted list of filenames

I have a question: Write a class that represents a sorted list of filenames. The filenames are sorted by most recently added first. Duplicates are not allowed. And I wrote this solution: ...
5
votes
2answers
595 views

sorting a map by converting it to vector

I have a map that I want to print out sorted by value, I convert it to vector and sort the vector. Is this code correct? ...
2
votes
0answers
26 views

Sort ascending/descending functionally

I've been trying to get into functional programming with javascript and have tried to make some experiments with currying and function composition. What I've played with so far is a sorter function ...
-4
votes
1answer
84 views

Bidirectional bubble sort using goto [closed]

I am aware of Dijkstra's paper on why goto is harmful, but I thought it would still be fun to make a bidirectional bubble sort using ...
3
votes
2answers
672 views

Insert a number in a sorted list and return the list with the number at the correct index

I feel like the title is a bit wordy. What I have defined here is a function that takes two parameters: a list that contains valued sorted from smallest to biggest a number to insert at the right ...
3
votes
1answer
56 views

Selection and Insertion sorts from scratch in Java

I am trying to find a good, basic way to make selection and insertion sorts so that I can manipulate them for other sorting techniques. How do these look? Is there a simpler way to write them? ...
2
votes
2answers
104 views

Evaluation of a variation of quick-sort (pivot-selection)

Here is a variation of quick-sort where-in the pivot selection is based on calculating the average of the values of the highest and lowest numbers. ...
13
votes
3answers
395 views

Building unusual IComparer<T> from expressions

I've needed a couple of very special comparers recenty and didn't want to implement each one of them every time so I created a builder and a couple of supporting classes that do that for me. Example ...
1
vote
1answer
69 views

Verify that each named sequence is strictly increasing

The master launch sequence consists of several independent sequences for different systems. Your goal is to verify that all the individual system sequences are in strictly increasing order. In other ...
1
vote
2answers
77 views

Merge Sort code in C

Below is the code of Merge Sort for sorting an integer array in ascending order of values written by me. By looking at the code, I am thinking I can make the code more compact and secure, but not ...
1
vote
1answer
72 views

Sorting an unsorted array

This is a very simple sorting logic that I have written and it's working also. But Is there anything wrong in the traversing of the array? Or the code is not optimized? The code is simply declaring an ...
4
votes
1answer
60 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 ...
0
votes
1answer
41 views

Quicksort implementation with pivotal calculated as middle element

I read quick sort algorhitm and implemented in like this: ...
0
votes
1answer
274 views

Minimum number of swaps required to sort the array in ascending order

Here is my problem statement. An excerpt: You are given an unordered array consisting of consecutive integers ∈ [1, 2, 3, ..., n] without any duplicates. You are ...
5
votes
2answers
64 views

Computing word-frequency-table

I got a word count assignment and the requirements are shown below: INPUT The input contains words in several lines and in each line there could be more than one word. The input only ...
1
vote
2answers
63 views

Speed up sorting algorithm in R: make one column “smaller” than the other

Consider the following sorting algorithm: ...
1
vote
2answers
125 views

Bubble and Cocktail sort

Lately, I've been developing sorting algorithms as a kind of exercise to make better code and I've come accross something curious, even though bubble and cocktail sort are known to perform horribly, I ...
1
vote
1answer
203 views

Quicksort Without Recursion

Am teaching myself about sorting techniques and trying my hand at a quicksort implementation. I found a lot of examples based on recursion but didn't want to go that route in a language that doesn't ...