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

Adding Quicksort #314

Merged
merged 61 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
5b34a79
Update queue.py
Arvind-raj06 Jan 7, 2021
52f01c3
Update linked_lists.py
Arvind-raj06 Jan 8, 2021
6610a7f
Update linked_lists.py
Arvind-raj06 Jan 8, 2021
4c5c855
Update linked_lists.py
Arvind-raj06 Jan 8, 2021
f93008a
Update queue.py
Arvind-raj06 Jan 8, 2021
6867add
Update linked_lists.py
Arvind-raj06 Jan 8, 2021
628045c
Update linked_lists.py
Arvind-raj06 Jan 8, 2021
09e5e42
Completed updating the insert_after
Arvind-raj06 Jan 8, 2021
74009e6
Update linked_lists.py
Arvind-raj06 Jan 8, 2021
0055baa
Update queue.py
Arvind-raj06 Jan 8, 2021
9dec38c
Update linked_lists.py
Arvind-raj06 Jan 10, 2021
708f6bc
Cocktail
Arvind-raj06 Jan 10, 2021
27a5f1a
Update algorithms.py
Arvind-raj06 Jan 10, 2021
6ae5a0c
Implementing the cocktail sort
Arvind-raj06 Jan 10, 2021
aaf529a
Update __init__.py
Arvind-raj06 Jan 10, 2021
9f937d4
Correcting error
Arvind-raj06 Jan 10, 2021
38ebcbe
Completion
Arvind-raj06 Jan 10, 2021
9a77768
Converting to ODA
Arvind-raj06 Jan 10, 2021
40350b4
Update algorithms.py
Arvind-raj06 Jan 10, 2021
a049c11
Update algorithms.py
Arvind-raj06 Jan 10, 2021
a990d46
Really!
Arvind-raj06 Jan 10, 2021
e49d268
Including cocktail sort
Arvind-raj06 Jan 11, 2021
c6c5fdd
Correcting for doda
Arvind-raj06 Jan 11, 2021
db70e68
Error Correction
Arvind-raj06 Jan 11, 2021
9acbebf
Yep done!
Arvind-raj06 Jan 11, 2021
57d7fbf
Hope this works fine
Arvind-raj06 Jan 11, 2021
3569652
Update algorithms.py
Arvind-raj06 Jan 11, 2021
c9d4f9c
Commit
Arvind-raj06 Jan 11, 2021
e1d817f
Cocktail update
Arvind-raj06 Jan 11, 2021
864e95c
Update algorithms.py
Arvind-raj06 Jan 11, 2021
570a287
Update test_algorithms.py
Arvind-raj06 Jan 12, 2021
cf91f8a
Let's check
Arvind-raj06 Jan 12, 2021
3c34257
Update test_algorithms.py
Arvind-raj06 Jan 12, 2021
1ae0e3d
Update test_algorithms.py
Arvind-raj06 Jan 12, 2021
89b903a
Fixed cocktail sort
czgdp1807 Jan 13, 2021
7a4581c
cocktail_sort -> cocktail_shaker_sort
czgdp1807 Jan 13, 2021
0c741ca
Starting with Quicksort
Arvind-raj06 Jan 13, 2021
4835dd4
Adding quick sort
Arvind-raj06 Jan 13, 2021
069613a
Making changes
Arvind-raj06 Jan 13, 2021
3f97409
Update algorithms.py
Arvind-raj06 Jan 13, 2021
2177e04
Update __init__.py
Arvind-raj06 Jan 13, 2021
7e23b41
Hope this works
Arvind-raj06 Jan 13, 2021
abe4362
Update algorithms.py
Arvind-raj06 Jan 13, 2021
0ad0b56
Update algorithms.py
Arvind-raj06 Jan 13, 2021
4e1e8d4
Ok
Arvind-raj06 Jan 13, 2021
f8afd26
Update algorithms.py
Arvind-raj06 Jan 13, 2021
377bfd6
added quick sort
Arvind-raj06 Jan 13, 2021
a1fd65a
Merge branch 'master' into Let'scode
Arvind-raj06 Jan 13, 2021
7c4d918
Update algorithms.py
Arvind-raj06 Jan 13, 2021
c042722
Removing whitespace
Arvind-raj06 Jan 13, 2021
ed7a059
Error correction
Arvind-raj06 Jan 13, 2021
9cda682
Start to implement stack
Arvind-raj06 Jan 14, 2021
65d0f88
Hope this work
Arvind-raj06 Jan 14, 2021
f22a7ac
Update algorithms.py
Arvind-raj06 Jan 14, 2021
5da091e
Yep
Arvind-raj06 Jan 14, 2021
2c7afac
Forgot
Arvind-raj06 Jan 14, 2021
e8d13b0
Shifting None to end
czgdp1807 Jan 15, 2021
f7c6463
Restored test
czgdp1807 Jan 15, 2021
b476021
Apply suggestions from code review
czgdp1807 Jan 15, 2021
923b1be
Yes Done
Arvind-raj06 Jan 15, 2021
bcb678e
Pivot picking logic corrected
czgdp1807 Jan 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
matrix_multiply_parallel,
counting_sort,
bucket_sort,
cocktail_shaker_sort
cocktail_shaker_sort,
quick_sort
)
__all__.extend(algorithms.__all__)
86 changes: 85 additions & 1 deletion pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
'matrix_multiply_parallel',
'counting_sort',
'bucket_sort',
'cocktail_shaker_sort'
'cocktail_shaker_sort',
'quick_sort'
]

def _merge(array, sl, el, sr, er, end, comp):
Expand Down Expand Up @@ -620,6 +621,89 @@ def swap(i, j):
swapping = False
lower = lower + 1

return array

def quick_sort(array: Array, **kwargs) -> Array:
"""
Performs quick sort on the given array.

Parameters
==========

array: Array
The array which is to be sorted.
start: int
The starting index of the portion
which is to be sorted.
Optional, by default 0
end: int
The ending index of the portion which
is to be sorted.
Optional, by default the index
of the last position filled.
comp: lambda/function
The comparator which is to be used
for sorting. If the function returns
False then only swapping is performed.
Optional, by default, less than or
equal to is used for comparing two
values.

Returns
=======

output: Array
The sorted array.

Examples
========

>>> from pydatastructs import OneDimensionalArray as ODA, quick_sort
>>> arr = ODA(int, [5, 78, 1, 0])
>>> out = quick_sort(arr)
>>> str(out)
'[0, 1, 5, 78]'
>>> arr = ODA(int, [21, 37, 5])
>>> out = quick_sort(arr)
>>> str(out)
'[5, 21, 37]'

References
==========

.. [1] https://en.wikipedia.org/wiki/Quicksort
"""
from pydatastructs import Stack
comp = kwargs.get("comp", lambda u, v: u <= v)

def partition(low, high):
i = (low - 1)
x = array[high]
for j in range(low , high):
if _comp(array[j], x, comp) is True:
i = i + 1
array[i], array[j] = array[j], array[i]
array[i + 1], array[high] = array[high], array[i + 1]
return (i + 1)

lower = kwargs.get('start', 0)
upper = kwargs.get('end', len(array) - 1)
stack, top = Stack(), -1

stack.push(lower)
stack.push(upper)

while stack.is_empty==False:
czgdp1807 marked this conversation as resolved.
Show resolved Hide resolved
high = stack.pop()
low = stack.pop()
p = partition(low, high)
if p - 1 > low:
stack.push(low)
stack.push(p - 1)
if p + 1 < high:
stack.push(p + 1)
stack.push(high)

if _check_type(array, DynamicArray):
array._modify(force=True)

Expand Down
7 changes: 5 additions & 2 deletions pydatastructs/linear_data_structures/tests/test_algorithms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from pydatastructs import (
merge_sort_parallel, DynamicOneDimensionalArray,
OneDimensionalArray, brick_sort, brick_sort_parallel,
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort, cocktail_shaker_sort)
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort, cocktail_shaker_sort, quick_sort)

from pydatastructs.utils.raises_util import raises
import random

Expand Down Expand Up @@ -29,7 +30,6 @@ def _test_common_sort(sort, *args, **kwargs):
None, None, None, None, None,
None, None, None, None, None, None, None]
assert arr._data == expected_arr
assert (arr._last_pos_filled, arr._num, arr._size) == (12, 13, 31)

n = random.randint(10, 20)
arr = OneDimensionalArray(int, n)
Expand Down Expand Up @@ -73,6 +73,9 @@ def test_counting_sort():
def test_cocktail_shaker_sort():
_test_common_sort(cocktail_shaker_sort)

def test_quick_sort():
_test_common_sort(quick_sort)

def test_matrix_multiply_parallel():
ODA = OneDimensionalArray

Expand Down