# Tag Info

## Hot answers tagged sorting

4

Review: static size_t& logb2(size_t num) { size_t aux = 1; if (num == 0) return (aux = -1); while (0 < (num >>= 1)) aux++; return aux; } This is returning a reference to a local object. aux goes out of scope at the end of the function, so there's no way that the calling function can use a reference to it. It might work ...

3

File in = new File("input.txt"); Do not shorten variable names, it makes the code harder to understand and maintain. for (int i = 0; i < numbers.length; i++) { numbers[i] = input.nextInt(); } I'm a very persistent advocate that you're only allowed to use single-letter variable names if you're dealing with ...

2

isset($variable) && !empty($variable) is a classic/common anti-pattern that should not exist in anyone's code. Please read this: https://stackoverflow.com/a/4559976/2943403 A lookup array will allow you to ditch all those condition blocks. $lookup = [ 'customer' => [ 'tuotesarjat' => ['sarjanimi', 'laattatyyppi'], '... 2 This is a CodeIgniter application, so I expect its submission gathering methods to be used rather than native$_GET and $_POST. When you nominate the expected key in the$this->input->post('run') call, CI will automatically set the value to null if it does not exist. $this->load->model('country_model'); in the constructor is appropriate while ... 2 You have been posting in CodeReview for about 1 year, so while you are still learning C# and a hobbyist, you are not a total newbie. Therefore, you should expect the reviews to be a little more critical. Do understand that "critical" does not mean "mean spirited" but rather more blunt while also being constructive. What disappoints me ... 2 use declarations use std::env; use std::fs::File; use std::io::*; That's a sneaky way to import io::{Error, Result}. For the sake of readability, it may be beneficial to spell out io:: each time. Also see the Error handling section below. use declarations can be condensed. For example: use std::{env, fs::File, io}; Allocation let args: Vec<_> = ... 1 What you have implemented is called Bubble Sort; it's worth mentioning that in the comment of sort() to help readers. int main() Not bad, but we can be a little more specific by showing that the main() takes no arguments: int main(void) We have a problem here: int n; scanf("%d",&n); while(n<=0) If scanf() is unable to convert the input ... 1 General Observations Good job creating functions and the naming of the functions. The naming of the variables could probably be improved, for instance n could be renamed to item_count or array_element_count. The indentation is inconsistent. This could lead issues during maintenance of the code. Missing Function As stated above you did a pretty good job of ... 1 Array is not a Vector JavaScript does not have vectors, it uses Array and Object that can both behave like vectors (grows and shrinks). Arrays can also be sparse [hash map] or dense [flat sequential array]. Objects are sparse. Objects do not have a length property As you have not defined what you mean by vector one must make an assumption in regard to ... 1 Performance The root cause is a poor referential locality. As sorting progresses, and the nodes get relinked, they become accessed in no particular order, all over the memory. This results in too many cache misses, and a cache miss is very expensive. binarySort is a bit too complicated. It is very hard to follow. Things like flag, and flag2, (what do they ... 1 Note: I wrote this answer in response to your other post, which was deleted just before I posted. However, it nearly all applies to this code too, so here it is: size_t& logb2(size_t num) static size_t& get_max(obj_arr*& arr, const size_t& size) static size_t& get_max(obj_arr**& arr, const size_t& size) bug: These all return ... 1 Since your ordering array contains all of the ids in your data array, you can prepare the two arrays to have relatable keys then just call array_replace(). This means a fully "functional style" instead of classic foreach loops (if you like them). The foreach() loops often outperform function calls due to having less overhead, but with my snippet ... 1 The default behavior of array_filter() is greedy about destroying falsey values.$new_query = array_filter($new_query, function($element) { return !empty($element);}); is identical in functionality to:$new_query = array_filter(\$new_query); I recommend consistently using curly braces on all constructs and logical tabbing to make your code more readable. ...

1

You don't need to call any sort() functions because your input and response arrays have a 1-to-1 relationship. Flip your input array so that the values become keys. Use array_column()'s third parameter to assign temporary keys to the response array. Now that the two arrays have relatable keys, you can just overwrite the flipped input array with the newly-...

1

So far I don't see anything indicating that you need something other than ASC sorting. The spaceship operator auto-magically compares numeric values as numeric values and non-numeric strings as strings, so it seems to me that there is currently no benefit to creating conditional sorting calls. Just pass the target column into usort() using use() (if under ...

1

I took a moment to compile a list of real movie titles which represent some squirrelly fringe cases. I arrived at a one-liner that relies on regex for best accuracy that doesn't fail when the leading The word is not immediately followed by a space. Notice that one real movie title below starts with the word "the", but the fourth character is not a ...

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