Hot answers tagged

9 votes
Accepted

A New Sorting Algorithm

Line length: you want to avoid long lines of code when you can. With some lines this is unavoidable, but you shouldn't have a long line just because you wanted to print a spacer to the console. You ...
  • 739
8 votes
Accepted

Sort a Python list of strings where each item is made with letters and numbers

Because we need to sort the data, the best we can do in terms of performance is to try and settle for \$O(N \log N\$). What you have here is called natural sort. Apart from some libraries that you ...
5 votes
Accepted

Sort array of associative arrays by the key of the subarrays

Short of simplifying your input array structure, using functional sorting will be most direct with array_multisort() which is fed an array of keys to sort in a ...
  • 8,050
4 votes

Find first and last matches on a sorted list

Binary search This should be a binary search as that is the expected algorithm. Why? because the array is sorted. Your function does not take advantage of the fact that the elements are sorted. ...
  • 20.7k
4 votes

Introsort implementation

I strongly recommend to try std::partition and std::make_heap/std::sort_heap to see what is ...
  • 54.4k
4 votes
Accepted

Multithreaded bogosort in Rust

List of improvements I would make: I'm uncertain why you use Arc::clone(...), just use .clone() directly. Same for ...
  • 446
4 votes
Accepted

Simple selection_sort in C

Test for Possible Memory Allocation Errors In modern high-level languages such as C++, memory allocation errors throw an exception that the programmer can catch. This is not the case in the C ...
  • 19.9k
3 votes

Sort and search vectors by different criteria

Unnecessary sorting of data You should be able to find the desired items in \$\mathcal{O}(N)\$ time without needing to sort the container multiple times. You can just linearly go through the items in ...
  • 38.3k
3 votes

Sum and bubble-sort student scores

Advice 1 typedef struct student { int id; int prob_1; int prob_2; int prob_3; int total_score; char grade; }Student; Since ...
  • 26.4k
3 votes

Sorting multiple vectors based on a reference vector

If you ahead of time know the number of arrays/vectors then with ranges-v3 you can do exactly what the OP was trying to achieve. sort vectors based on a reference vector With ranges-v3 you can zip ...
3 votes

CodeForce 230 Dragon Fighting, Sorting without using array

Your code is too difficult to read. Please do some good indentation and formatting. You can't solve this without storing whole information somewhere, because you can fight dragon in any order, so in ...
  • 1,643
3 votes

Sum and bubble-sort student scores

But I wonder if there is a more efficient way. Instead of bubble-sort, use qsort(). Yet I expect OP's assignment may oblige bubble-sort. ...
3 votes

Sort array of associative arrays by the key of the subarrays

Bubble sort algorithm - inefficiently implemented After each iteration of the Bubble sort algorithm "one less element (the last one) is needed to be compared until there are no more elements left ...
3 votes

Fast portable, parallel MSD radix sort for unsigned keys in C89

Consider using C11 thread support You are making your code compatible with C89, however that is a standard that is more than 30 years old, and we have advanced since then. C11 introduced support for ...
  • 38.3k
2 votes

Counting Radix Sort: A C++ version

The assumption that CHAR_BIT is 8 can be avoided. I'm not convinced it's necessary to use the exact-width type std::uint64_t. ...
  • 63.3k
2 votes

Quicksort Invariant and steps to determine correcteness

The invariant you stated is correct. These invariants hold for the partition loop, so they should hold before partition and after partition. Before partition, we have to consider the pivot to be the ...
2 votes
Accepted

Custom sorting algorithm in Salesforce Apex

For a less verbose way to achieve this in Apex you need to create a wrapper class for EmailTemplate that implements Comparable interface. The advantage is that elements don't need to be manually moved ...
2 votes

CodeForce 230 Dragon Fighting, Sorting without using array

You can separate input and processing. And worst case, looking at the input values before the last one is read doesn't help you any: The last dragon may be the only one Kirito initially dominates. ...
  • 5,108
2 votes

Sort a Python list of strings where each item is made with letters and numbers

match.groups()[0] returns all the groups of the match, and then indexes into the returned tuple to extract the first one. ...
  • 32.5k
2 votes

HackerRank CountingSort 1 in C#

Only focusing on countingSort1() using 2 loops doesn't buy you anything, it just blows up your code. But this is dangerous as well if you plan to reuse this method ...
  • 50.1k
2 votes
Accepted

Partitioning a list so that even numbers appear before odd numbers

welcome to the Rust community! Your code is already quite idiomatic. You may use an unsafe and unstable feature to improve efficiency, swap_unchecked. It's only ...
2 votes
Accepted

Introsort implementation

One of the major reasons the native C++ std::sort() is faster than qsort() inherited from C is that the latter calls the ...
  • 18.2k
2 votes

Multithreaded bogosort in Rust

You definitely want access to shared stuff to be read-only in the normal case so multiple threads can do it in parallel. Reading a lock-free atomic simply works that way, unlike taking a lock around ...
  • 2,787
2 votes

Sort array of associative arrays by the key of the subarrays

I solved it with usort like recommended by @greybeard. Credits to him! ...
  • 237
2 votes
Accepted

Sorting visualizer using Java Swing

In many cases your use of import * is a little too aggressive. You're filling the working namespace of your files with everything from ...
  • 54.4k
2 votes
Accepted

Fast portable, parallel MSD radix sort for unsigned keys in C89

The function parallel_radix_sort_impl() is over 400 lines of code. There must be some way to break this up into multiple functions. This is way too complex, and too ...
  • 19.9k
1 vote
Accepted

A Sorting Algorithm

The Swap It's the good old "arithmetic swap", a variant of the XOR-swap that uses addition and subtraction instead of XOR. It avoids naming a temporary variable, which was not a significant ...
  • 7,059
1 vote

bubble sort algorithm with nested loops performs better than with one loop JS

There are four instances of startVal = array[i + c] comparingVal = array[i + 1 + c] in 1 (for c 0 or 1). Don't Repeat Yourself: ...
1 vote

Find first and last matches on a sorted list

A short review; I prefer list over arr, because I prefer fully spelled out words above abbreviations, but I like obvious one ...
  • 32.8k
1 vote

Find first and last matches on a sorted list

You answer seems fine. Don't worry about how other people have solved the same problem. The break/continue logic is not bad GIVEN the question, but you should definitely comment in your code what the ...

Only top scored, non community-wiki answers of a minimum length are eligible