Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More precise algorithm definition #11

Open
Dixtosa opened this issue Nov 3, 2018 · 10 comments
Open

More precise algorithm definition #11

Dixtosa opened this issue Nov 3, 2018 · 10 comments
Labels
🆘 help wanted Extra attention is needed ⭐ enhancement New feature or request

Comments

@Dixtosa
Copy link
Contributor

Dixtosa commented Nov 3, 2018

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
Copy link
Owner

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

@Nekvin
Copy link

Nekvin commented Dec 19, 2018

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
Copy link
Contributor

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
Copy link

rnitta commented Aug 1, 2019

Any element which is out of order is eliminated.

There should be more precise definition of "eliminated".

For instance,

in javascript,

return array.filter((el, i) => {

filtering elements by the functional way can be called "elimination", I think.

in php,

return sort([$x, ...$zs]);

the recursive way also may well be called "elimination".

in haxe,

sortedArray.push(maxValue = array[index]);

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

Thoughts?

@gabrielcarneiro97
Copy link
Collaborator

Any element which is out of order is eliminated.

There should be more precise definition of "eliminated".

For instance,

in javascript,

return array.filter((el, i) => {

filtering elements by the functional way can be called "elimination", I think.
in php,

return sort([$x, ...$zs]);

the recursive way also may well be called "elimination".
in haxe,

sortedArray.push(maxValue = array[index]);

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
Copy link
Collaborator

gabrielcarneiro97 commented Aug 1, 2019

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:

public static void main(String[] args) {
int arr1[] = new int[]{1, 2, 4, 3, 6, 8, 0, 9, 5, 7};
int sorted1[] = stalinSort(arr1);
int arr2[] = new int[]{9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int sorted2[] = stalinSort(arr2);
int arr3[] = new int[]{1, 3, 2, 5, 4, 7, 0, 9, 8, 6};
int sorted3[] = stalinSort(arr3);
System.out.print("Original: ");
System.out.println(Arrays.toString(arr1));
System.out.print("Sorted: ");
System.out.println(Arrays.toString(sorted1));
System.out.print("Original: ");
System.out.println(Arrays.toString(arr2));
System.out.print("Sorted: ");
System.out.println(Arrays.toString(sorted2));
System.out.print("Original: ");
System.out.println(Arrays.toString(arr3));
System.out.print("Sorted: ");
System.out.println(Arrays.toString(sorted3));
}

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.

function stalinSort(array) {
if (!Array.isArray(array)) throw new TypeError('Argument must be an Array!');
let [holder] = array;
return array.filter((el, i) => {
if (i === 0) return true;
if(el >= holder) {
holder = el;
return true;
}
return false;
});
}
module.exports = stalinSort

@gabrielcarneiro97
Copy link
Collaborator

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:

def sort(l):
max_val = l[0]
def add_val(num):
nonlocal max_val
max_val = num
return num
return [add_val(x) for x in l if x >= max_val]

Bad example:

void stalinSort(std::vector<int> &arr, std::vector<int> &sorted)
{
sorted.push_back(arr[0]);
for (int i = 1, j = 0; i < arr.size(); i++)
{
if (arr[i] > arr[j])
{
sorted.push_back(arr[i]);
j++;
}
}
}

@gabrielcarneiro97
Copy link
Collaborator

gabrielcarneiro97 commented Aug 1, 2019

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:

if (!Array.isArray(array)) throw new TypeError('Argument must be an Array!');

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

return [] if arr.empty?

@gabrielcarneiro97 gabrielcarneiro97 added 🆘 help wanted Extra attention is needed ⭐ enhancement New feature or request labels Aug 1, 2019
@gabrielcarneiro97 gabrielcarneiro97 pinned this issue Aug 1, 2019
@gabrielcarneiro97
Copy link
Collaborator

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

@Artoria2e5
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆘 help wanted Extra attention is needed ⭐ enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants