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

[Quicksort]Added shuffling of input #16

Merged
merged 1 commit into from
Aug 13, 2016

Conversation

miczal
Copy link
Contributor

@miczal miczal commented Aug 11, 2016

This is safety measure against the worst case scenario - sorted input and O(n^2) complexity

@AnupKumarPanwar
Copy link
Member

But Shuffle will add complexity in other cases

@miczal
Copy link
Contributor Author

miczal commented Aug 12, 2016

I'm by no means a mathematician, but shuffling of an input gives you a possibility to have average case complexity as often as theoretically possible in every case. It doesn't add to the overall average complexity - O(nlog n + n) is still O(nlog n). I've wrote this short script to let you know how important randomization of input is and how often people don't do it:

how_many = 100000
sorting_func = sort #this shuffles input
#sorting_func = quick_sort
tries = 10

accumulated_time = {'Sorted': 0,
                    'Reverse': 0,
                    'Random': 0,
                    'A lot of equal keys': 0,
                    'Same keys': 0}
for i in range(tries):
    data_sets = {'Sorted': [x for x in range(how_many)],
                 'Reverse': list(reversed([x for x in range(how_many)])),
                 'Random': [random.randint(0, how_many) for x in range(how_many)],
                 'A lot of equal keys': [x if x % 2 else 0 for x in range(how_many)],
                 'Same keys': [1] * how_many}
    for what, set in data_sets.items():
        start = time.clock()
        sorting_func(set)
        end = time.clock()
        accumulated_time[what] += (end - start)

print("Average time:")
for what, total_time in accumulated_time.items():
    print(what + ": " + str(total_time/tries))

Average time on my PC with shuffling of input:

Average time:
A lot of equal keys: 0.30944705735259076
Reverse: 0.5213236423849711
Random: 0.5092556249405173
Sorted: 0.5258773700452282
Same keys: 0.09532146027318696

In case of current implementation of quick_sort only in case of Same keys and Random data sets I didn't have a RecursionError

Average time:
Same keys: 0.016281707645539805
Random: 0.40859697925563115

As you can see both are faster by about 0.09 second - this is the effect of added shuffling.

Only after changing data sets length to 950 from 100000 (!!!) I didn't get an error and I could sort every input.

See for yourself - comment lines in accumulated_time and data_sets to disable some inputs. I didn't have time to refactor it, so it isn't perfect, but it's close enough to the actual testing environment.

tries is to average the time from many sortings and to minimize error from environmental variables or whatever.

@dynamitechetan dynamitechetan merged commit e3c24fe into TheAlgorithms:master Aug 13, 2016
Blues1998 added a commit to Blues1998/Python that referenced this pull request Jul 1, 2019
poyea pushed a commit that referenced this pull request Jul 8, 2019
* Create text file for numbers

* Create sol2.py

* Pythonic version of Problem #16 solution

* Update sol2.py

* Valid Python code for Python version 2-3

* Update sol2.py
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
…#935)

* Create text file for numbers

* Create sol2.py

* Pythonic version of Problem TheAlgorithms#16 solution

* Update sol2.py

* Valid Python code for Python version 2-3

* Update sol2.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

Successfully merging this pull request may close these issues.

None yet

3 participants