Tag Info

8

Your code is quite complicated. The C standard library provides all the ingredients you need to sort an array. You just need to define a comparison function. All the rest is done by the qsort function from stdlib.h. The comparison function should look like: static int even_first(const void *a, const void *b) { int left = *(const int *)a; int right =...

7

Firstly, we should separate the sorting from the reading of inputs and writing of outputs. We can create a function sort_evens_first() - that's the foundation of writing re-usable code. An advantage (even in this small program) is that a separable function can more easily be tested - no need for an external script to run many instances of the program with ...

5

Zero based indexing Counting: when the index, or the position, is dependent on the item. Count from one. (Zero-based index is mathematically wrong.) I will disagree. The index does not represent a count but rather it is a position. When we measure something we start at 0. For example a length of 5 units. Unit 0 the 1st unit is at 0 to < 1, and the ...

5

You have access to the stack array directly so you can copy the unsorted stack it to a new stack and sort it in place using Array.sort "use strict"; function sortStack(stack) { const sorted = new Stack(); while (!stack.isEmpty()) { sorted.push(stack.pop()) } sorted.storage.sort((a, b) => a - b); return sorted; } or "use strict"; ...

4

You seem to have some extra code in your function that is not needed. The code below does the same thing as your function: const sortStack = (stack) => { sorted = new Stack(); while (!stack.isEmpty()) { tmp = stack.pop(); while (tmp < sorted.peek()) { stack.push(sorted.pop()); } sorted.push(tmp); } return sorted; } The ...

3

I don't see a way to improve the algorithm, but here are some general coding tips: A number can either be greater than 1, or less than 2. So you can use 'else' here instead. if(list.length < 2){ return list }else if(list.length > 1){ Sometimes people create extra variables to improve readability, but here 'n' is less readable than list.length. ...

3

It can be slightly more performant: Keep track of the number of elements you add back to the original stack Empty check Add first item if not empty (size of one check) class Stack { constructor() { this.storage = []; this.counts = { push: 0, pop: 0, peek: 0, isEmpty: 0 } } ...

3

Style Use proper styling. You should use more whitespaces, this will make your code more readable. Instead of num_sort=new use num_sort = new, insead of i!=min_index use i != min_index, etc Follow the Java naming conventions: variable and function names should use camelCase, not snake_case. You should include documentation that explains the user how the ...

2

Using a symbolic constant (INVALID_PARAMETER) along with a literal 0 as a return value is not the cleanest solution. As a side note, a return value signifying an error is traditionally negative. The best kept secret of insertion sort is the fact that the j > 0 && arr[j - 1] > key termination condition of an inner loop is suboptimal. It tests ...

2

The most valuable property of merge sort is stability: the elements compared equal retain their relative order. The condition if(listA[i] < listB[j]){ destabilizes. If the elements happen to be equal, one from listB will be merged first. A simple fix is to rewrite the condition as if(listB[i] < listA[j]){ The Infinity trick assumes that ...

2

Another solution is to use the sort function of javascript. I recommend this solution for simplicity and readability var array1 = [1, 30, 4, 21, 100000]; array1.sort(() => { return b - a }); console.log(array1) // [100000, 30, 21, 4, 1] See the docs for more informations : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/...

2

I think the problem could be, that your algorithm isn't exactly a selection sort but rater a bubble sort. They are very similar, but where bubble sort swaps every two elements when the left is smaller than the right (descending order), selection sort defers swapping to after the inner loop is finished and then only swaps the last found candidate with the ...

2

You are not sorting in place - that is: the input array is untouched by the operation and you return a new sorted array. I would expect the input array to be sorted when the function returns. Javascript's Array.sort() behaves this way. listA.push(Infinity) listB.push(Infinity) I you encounter a problem where you are tempted to do this, you should ...

1

Readability Review It is fine to use n to represent a count or size. It's a variable name common in mathematics, and in expansion also when programming algorithms. However, OP mixes list.length and n representing the same thing, this is bad practice as it's confusing. Furthermore: I would use const over let if a variable is immutable. Include sufficient ...

1

BubbleSort is not an efficient algorithm. Talking time-complexity it runs in $O(n^2)$ which is okay given a very small array, but for a larger amount of numbers its almost unusable. This is a comparison between a few sorting algorithms that i wrote in C#. As you can see the only advantage of BubbleSort over the other algorithms is that it is very easy to ...

1

There's another answer here with a great assessment of your solution. The upshot of that answer (and this one) is that you'd be better off doing a test.sort() and calling it a day. Even though you should probably consider scrapping this solution, as explained in the other answer, a few things here may be worth commenting on anyway. Overall, the style of ...

1

First off, what @Blindman67 said. However, if you were to use this approach in production code, there are a few things to consider: You probably want to create a class/function with a functions like add I see a few issues with your add code: if (test_original[test[x]] === undefined){ test_original[test[x]] = []; test_original[test[x]][0] = test[x]; } ...

1

Your implementation consistently uses trailing return type specification even when not needed, hopefully to look more sexy. Personally, I don't like it, but sexiness is subjective. The log2 function can be made constexpr (thus noexcept), and Integer should be constrained. Also, assumes n > 0 may deserve an assertion. Like this: template <typename ...

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