Skip to content

Commit d836728

Browse files
committed
Add a function to check sorting of
OneDimensionalArray. Appended algorithms file with is_sorted function. This will be useful to check whether the array given is sorted or not.
1 parent a21d00e commit d836728

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

pydatastructs/linear_data_structures/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
bucket_sort,
3333
cocktail_shaker_sort,
3434
quick_sort,
35-
longest_common_subsequence
35+
longest_common_subsequence,
36+
is_sorted
3637
)
3738
__all__.extend(algorithms.__all__)

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
'bucket_sort',
1515
'cocktail_shaker_sort',
1616
'quick_sort',
17-
'longest_common_subsequence'
17+
'longest_common_subsequence',
18+
'is_sorted'
1819
]
1920

2021
def _merge(array, sl, el, sr, er, end, comp):
@@ -787,3 +788,35 @@ def longest_common_subsequence(seq1: OneDimensionalArray, seq2: OneDimensionalAr
787788
check_mat[i][j] = check_mat[i][j-1]
788789

789790
return OneDimensionalArray(seq1._dtype, check_mat[row][col][-1])
791+
792+
def is_sorted(array, start, end):
793+
"""
794+
Checks whether the given array is sorted or not.
795+
796+
Parameters
797+
=========
798+
array : OneDimensionalArray
799+
The array which is to be checked for sort.
800+
801+
start : int
802+
The starting index of the portion of array to be checked.
803+
804+
end : int
805+
The ending index of the portion of array to be checked.
806+
807+
Examples
808+
========
809+
810+
>>> from pydatastructs import OneDimensionalArray, is_sorted
811+
>>> arr = OneDimensionalArray(int,[4,3,2,1])
812+
>>> is_sorted(arr,0,3)
813+
False
814+
>>> arr1 = OneDimensionalArray(int,[1,2,3])
815+
>>> is_sorted(arr1,0,2)
816+
True
817+
818+
"""
819+
for i in range(start+1,end+1):
820+
if(array[i]<array[i-1]):
821+
return False
822+
return True

pydatastructs/linear_data_structures/tests/test_algorithms.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pydatastructs import (
22
merge_sort_parallel, DynamicOneDimensionalArray,
33
OneDimensionalArray, brick_sort, brick_sort_parallel,
4-
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort, cocktail_shaker_sort, quick_sort, longest_common_subsequence)
4+
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort, cocktail_shaker_sort, quick_sort, longest_common_subsequence, is_sorted)
55

66

77
from pydatastructs.utils.raises_util import raises
@@ -126,3 +126,36 @@ def test_longest_common_sequence():
126126
Z = ODA(int, [])
127127
output = longest_common_subsequence(Y, Z)
128128
assert str(output) == '[]'
129+
130+
def test_is_sorted():
131+
ODA = OneDimensionalArray
132+
133+
expected_result = True
134+
135+
arr = ODA(int,[1,2,5,6])
136+
output = is_sorted(arr,0,len(arr)-1)
137+
assert output == expected_result
138+
139+
expected_result = False
140+
141+
arr1 = ODA(int,[4,3,2,1])
142+
output = is_sorted(arr1,0,len(arr1)-1)
143+
assert output == expected_result
144+
145+
expected_result = True
146+
147+
arr2 = ODA(int,[6,1,2,3,4,5])
148+
output = is_sorted(arr2,1,5)
149+
assert output == expected_result
150+
151+
expected_result = False
152+
153+
arr3 = ODA(int,[0,-1,-2,-3,-4,4])
154+
output = is_sorted(arr3,0,5)
155+
assert output == expected_result
156+
157+
expected_result = True
158+
159+
arr4 = ODA(int,[1,2,3,4,5,6,7,8,9,10])
160+
output = is_sorted(arr4,0,9)
161+
assert output == expected_result

0 commit comments

Comments
 (0)