Syntax behind sorted(key=lambda: …)

key is a function that will be called to transform the collection’s items before they are compared. The parameter passed to key must be something that is callable. The use of lambda creates an anonymous function (which is callable). In the case of sorted the callable only takes one parameters. Python’s lambda is pretty simple. It can only do and return one thing really. … Read more

What is stability in sorting algorithms and why is it important?

A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input array to be sorted. Some sorting algorithms are stable by nature like Insertion sort, Merge Sort, Bubble Sort, etc. And some sorting algorithms are not, like Heap Sort, Quick … Read more

how to implement quick sort algorithm in C++

Your consideration is wrong. The value of r does not change, since it is given as value to the Quicksort function(not a reference). You handle the ranges with p,q such that p is the first index in the range and q the first index not in the range. Thus, your calls were wrong: Here is the complete example. I used std::swap to change elements and ans std::vector … Read more