gustavo-depaula / stalin-sort

Add a stalin sort algorithm in any language you like ❣️ if you like give us a ⭐️
MIT License
1.5k stars 174 forks source link

More precise algorithm definition #11

Open Dixtosa opened 5 years ago

Dixtosa commented 5 years ago

As this project gets larger and larger, lol, we may need a less vague algorithm definition than a tweet so that it is easier to know if a PR is wrong.

gustavo-depaula commented 5 years ago

This is definitively a good idea! Do you have any recommendation on how we can define it?

Nekvin commented 5 years ago

This is how i understood it: Array[1,3,2,7,6,7,34] Array.sort();

The remaining numbers would then be 1,3,7,7,34 or if you don't want the same numbers 1,3,7,34.

luksamuk commented 5 years ago

The remaining numbers would then be 1,3,7,7,34 or if you don't want the same numbers 1,3,7,34.

I believe that no extra information should be lost apart from what the algorithm instructs, so the double 7 seems like something that should remain. One other thing is that there should be some well-tested inputs and outputs for comparison.

rnitta commented 5 years ago

Any element which is out of order is eliminated.

There should be more precise definition of "eliminated".

For instance,

in javascript, https://github.com/gustavo-depaula/stalin-sort/blob/174338ddbbb6115657fd1a47e8151de8f19da4cb/javascript/stalin-sort.js#L5 filtering elements by the functional way can be called "elimination", I think.

in php, https://github.com/gustavo-depaula/stalin-sort/blob/174338ddbbb6115657fd1a47e8151de8f19da4cb/php/function.php#L22 the recursive way also may well be called "elimination".

in haxe, https://github.com/gustavo-depaula/stalin-sort/blob/2972fd309f946cda03d842bba0a55530d2d9709d/haxe/stalin_sort.hx#L8

procedurally adding admitted elements to a new array is not appropriate, imo.

Thoughts?

gabrielcarneiro97 commented 5 years ago

Any element which is out of order is eliminated.

There should be more precise definition of "eliminated".

For instance,

in javascript, https://github.com/gustavo-depaula/stalin-sort/blob/174338ddbbb6115657fd1a47e8151de8f19da4cb/javascript/stalin-sort.js#L5

filtering elements by the functional way can be called "elimination", I think. in php, https://github.com/gustavo-depaula/stalin-sort/blob/174338ddbbb6115657fd1a47e8151de8f19da4cb/php/function.php#L22

the recursive way also may well be called "elimination". in haxe, https://github.com/gustavo-depaula/stalin-sort/blob/2972fd309f946cda03d842bba0a55530d2d9709d/haxe/stalin_sort.hx#L8

procedurally adding admitted elements to a new array is not appropriate, imo.

Thoughts?

I don't think that something so expecific needs to be a concern, for instance filtering in JS returns a new Array, so it's just like adding something to the a new Array, the way that some codes are doing.

gabrielcarneiro97 commented 5 years ago

I divided my comment so we can discuss each proposal separately.

One important thing that I thought that needs to be pattern is the way that the function and the codes are written. For example, the Java and the C code have a main function that uses the funcion, this is one think that I see as bad. Look how many lines were used for this purpose:

https://github.com/gustavo-depaula/stalin-sort/blob/174338ddbbb6115657fd1a47e8151de8f19da4cb/java/stalin.java#L16-L35

I suggest that all codes needs to be a single function that is exported from the file at its end, just like the JS code. Of course that some languages don't have this feature of exporting a single function, so for this purposes, like Java ~I guess~, the code just exports a class from the file. My point is that the files mustn't have the main function.

https://github.com/gustavo-depaula/stalin-sort/blob/174338ddbbb6115657fd1a47e8151de8f19da4cb/javascript/stalin-sort.js#L1-L15

gabrielcarneiro97 commented 5 years ago

I divided my comment so we can discuss each proposal separately.

Other point that I think that needs to be pattern is the number of the parameters and the return of the function. All functions must have one param, returns one value and the received argument mustn't be modified, it makes the code easier to understand in any given language.

Good example:

https://github.com/gustavo-depaula/stalin-sort/blob/174338ddbbb6115657fd1a47e8151de8f19da4cb/python/stalin_sort.py#L1-L9

Bad example:

https://github.com/gustavo-depaula/stalin-sort/blob/174338ddbbb6115657fd1a47e8151de8f19da4cb/C%2B%2B/stalin_sort.cpp#L4-L15

gabrielcarneiro97 commented 5 years ago

I divided my comment so we can discuss each proposal separately.

Other important thing is the validation and error throwing that needs to be done. Some codes simply don't check if the argument is really an array or a pointer, in some cases. I think that all codes needs to check this, just like here:

https://github.com/gustavo-depaula/stalin-sort/blob/174338ddbbb6115657fd1a47e8151de8f19da4cb/javascript/stalin-sort.js#L2

Other good checking is check if the array is empty, it's a good improvement for all codes, just like here;

https://github.com/gustavo-depaula/stalin-sort/blob/174338ddbbb6115657fd1a47e8151de8f19da4cb/ruby/stalin_sort.rb#L2

gabrielcarneiro97 commented 5 years ago

Hey mates, could you take a look at PR #68?

Artoria2e5 commented 1 year ago

We talk about arrays a lot, but a good(?!) property of Stalin Sort is that it can be done in-place on a linked list, still in O(n) time. In-place sort using an array is also possible, but more complex as you'd need to keep track of the position offset from elimination.