quicksort

Quicksort in LISP

心不动则不痛 提交于 2019-12-10 16:16:54
问题 I am trying to do a quicksort using LISP but I am having trouble with my functions output. (defun qsort (L) (cond ((null L) nil) (t(append (qsort (list< (car L) (cdr L))) (cons (car L) nil) (qsort (list>= (car L) (cdr L))))))) (defun list< (a b) (cond (( or(null a)(null b) nil)) (( < a (car b)) (list< a (cdr b))) (t(cons (car b) (list< a (cdr b)))))) (defun list>= (a b) (cond (( or( null a)(null b) nil)) (( >= a (car b)) (list> a (cdr b))) (t(cons (car b) (list> a (cdr b)))))) My problem

What is the difference between quicksort and tuned quicksort?

人盡茶涼 提交于 2019-12-10 14:36:02
问题 What is the fundamental difference between quicksort and tuned quicksort? What is the improvement given to quicksort? How does Java decide to use this instead of merge sort? 回答1: "Tuned" quicksort just means that some improvements are applied to the basic algorithm. Usually the improvements are to try and avoid worst case time complexity. Some examples of improvements might be to choose the pivot (or multiple pivots) so that there's never only 1 key in a partition, or only make the recursive

Why is insertion sort faster than quick-sort and bubble-sort for small cases?

心已入冬 提交于 2019-12-10 13:38:59
问题 I recently read an article that talked about the computation complexity of algorithms. The author mentioned "why insertion sort is faster than quick-sort and bubble-sort for small cases". Could anybody make some explanation for that? Does anybody know the actual complexity of each sort algorithm I mentioned above? 回答1: Consider two complexity functions: F(X) = X^2 G(X) = 4 * X * ln(X) F(3) = 9 G(3) = 13 So algorithm F wins for 3 items. But: F(100) = 10,000 G(100) = 1,842 So algorithm G wins

Quicksort Algorithm not assigning pivot correctly

半世苍凉 提交于 2019-12-10 13:28:23
问题 I watched this fantastic visualization of a quick sort algorithm: http://www.youtube.com/watch?v=Z5nSXTnD1I4 I felt I really understood the principles behind quick sort and, with the help of some guides online, set about creating my own quick sort. This is what I came up with: public void quickSort(int[] a, int left, int right) { int index = partition(a, left, right); if (left < index - 1) quickSort(a, left, index); if (index < right) quickSort(a, index + 1, right); } private int partition

Quick Sort on a Linked List with a random pivot in C

依然范特西╮ 提交于 2019-12-09 19:32:52
问题 I have spent a lot of time trying to work on this problem for a class and am at ends. I have found lots of resources regarding arrays and other ways of selecting a pivot but I am just at ends and am really going crazy here, any help would be so much appreciated you can not possibly imagine. #include <stdlib.h> /*and, malloc*/ #include <stdio.h> /*printf*/ struct listnode { struct listnode *next; long value; }; /*Finds length of list, which is usefull in selecting a random pivot*/ int

C++ : Fastest way to sort a list of number and their index

守給你的承諾、 提交于 2019-12-09 09:46:51
问题 I have a question that could seem very basic, but it is in a context where "every CPU tick counts" (this is a part of a larger algorithm that will be used on supercomputers). The problem is quite simple : what is the fastest way to sort a list of unsigned long long int numbers and their original indexes ? (At the beginning, the unsigned long long int numbers are in a completely random order.) Example : Before Numbers: 32 91 11 72 Indexes: 0 1 2 3 After Numbers: 11 32 72 91 Indexes: 2 0 3 1 By

Quicksort- how pivot-choosing strategies affect the overall Big-oh behavior of quicksort?

放肆的年华 提交于 2019-12-09 06:59:46
问题 I have came up with several strategies, but I am not entirely sure how they affect the overall behavior. I know the average case is O(NlogN), so I would assume that would be in the answer somewhere. I want to just put NlogN+1 for if I just select the 1st item in the array as the the pivot for the quicksort, but I don't know whether that is either correct nor acceptable? If anyone could enlighten me on this subject that would be great. Thanks! Possible Strategies: a) Array is random: pick the

Quicksort slower than Mergesort?

痞子三分冷 提交于 2019-12-09 05:06:15
问题 I was working on implementing a quicksort yesterday, and then I ran it, expecting a faster runtime than the Mergesort (which I had also implemented). I ran the two, and while the quicksort was faster for smaller data sets <100 elements (and I did verify that it works), the mergesort became the quicker algorithm fairly quickly. I had been taught that quicksort is almost always "quicker" than mergesort, and I understand that there is some debate on this topic, but I at least expected it to be

3 partition mergesort

本小妞迷上赌 提交于 2019-12-08 20:47:29
My professor assigned my class with implementing mergesort in arrays with 3-part partitioning and merging. That was the exact question from the professor. Problem is I have found no such thing as a 3-way mergesort I only know of a 3-way quicksort so I thought that he probably meant to take an array, split it into 3 parts and then mergesort those 3 parts together and I'm doing this by mergesorting the first 2 parts together and then mergesorting the combined part with the 3rd part. Did I think correctly and did I do the right thing (already implemented but I'm not posting the code since it

Quicksort drama

被刻印的时光 ゝ 提交于 2019-12-08 19:16:54
问题 I just could smash my head against the wall. I do not understand, why my following quicksort algorithm is not working. It is written in Pascal: program quicksort; Type iArray = array [0..8] of integer; var intArray, newArray : iArray; Function getIntArray(localIntArray : iArray) : iArray; begin localIntArray[0] := 3; localIntArray[1] := 1; localIntArray[2] := 8; localIntArray[3] := 4; localIntArray[4] := 9; localIntArray[5] := 0; localIntArray[6] := 8; localIntArray[7] := 2; localIntArray[8]