MCPcopy Create free account
hub / github.com/Distributive-Network/PythonMonkey / quickSort

Function quickSort

src/PyListProxyHandler.cc:1656–1683  ·  view source on GitHub ↗

Adapted from Kernigan&Ritchie's C book

Source from the content-addressed store, hash-verified

1654
1655// Adapted from Kernigan&Ritchie's C book
1656static void quickSort(PyObject *list, int left, int right, JSContext *cx, JS::HandleFunction callBack) {
1657
1658 if (left >= right) {
1659 // base case
1660 return;
1661 }
1662
1663 swapItems(list, left, (left + right) / 2);
1664
1665 JS::RootedValue leftValue(cx, jsTypeFactory(cx, PyList_GetItem(list, left)));
1666
1667 int last = left;
1668 for (int index = left + 1; index <= right; index++) {
1669 int result = invokeCallBack(list, index, leftValue, cx, callBack);
1670 if (PyErr_Occurred()) {
1671 return;
1672 }
1673 if (result < 0) {
1674 swapItems(list, ++last, index);
1675 }
1676 }
1677
1678 swapItems(list, left, last);
1679
1680 quickSort(list, left, last - 1, cx, callBack);
1681
1682 quickSort(list, last + 1, right, cx, callBack);
1683}
1684
1685// private
1686static bool js_sort_compare_default(JSContext *cx, unsigned argc, JS::Value *vp) {

Callers 1

array_sortFunction · 0.85

Calls 3

swapItemsFunction · 0.85
jsTypeFactoryFunction · 0.85
invokeCallBackFunction · 0.85

Tested by

no test coverage detected