1 / 14

Rendezések 2.

Rendezések 2. http://wikipedia.org. Quicksort 1.

ginny
Télécharger la présentation

Rendezések 2.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Rendezések 2. http://wikipedia.org

  2. Quicksort 1. Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare that, on average, makes Θ(n log n) comparisons to sort n items. However, in the worst case, it makes Θ(n2) comparisons. Typically, quicksort is significantly faster in practice than other Θ(n log n) algorithms, because its inner loop can be efficiently implemented on most architectures, and in most real-world data it is possible to make design choices which minimize the possibility of requiring quadratic time.

  3. Quicksort 2 Quicksort sorts by employing a divide and conquer strategy to divide a list into two sub-lists. The steps are: • Pick an element, called a pivot (forgócsap), from the list. • Reorder the list so that all elements which are less than the pivot come before the pivot and so that all elements greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. • Recursively sort the sub-list of lesser elements and the sub-list of greater elements. • The base case of the recursion are lists of size zero or one, which are always sorted. The algorithm always terminates because it puts at least one element in its final place on each iteration.

  4. Qsort deklarációk program qsort; {$APPTYPE CONSOLE} uses SysUtils, Math; const n=12; type tipus = array[1..n] of integer; Var tomb : tipus; i : integer; RandSeed: LongInt = 0;

  5. qsort procedure procedure QuickSort(var A: tipus; iLo, iHi: Integer); var Lo, Hi, Mid, T: Integer; begin Lo := iLo; Hi := iHi; Mid := A[(Lo + Hi) div 2]; repeat while A[Lo] < Mid do Inc(Lo); while A[Hi] > Mid do Dec(Hi); if Lo <= Hi then begin T := A[Lo]; A[Lo] := A[Hi]; A[Hi] := T; Inc(Lo); Dec(Hi); end; until Lo > Hi; if Hi > iLo then QuickSort(A, iLo, Hi); if Lo < iHi then QuickSort(A, Lo, iHi); end;

  6. End of qsort begin Randomize; for i:= 1 to n do tomb[i]:= ceil(10*random) - 2; writeln; for i:=1 to n do write(tomb[i],' '); readln; QuickSort(tomb, 0, n); writeln('VEGEREDMENY:'); for i:=1 to n do write(tomb[i],' '); readln; end.

  7. Qsort analysis 1 Quicksort's inner loop, which performs the partition, is amenable to optimization on typical modern machines for two main reasons: • All comparisons are being done with a single pivot value, which can be stored in a register. • The list is being traversed sequentially, i.e. the elements are close in the memory so effective cache usage is possible. • This close fit with modern architectures makes quicksort one of the fastest sorting algorithms on average. Because of this excellent average performance and simple implementation, quicksort has become one of the most popular sorting algorithms in practical use.

  8. Qsort analysis 2 • Although it is often believed to work in-place, quicksort uses O(log n) additional stack space on average in recursive implementations and O(n) stack space in the worst case. • However, if the input is already sorted, a common practical case, and the pivot element (Mid above) is selected e.g. always the first one, this implementation of quicksort degenerates into a selection sort with O(n2) running time. When using the simplest variant, the recursion depth also becomes linear, requiring O(n) extra stack space.

  9. Selection sort 1 procedure SelectionSort(var x: tipus); var i,j,min, temp: integer; begin for i := 1 To n - 1 do begin min := i; For j := i + 1 To n do If x[min] > x[j] Then min := j; temp := x[i]; x[i] := x[min]; x[min]:= temp; end; end;

  10. Selection sort 2 The idea is to • find the minimum value in the list and • swap it with the value in the first position • sort the remainder of the list (excluding the first value). It is probably the most intuitive sort algorithm to invent. This algorithm, iterating through a list of n unsorted items, has a worst-case, average-case, and best-case run-time of Θ(n2), assuming that comparisons can be done in constant time. It is generally outperformed by insertion sort. Selection sort is not a stable sort.

  11. Returning to qsort • kozep := ceil(abs(Hi - Lo)*random) +Lo ; • Mid := A[kozep]; Is a possible modification of qsort if pivots are chosen randomly, most poor choices of pivots are unlikely; the worst-case, for example, has only probability 1/n! of occurring. This variation, called randomized quicksort, can be shown to use O(n log n) comparisons on any input with very high probability.

  12. Heapsort Heapsort is one of the best general-purpose sort algorithms, and is part of the selection sort family. Although somewhat slower in practice on most machines than a good implementation of quicksort, it has the advantages of worst-case O(n log n) runtime and being an in-place algorithm. Heapsort is not a stable sort.

  13. Numerical Recipes (C,C++,Fortran,Pascal) http://www.library.cornell.edu/nr/cbookcpdf.html A very good professional source… A lot of mathematics… A lot of C++… (What more does one need for happiness?)

  14. Idea of heapsort Let a1,a2,…,an be numbers. They form a heap if for all j such that 1≤ j div 2 < j ≤ n we have aj/2 ≥ aj Assume that we have been able to organise our data into a heap. Then sorting goes as follows. a1 is the greatest, ‘the boss’. If he resigns, one of his deputies, a2 or a3, whoever is greater, takes over and we get a new heap. The new boss resigns after a while, and everything goes on recursively…

More Related