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

Enhancement in sorting algorithms #216

Merged
merged 2 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 4 additions & 6 deletions pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydatastructs.linear_data_structures.arrays import (
OneDimensionalArray, DynamicArray)
from pydatastructs.utils.misc_util import _check_type
from pydatastructs.utils.misc_util import _check_type, _comp
from concurrent.futures import ThreadPoolExecutor
from math import log, floor

Expand All @@ -11,18 +11,16 @@
def _merge(array, sl, el, sr, er, end, comp):
l, r = [], []
for i in range(sl, el + 1):
if (i <= end and
array[i] is not None):
if i <= end:
l.append(array[i])
array[i] = None
for i in range(sr, er + 1):
if (i <= end and
array[i] is not None):
if i <= end:
r.append(array[i])
array[i] = None
i, j, k = 0, 0, sl
while i < len(l) and j < len(r):
if comp(l[i], r[j]):
if _comp(l[i], r[j], comp):
array[k] = l[i]
i += 1
else:
Expand Down
9 changes: 7 additions & 2 deletions pydatastructs/linear_data_structures/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,16 @@ def _modify(self, force=False):
Contracts the array if Num(T)/Size(T) falls
below load factor.
"""
if (self._num/self._size < self._load_factor) or force:
if force:
i = -1
while self._data[i] is None:
i -= 1
self._last_pos_filled = i%self._size
if (self._num/self._size < self._load_factor):
arr_new = OneDimensionalArray(self._dtype, 2*self._num + 1)
j = 0
for i in range(self._last_pos_filled + 1):
if self[i] is not None:
if self._data[i] is not None:
arr_new[j] = self[i]
j += 1
self._last_pos_filled = j - 1
Expand Down
18 changes: 13 additions & 5 deletions pydatastructs/linear_data_structures/tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ def test_merge_sort_parallel():
arr.append(random.randint(1, 1000))
for _ in range(n//3):
arr.delete(random.randint(0, n//2))
expected_arr = [686, 779, 102, 134, 362,
448, 480, 548, 228, 688,
247, 373, 696, None, None,
None, None, None, None,
None, None, None, None,
expected_arr = [686, 779, 102, 134, 362, 448,
480, 548, None, None, None,
228, 688, 247, 373, 696, None,
None, None, None, None, None,
None, None, None, None, None,
None, None, None, None]
merge_sort_parallel(arr, 5, start=2, end=10)
assert arr._data == expected_arr
merge_sort_parallel(arr, 5)
expected_arr = [102, 134, 228, 247, 362, 373, 448,
480, 548, 686, 688, 696, 779,
None, None, None, None, None, None,
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
14 changes: 14 additions & 0 deletions pydatastructs/utils/misc_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,17 @@ def __new__(cls, key, data=None):
obj.data = data
obj.parent, obj.size = [None]*2
return obj

def _comp(u, v, tcomp):
"""
Overloaded comparator for comparing
two values where any one of them can be
`None`.
"""
if u is None and v is not None:
return False
elif u is not None and v is None:
return True
elif u is None and v is None:
return False
return tcomp(u, v)