Skip to content

timsort #959

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

Closed
xu561865 opened this issue Jul 5, 2019 · 2 comments
Closed

timsort #959

xu561865 opened this issue Jul 5, 2019 · 2 comments

Comments

@xu561865
Copy link

xu561865 commented Jul 5, 2019

When I used [2, 4, 5, 3, 1] to test, it just returned [1, 2, 4, 5]

@FrogBattle
Copy link
Contributor

FrogBattle commented Jul 5, 2019

Hello,

After some play with the code I reached the following conclusion:
The 3 is left out in the following if clause :

        if lst[i] < lst[i - 1]: 
            if not new_run:
                runs.append([lst[i - 1]])
                new_run.append(lst[i])
            else:`
                runs.append(new_run)
                new_run = []

Lines 56-62.
The current index i is not assigned to "new_run" before the same variable is set to empty list, thus the 3 is lost. It is supposed to be added to the list on the next loop iteration, although this is stopped by the first check done here :

        if i == length - 1:
            new_run.append(lst[i])
            runs.append(new_run)
                break

Lines 51-54.

If you put the if clause with the break statement last, the program may add some elements to the new_run list twice. This happens as new_run initializes to [lst[0]] and it may happen that it adds some elements twice to different runs.

In general, if you rely on the next loop iteration to add the previous element into a new_run list, it must do it explicitly ( if new_run==[]), which would mean another conditional. Right now, you will not lose an element if and only if the next iterated element is also smaller than the one before him :

        if lst[i] < lst[i - 1]:
            if not new_run:
                runs.append([lst[i - 1]])
                new_run.append(lst[i])
            else:
                runs.append(new_run)
                new_run = []
        else:
            new_run.append(lst[i])

Lines 56-64.

Hope this helps.
Alex

FrogBattle added a commit to FrogBattle/Python that referenced this issue Jul 8, 2019
The previous algorithm was skipping numbers, according to issue TheAlgorithms#959, and my own tests.
The version I am applying uses a while loop, which works correctly and is easier to compute, as there is no break statement.
FrogBattle added a commit to FrogBattle/Python that referenced this issue Jul 8, 2019
Update tim_sort.py

The previous algorithm was skipping numbers, according to issue TheAlgorithms#959, and my own tests.
The version I am applying uses a while loop, which works correctly and is easier to compute, as there is no break statement.
@cclauss
Copy link
Member

cclauss commented Jul 11, 2019

Can this issue be closed?

cclauss pushed a commit that referenced this issue Jul 30, 2019
* Update tim_sort.py


Update tim_sort.py

The previous algorithm was skipping numbers, according to issue #959, and my own tests.
The version I am applying uses a while loop, which works correctly and is easier to compute, as there is no break statement.

* Update tim_sort.py
@cclauss cclauss closed this as completed Aug 1, 2019
stokhos pushed a commit to stokhos/Python that referenced this issue Jan 3, 2021
* Update tim_sort.py


Update tim_sort.py

The previous algorithm was skipping numbers, according to issue TheAlgorithms#959, and my own tests.
The version I am applying uses a while loop, which works correctly and is easier to compute, as there is no break statement.

* Update tim_sort.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants