quicksort

Quicksort - conditions that makes it stable

半腔热情 提交于 2019-12-04 22:35:57
问题 A sorting algorithm is stable if it preserves the relative order of any two elements with equals keys. Under which conditions is quicksort stable? Quicksort is stable when no item is passed unless it has a smaller key. What other conditions make it stable? 回答1: Well, it is quite easy to make a stable quicksort that uses O(N) space rather than the O(log N) that an in-place, unstable implementation uses. Of course, a quicksort that uses O(N) space doesn't have to be stable, but it can be made

Quick sort time complexity [closed]

孤者浪人 提交于 2019-12-04 19:45:13
I recently read about time complexity and I found out that Quick sort has an average time complexity of O(nlog(n)) . Question 1: What I do not understand is how is the log(n) present in the time complexity equation? Question 2: Why do we always use the big O notation to find the time complexity of algorithms? Why don't we use other notations? How did the logn got into the complexity formula? For each step, you invoke the algorithm recursively on the first and second half. Thus - the total number of steps needed, is the number of times it will take to reach from n to 1 if you devide the problem

In Java, How do you quicksort an ArrayList of objects in which the sorting field is multiple layers deep?

梦想的初衷 提交于 2019-12-04 15:01:57
问题 Basically, I have a Container class called "Employees" which has in it an ArrayList. This ArrayList contains "Employee" objects, which in turn contain "EmployeeData" objects which in turn contain String objects such as "first" or "last" (which are employee names). Here's a diagram of the ArrayList structure: ArrayList[Employee] emps ==> 1:Many ==> Employee emp Employee emp ==> 1:1 ==> EmployeeData data EmployeeData data ==> 1:2 ==> String last // A string that contains employee's last name.

Quick sort with middle element as pivot

淺唱寂寞╮ 提交于 2019-12-04 14:06:53
问题 My understanding of quick sort is Choose a pivot element (in this case I am choosing middle element as pivot) Initialize left and right pointers at extremes. Find the first element to the left of the pivot which is greater than pivot. Similarly find the first element to the right of the pivot which is smaller than pivot Swap elements found in 3 and 4. Repeat 3,4,5 unless left >= right. Repeat the whole thing for left and right subarray as pivot is now placed at its place. I am sure I am

Quicksort (Java) causes StackOverFlow at array.length > 60k

浪尽此生 提交于 2019-12-04 13:38:53
My code works properly (to my knowledge) up until my input array size ( a.length ) is around 62,000 at which time I consistently get a StackOverFlowError . I previously had used 2 recursive calls to quicksort (less than, and greater than the pivot q ) and then I switched to tail recursion. As you can see, I'm selecting the pivot to be the value at the end of the array. I know this isn't the best way to choose a pivot, but I still shouldn't be seeing StackOverFlowError s with an array size this small, right? What could be causing this? Thanks in advance! Here's my code: public static void

Quicksort with first element as pivot example

眉间皱痕 提交于 2019-12-04 12:02:04
I am currently studying quicksort and would like to know how it works when the first (or last) element is chosen as the pivot point. Say for example I have the following array: {15, 19, 34, 41, 27, 13, 9, 11, 44} This is what I think happens: {15, 19, 34, 41, 27, 13, 9, 11, 44} ^ pivot {15, 19, 34, 41, 27, 13, 9, 11, 44} ^ ^ compare these two, they are good {15, 19, 34, 41, 27, 13, 9, 11, 44} ^ ^ compare these two and swap {11, 19, 34, 41, 27, 13, 9, 15, 44} ^ ^ compare these two and swap {9, 19, 34, 41, 27, 13, 11, 15, 44} ^ ^ compare these two, they are good {9, 19, 34, 41, 27, 13, 11, 15,

Maximum and minimum depth of quicksort

谁说我不能喝 提交于 2019-12-04 09:03:59
This was a problem of CLR (Introduction to Algorithms) The question goes as follow: Suppose that the splits at every level of quicksort are in the proportion 1 - α to α, where 0 < α ≤ 1/2 is a constant. Show that the minimum depth of a leaf in the recursion tree is approximately - lg n/ lg α and the maximum depth is approximately -lg n/ lg(1 - α). (Don't worry about integer round-off.) http://integrator-crimea.com/ddu0043.html I'm not getting how to reach this solution. as per the link they show that for a ratio of 1:9 the max depth is log n/log(10/9) and minimum log n/log(10). Then how can

3 way quicksort (C implementation)

南楼画角 提交于 2019-12-04 08:09:49
I try to implement some of the algorithms pure generic using C. I stick with the 3-way quicksort but somehow the implementation does not give correct output. The output nearly sorted but some keys aren't where it should be. The code is below. Thanks in advance. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> static void swap(void *x, void *y, size_t size) { void *tmp = malloc(size); memcpy(tmp, x, size); memcpy(x, y, size); memcpy(y, tmp, size); free(tmp); } static int cmpDouble(const void *i, const void *j) { if (*(double *)i < *(double *)j) return 1; else if (*

Space Complexity of Quick Sort

吃可爱长大的小学妹 提交于 2019-12-04 07:51:54
I have learnt that the space complexity of quick sort without Sedgewick's trick of eliminating tail recursion is O(n). But if we trace the calls on the stack that are stored, it is O(log n) steps at any call as shown in the figure. In the figure, while calculating the value of (1,1) we store the calls of [(1,8), (1,4), (1,2)] , while calculating the value of (3,3) we store the calls of [(1,8), (1,4), (3,4)] and so on which constitute for only O(log n) space at ant point of time. Then does the complexity become O(n) ? In the tree example you gave above, you showed a run of quicksort that always

Lazy Quicksort in Scala

杀马特。学长 韩版系。学妹 提交于 2019-12-04 07:28:24
Is it possible to do this sort of thing in Scala? def quicksort[A](xs: Stream[A])(implicit o: Ordering[A]): Stream[A] = { import o._ if (xs.isEmpty) xs else { val (smaller, bigger) = xs.tail.partition(_ < xs.head) quicksort(smaller) #::: xs.head #:: quicksort(bigger) } } It can be done with views as well, though it's bound to be much slower: def quicksort[A](xs: List[A])(implicit o: Ordering[A]) = { import o._ def qs(xs: SeqView[A, List[A]]): SeqView[A, Seq[_]] = if (xs.isEmpty) xs else { val (smaller, bigger) = xs.tail.partition(_ < xs.head) qs(smaller) ++ (xs.head +: qs(bigger)) } qs(xs.view