Skip to content

Commit e3ce68f

Browse files
committed
added c++ backend for cocktail sort
1 parent f7a6296 commit e3ce68f

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

pydatastructs/linear_data_structures/_backend/cpp/algorithms/algorithms.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "misc_algorithms.hpp"
55

66
static PyMethodDef algorithms_PyMethodDef[] = {
7+
{"cocktail_shaker_sort", (PyCFunction) cocktail_shaker_sort,
8+
METH_VARARGS | METH_KEYWORDS, ""},
79
{"quick_sort", (PyCFunction) quick_sort,
810
METH_VARARGS | METH_KEYWORDS, ""},
911
{"bubble_sort", (PyCFunction) bubble_sort,

pydatastructs/linear_data_structures/_backend/cpp/algorithms/quadratic_time_sort.hpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,78 @@ static PyObject* insertion_sort(PyObject* self, PyObject* args, PyObject* kwds)
185185
}
186186

187187

188+
static PyObject* cocktail_shaker_sort_impl(PyObject* array, size_t lower, size_t upper, PyObject* comp) {
189+
bool is_sorted = false;
190+
191+
while(!is_sorted){
192+
is_sorted = true;
193+
194+
for(size_t i = lower; i < upper; i++){
195+
PyObject* i_PyObject = PyLong_FromSize_t(i);
196+
PyObject* i1_PyObject = PyLong_FromSize_t(i+1);
197+
if(_comp(PyObject_GetItem(array, i_PyObject), PyObject_GetItem(array, i1_PyObject), comp) != 1){
198+
PyObject* tmp = PyObject_GetItem(array, i1_PyObject);
199+
PyObject_SetItem(array, i1_PyObject, PyObject_GetItem(array, i_PyObject));
200+
PyObject_SetItem(array, i_PyObject, tmp);
201+
is_sorted = false;
202+
}
203+
}
204+
205+
for (size_t i = upper - 1; i > lower; i--) {
206+
PyObject* i_PyObject = PyLong_FromSize_t(i);
207+
PyObject* i1_PyObject = PyLong_FromSize_t(i - 1);
208+
if (_comp(PyObject_GetItem(array, i1_PyObject), PyObject_GetItem(array, i_PyObject), comp) != 1) {
209+
PyObject* tmp = PyObject_GetItem(array, i1_PyObject);
210+
PyObject_SetItem(array, i1_PyObject, PyObject_GetItem(array, i_PyObject));
211+
PyObject_SetItem(array, i_PyObject, tmp);
212+
is_sorted = false;
213+
}
214+
}
215+
}
216+
return array;
217+
}
218+
219+
static PyObject* cocktail_shaker_sort(PyObject* self, PyObject* args, PyObject* kwds) {
220+
PyObject *args0 = NULL, *start = NULL, *end = NULL;
221+
PyObject *comp = NULL;
222+
size_t lower, upper;
223+
224+
args0 = PyObject_GetItem(args, PyZero);
225+
int is_DynamicOneDimensionalArray = _check_type(args0, &DynamicOneDimensionalArrayType);
226+
int is_OneDimensionalArray = _check_type(args0, &OneDimensionalArrayType);
227+
if (!is_DynamicOneDimensionalArray && !is_OneDimensionalArray) {
228+
raise_exception_if_not_array(args0);
229+
return NULL;
230+
}
231+
232+
comp = PyObject_GetItem(kwds, PyUnicode_FromString("comp"));
233+
if (comp == NULL) {
234+
PyErr_Clear();
235+
}
236+
237+
start = PyObject_GetItem(kwds, PyUnicode_FromString("start"));
238+
if (start == NULL) {
239+
PyErr_Clear();
240+
lower = 0;
241+
} else {
242+
lower = PyLong_AsSize_t(start);
243+
}
244+
245+
end = PyObject_GetItem(kwds, PyUnicode_FromString("end"));
246+
if (end == NULL) {
247+
PyErr_Clear();
248+
upper = PyObject_Length(args0) - 1;
249+
} else {
250+
upper = PyLong_AsSize_t(end);
251+
}
252+
253+
args0 = cocktail_shaker_sort_impl(args0, lower, upper, comp);
254+
if (is_DynamicOneDimensionalArray) {
255+
PyObject_CallMethod(args0, "_modify", "O", Py_True);
256+
}
257+
258+
Py_INCREF(args0);
259+
return args0;
260+
}
261+
188262
#endif

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,10 @@ def cocktail_shaker_sort(array: Array, **kwargs) -> Array:
658658
659659
.. [1] https://en.wikipedia.org/wiki/Cocktail_shaker_sort
660660
"""
661+
backend = kwargs.pop("backend", Backend.PYTHON)
662+
if backend == Backend.CPP:
663+
return _algorithms.cocktail_shaker_sort(array, **kwargs)
664+
661665
raise_if_backend_is_not_python(
662666
cocktail_shaker_sort, kwargs.get('backend', Backend.PYTHON))
663667
def swap(i, j):

0 commit comments

Comments
 (0)