Harmonic oscillator: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
fixed varphi to phi
Line 1: Line 1:
{{Use dmy dates|date=July 2012}}
My name: Clark Harcus<br>My age: 21 years old<br>Country: France<br>City: La Varenne-Saint-Hilaire <br>Postal code: 94210<br>Street: 25 Rue Du Clair Bocage<br><br>Feel free to surf to my web site :: [http://www.Xxzrmyy.com/plus/guestbook.php Fifa Coin Generator]
{{Infobox Algorithm|class=[[Sorting algorithm]]
|image=[[Image:Sorting heapsort anim.gif|none|A run of the heapsort algorithm sorting an array of randomly permuted values. In the first stage of the algorithm  array elements are reordered to satisfy the [[Heap (data structure)|heap property]]. Before the actual sorting takes place, the heap tree structure is shown briefly for illustration.]]</small>A run of the heapsort algorithm sorting an array of randomly permuted values. In the first stage of the algorithm the array elements are reordered to satisfy the [[Heap (data structure)|heap property]]. Before the actual sorting takes place, the heap tree structure is shown briefly for illustration.</small>
|data=[[Array data structure|Array]]
|time=<math>O(n\text{ }\log\text{ }n)</math>
|average-time=<math>O(n\text{ }\log\text{ }n)</math>
|best-time=<math>\Omega(n), O(n\text{ }\log\text{ }n)</math><ref>http://dx.doi.org/10.1006/jagm.1993.1031</ref>
|space=<math>O(1)</math> auxiliary
|optimal=Never
}}
'''Heapsort''' is a [[comparison sort|comparison-based]] [[sorting algorithm]] to create a [[sorted array]] (or list), and is part of the [[selection sort]] family. Although somewhat slower in practice on most machines than a well-implemented [[quicksort]], it has the advantage of a more favorable worst-case [[big O notation|O]](''n'' log ''n'') runtime.  Heapsort is an [[in-place algorithm]], but it is not a [[stable sort]]. It was invented by [[J. W. J. Williams]] in 1964.<ref>{{harvnb|Williams|1964}}</ref>
 
== Overview ==
The heapsort algorithm can be divided into two parts.
 
In the first step, a [[Heap (data structure)|heap]] is [[Binary heap#Building a heap|built]] out of the data.
 
In the second step, a sorted array is created by repeatedly removing the largest element from the heap, and inserting it into the array. The heap is reconstructed after each removal. Once all objects have been removed from the heap, we have a sorted array. The direction of the sorted elements can be varied by choosing a min-heap or max-heap in step one.
 
Heapsort can be performed in place. The array can be split into two parts, the sorted array and the heap. The storage of heaps as arrays is diagrammed [[Binary heap#Heap implementation|here]].  The heap's invariant is preserved after each extraction, so the only cost is that of extraction.
 
== Variations ==
*The most important variation to the simple variant is an improvement by [[Robert Floyd|R. W. Floyd]] that, in practice, gives about a 25% speed improvement by using only one comparison in each [[Binary heap#Insert|siftup]] run, which must be followed by a [[Binary heap#Delete|siftdown]] for the original child. Moreover, it is more elegant to formulate. Heapsort's natural way of indexing works on indices from 1 up to the number of items. Therefore the start address of the data should be shifted such that this logic can be implemented avoiding unnecessary +/- 1 offsets in the coded algorithm. The worst-case number of comparisons during the Floyd's heap-construction phase of Heapsort is known to be equal to 2N − 2s<sub>2</sub>(N) − e<sub>2</sub>(N), where s<sub>2</sub>(N) is the sum of all digits of the binary representation of N and e<sub>2</sub>(N) is the exponent of 2 in the prime factorization of N.<ref>{{citation
| last1 = Suchenek | first1 = Marek A.
| title = Elementary Yet Precise Worst-Case Analysis of Floyd's Heap-Construction Program
| doi = 10.3233/FI-2012-751
| pages = 75–92
| publisher = IOS Press
| journal = Fundamenta Informaticae
| volume = 120
| issue = 1
| year = 2012
| url = http://www.deepdyve.com/lp/ios-press/elementary-yet-precise-worst-case-analysis-of-floyd-s-heap-50NW30HMxU}}.</ref>
*[[Ternary heapsort]]<ref>"Data Structures Using Pascal", 1991, page 405, gives a ternary heapsort as a student exercise. "Write a sorting routine similar to the heapsort except that it uses a ternary heap."</ref> uses a ternary heap instead of a binary heap; that is, each element in the heap has three children. It is more complicated to program, but does a constant number of times fewer swap and comparison operations.  This is because each step in the shift operation of a ternary heap requires three comparisons and one swap, whereas in a binary heap two comparisons and one swap are required. The ternary heap does two steps in less time than the binary heap requires for three steps, which multiplies the index by a factor of 9 instead of the factor 8 of three binary steps. Ternary heapsort is about 12% faster than the simple variant of binary heapsort.{{Citation needed|date=March 2007}}
*The '''[[smoothsort]]''' algorithm<ref>http://www.cs.utexas.edu/users/EWD/ewd07xx/EWD796a.PDF</ref><ref>http://www.cs.utexas.edu/~EWD/transcriptions/EWD07xx/EWD796a.html</ref> is a variation of heapsort developed by [[Edsger W. Dijkstra|Edsger Dijkstra]] in 1981. Like heapsort, smoothsort's upper bound is [[Big O notation|O]](''n'' log&nbsp;''n''). The advantage of smoothsort is that it comes closer to O(''n'') time if the [[Adaptive sort|input is already sorted to some degree]], whereas heapsort averages O(''n'' log&nbsp;''n'') regardless of the initial sorted state. Due to its complexity, smoothsort is rarely used.
*Levcopoulos and Petersson<ref>{{citation
| last1 = Levcopoulos | first1 = Christos
| last2 = Petersson | first2 = Ola
| contribution = Heapsort - Adapted for Presorted Files
| doi = 10.1007/3-540-51542-9_41
| location = London, UK
| pages = 499–509
| publisher = Springer-Verlag
| series = Lecture Notes in Computer Science
| title = WADS '89: Proceedings of the Workshop on Algorithms and Data Structures
| volume = 382
| year = 1989}}.</ref> describe a variation of heapsort based on a [[Cartesian tree]] that does not add an element to the heap until smaller values on both sides of it have already been included in the sorted output. As they show, this modification can allow the algorithm to sort more quickly than O(''n''&nbsp;log&nbsp;''n'') for inputs that are already nearly sorted.
 
== Comparison with other sorts ==
Heapsort primarily competes with [[quicksort]], another very efficient general purpose nearly-in-place comparison-based sort algorithm.
 
Quicksort is typically somewhat faster due to some factors, but the worst-case running time for quicksort is [[Big O notation|O]](''n''<sup>2</sup>), which is unacceptable for large data sets and can be deliberately triggered given enough knowledge of the implementation, creating a security risk. See [[quicksort]] for a detailed discussion of this problem and possible solutions.
 
Thus, because of the O(''n'' log ''n'') upper bound on heapsort's running time and constant upper bound on its auxiliary storage, embedded systems with real-time constraints or systems concerned with security often use heapsort.
 
Heapsort also competes with [[merge sort]], which has the same time bounds. Merge sort requires Ω(n) auxiliary space, but heapsort requires only a constant amount. Heapsort typically runs faster in practice on machines with small or slow [[data cache]]s. On the other hand, merge sort has several advantages over heapsort:
* Merge sort on arrays has considerably better data cache performance, often outperforming heapsort on modern desktop computers because merge sort frequently accesses contiguous memory locations (good [[locality of reference]]); heapsort references are spread throughout the heap.
* Heapsort is not a [[stable sort]]; merge sort is stable.
* Merge sort [[parallel algorithm|parallelizes]] well and can achieve close to [[linear speedup]] with a trivial implementation; heapsort is not an obvious candidate for a parallel algorithm.
* Merge sort can be adapted to operate on [[linked list]]s with O(1) extra space. Heapsort can be adapted to operate on doubly linked lists with only O(1) extra space overhead.{{Citation needed|date=June 2012}}
* Merge sort is used in [[external sorting]]; heapsort is not. Locality of reference is the issue.
 
[[Introsort]] is an alternative to heapsort that combines quicksort and heapsort to retain advantages of both: worst case speed of heapsort and average speed of quicksort.
 
== Pseudocode ==
 
The following is the "simple" way to implement the algorithm in [[pseudocode]]. Arrays are '''[[Comparison of programming languages (array)|zero-based]]''' and ''swap'' is used to exchange two elements of the array. Movement 'down' means from the root towards the leaves, or from lower indices to higher. Note that during the sort, the largest element is at the root of the heap at a[0], while at the end of the sort, the largest element is in a[end].
 
'''function''' heapsort(a, count) '''is'''
    '''input:''' an unordered array ''a'' of length ''count''
 
    heapify(a, count)
    (: The following loop maintains the [[Loop invariant|invariants]] that a[0:end] is a heap and every element
      : beyond end is greater than everything before it (so a[end:count] is in sorted order).
      :)
    end ← count - 1
    '''while''' end > 0 '''do'''
        swap(a[end], a[0])
        end ← end - 1
        siftDown(a, 0, end)         
 
(: Put elements of a in heap order, in-place :)
'''function''' heapify(a, count) '''is'''
    ''(start is assigned the index in a of the last parent node)''
    start ← (count - 2 ) / 2
   
    '''while''' start ≥ 0 '''do'''
        ''(sift down the node at index start to the proper place such that all nodes below''
        '' the start index are in heap order)''
        siftDown(a, start, count-1)
        start ← start - 1
    ''(after sifting down the root all nodes/elements are in heap order)''
'''function''' siftDown(a, start, end) '''is'''
    root ← start
    '''while''' root * 2 + 1 ≤ end '''do'''    (: While the root has at least one child :)
        child ← root * 2 + 1      (: left child :)
        swap ← root                (: keeps track of child to swap with :)
        '''if''' a[swap] < a[child]
            swap ← child
        '''if''' child+1 ≤ end '''and''' a[swap] < a[child+1]
            swap ← child + 1
        '''if''' swap ≠ root
            swap(a[root], a[swap])
            root ← swap            (: repeat to continue sifting down the child now :)
        '''else'''
            '''return'''
 
The heapify function can be thought of as building a heap from the bottom up, successively shifting downward to establish the [[Heap (data structure)|heap property]]. An alternative version (shown below) that builds the heap top-down and sifts upward may be conceptually simpler to grasp. This "siftUp" version can be visualized as starting with an empty heap and successively inserting elements, whereas the "siftDown" version given above treats the entire input array as a full, "broken" heap and "repairs" it starting from the last non-trivial sub-heap (that is, the last parent node).
 
[[File:Binary heap bottomup vs topdown.svg|thumb|right|Difference in time complexity between the "siftDown" version and the "siftUp" version.]]
Also, the "siftDown" version of heapify [[Binary heap#Building a heap|has {{math|''O''(''n'')}} time complexity]], while the "siftUp" version given below has {{math|''O''(''n'' log ''n'')}} time complexity due to its equivalence with inserting each element, one at a time, into an empty heap.<ref>{{cite web|title=Priority Queues|url=http://faculty.simpson.edu/lydia.sinapova/www/cmsc250/LN250_Weiss/L10-PQueues.htm|accessdate=24 May 2011}}</ref>
This may seem counter-intuitive since, at a glance, it is apparent that the former only makes half as many calls to its logarithmic-time sifting function as the latter; i.e., they seem to differ only by a constant factor, which never has an impact on asymptotic analysis.
 
To grasp the intuition behind this difference in complexity, note that the number of swaps that may occur during any one siftUp call ''increases'' with the depth of the node on which the call is made. The crux is that there are many (exponentially many) more "deep" nodes than there are "shallow" nodes in a heap, so that siftUp may have its full logarithmic running-time on the approximately linear number of calls made on the nodes at or near the "bottom" of the heap. On the other hand, the number of swaps that may occur during any one siftDown call ''decreases'' as the depth of the node on which the call is made increases. Thus, when the "siftDown" heapify begins and is calling siftDown on the bottom and most numerous node-layers, each sifting call will incur, at most, a number of swaps equal to the "height" (from the bottom of the heap) of the node on which the sifting call is made. In other words, about half the calls to siftDown will have at most only one swap, then about a quarter of the calls will have at most two swaps, etc.
 
The heapsort algorithm itself has {{math|''O''(''n'' log ''n'')}} time complexity using either version of heapify.
 
  '''function''' heapify(a,count) is
      ''(end is assigned the index of the first (left) child of the root)''
      end := 1
     
      '''while''' end < count
          ''(sift up the node at index end to the proper place such that all nodes above''
          '' the end index are in heap order)''
          siftUp(a, 0, end)
          end := end + 1
      ''(after sifting up the last node all nodes are in heap order)''
 
  '''function''' siftUp(a, start, end) '''is'''
      '''input: ''' ''start represents the limit of how far up the heap to sift.''
                    ''end is the node to sift up.''
      child := end
      '''while''' child > start
          parent := floor((child - 1) / 2)
          '''if''' a[parent] < a[child] '''then''' ''(out of max-heap order)''
              swap(a[parent], a[child])
              child := parent ''(repeat to continue sifting up the parent now)''
          '''else'''
              '''return'''
 
== Example ==
Let { 6, 5, 3, 1, 8, 7, 2, 4 } be the list that we want to sort from the smallest to the largest. (NOTE, for 'Building the Heap' step: Larger nodes don't stay below smaller node parents. They are swapped with parents, and then recursively checked if another swap is needed, to keep larger numbers above smaller numbers on the heap binary tree.)
[[File:Heapsort-example.gif|350px|thumb|right|An example on heapsort.]]
'''1. Build the heap'''
{| class="wikitable"
|-
! Heap !! newly added element !! swap elements
|-
| nil || 6 || 
|-
| 6 || 5 || 
|-
| 6, 5 || 3 || 
|-
| 6, 5, 3 || 1 || 
|-
| 6, 5, 3, 1 || 8 || 
|-
| 6, '''5''', 3, 1, '''8 '''||  || 5, 8
|-
| '''6''', '''8''', 3, 1, 5 ||  || 6, 8
|-
| 8, 6, 3, 1, 5 || 7 || 
|-
| 8, 6, '''3''', 1, 5, '''7''' ||  || 3, 7
|-
| 8, 6, 7, 1, 5, 3 || 2 || 
|-
| 8, 6, 7, 1, 5, 3, 2 || 4 || 
|-
| 8, 6, 7, '''1''', 5, 3, 2, '''4''' ||  || 1, 4
|-
| 8, 6, 7, 4, 5, 3, 2, 1 ||  || 
|}
 
'''2. Sorting.'''
{| class="wikitable"
|-
! Heap !! swap elements !! delete element !! sorted array !! details
|-
| '''8''', 6, 7, 4, 5, 3, 2, '''1''' || 8, 1 ||  ||  || swap 8 and 1 in order to delete 8 from heap
|-
| 1, 6, 7, 4, 5, 3, 2, '''8''' ||  || 8 ||  || delete 8 from heap and add to sorted array
|-
| '''1''', 6, '''7''', 4, 5, 3, 2 || 1, 7 ||  || 8 || swap 1 and 7 as they are not in order in the heap
|-
| 7, 6, '''1''', 4, 5, '''3''', 2 || 1, 3 ||  || 8 || swap 1 and 3 as they are not in order in the heap
|-
| '''7''', 6, 3, 4, 5, 1, '''2''' || 7, 2 ||  || 8 || swap 7 and 2 in order to delete 7 from heap
|-
| 2, 6, 3, 4, 5, 1, '''7''' ||  || 7 || 8 || delete 7 from heap and add to sorted array
|-
| '''2''', '''6''', 3, 4, 5, 1 || 2, 6 ||  || 7, 8 || swap 2 and 6 as they are not in order in the heap
|-
| 6, '''2''', 3, 4, '''5''', 1 || 2, 5 ||  || 7, 8 || swap 2 and 5 as they are not in order in the heap
|-
| '''6''', 5, 3, 4, 2, '''1'''|| 6, 1 ||  || 7, 8 || swap 6 and 1 in order to delete 6 from heap
|-
| 1, 5, 3, 4, 2, '''6''' ||  || 6 || 7, 8 || delete 6 from heap and add to sorted array
|-
| '''1''', '''5''', 3, 4, 2 || 1, 5 ||  || 6, 7, 8 || swap 1 and 5 as they are not in order in the heap
|-
| 5, '''1''', 3, '''4''', 2 || 1, 4 ||  || 6, 7, 8 || swap 1 and 4 as they are not in order in the heap
|-
| '''5''', 4, 3, 1, '''2''' || 5, 2 ||  || 6, 7, 8 || swap 5 and 2 in order to delete 5 from heap
|-
| 2, 4, 3, 1, '''5''' ||  || 5 || 6, 7, 8 || delete 5 from heap and add to sorted array
|-
| '''2''', '''4''', 3, 1 || 2, 4 ||  || 5, 6, 7, 8 || swap 2 and 4 as they are not in order in the heap
|-
| '''4''', 2, 3, '''1''' || 4, 1 ||  || 5, 6, 7, 8 || swap 4 and 1 in order to delete 4 from heap
|-
| 1, 2, 3, '''4''' ||  || 4 || 5, 6, 7, 8 || delete 4 from heap and add to sorted array
|-
| '''1''', 2, '''3''' || 1, 3 ||  || 4, 5, 6, 7, 8 || swap 1 and 3 as they are not in order in the heap
|-
| '''3''', 2, '''1''' || 3, 1 ||  || 4, 5, 6, 7, 8 || swap 3 and 1 in order to delete 3 from heap
|-
| 1, 2, '''3''' ||  || 3 || 4, 5, 6, 7, 8 || delete 3 from heap and add to sorted array
|-
| '''1''', '''2''' || 1, 2 ||  || 3, 4, 5, 6, 7, 8 || swap 1 and 2 as they are not in order in the heap
|-
| '''2''', '''1''' || 2, 1 ||  || 3, 4, 5, 6, 7, 8 || swap 2 and 1 in order to delete 2 from heap
|-
| 1, '''2''' ||  || 2 || 3, 4, 5, 6, 7, 8 || delete 2 from heap and add to sorted array
|-
| '''1''' ||  || 1 || 2, 3, 4, 5, 6, 7, 8 || delete 1 from heap and add to sorted array
|-
|  ||  ||  || 1, 2, 3, 4, 5, 6, 7, 8 || completed
|}
 
== Notes ==
{{reflist|30em}}
 
== References ==
* {{Citation |first=J. W. J. |last=Williams |author-link=J. W. J. Williams |title=Algorithm 232 - Heapsort |year=1964 |journal=[[Communications of the ACM]] |volume=7 |issue=6 |pages=347–348 |doi= }}
* {{Citation |first=Robert W. |last=Floyd |author-link=Robert W. Floyd |title=Algorithm 245 - Treesort 3 |year=1964 |journal=[[Communications of the ACM]] |volume=7 |issue=12 |page=701 |doi= }}
* {{Citation |first=Svante |last=Carlsson |author-link=Svante Carlsson |title=Average-case results on heapsort |year=1987 |journal=BIT |volume=27 |issue=1 |pages=2–17 |doi= }}
* {{Citation |first=Donald |last=Knuth |author-link=Donald Knuth |series=[[The Art of Computer Programming]] |volume=3 |title=Sorting and Searching |edition=third |publisher=Addison-Wesley |year=1997 |isbn=0-201-89685-0 |pages=144–155 |contribution=&sect;5.2.3, Sorting by Selection }}
* [[Thomas H. Cormen]], [[Charles E. Leiserson]], [[Ronald L. Rivest]], and [[Clifford Stein]]. ''[[Introduction to Algorithms]]'', Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Chapters 6 and 7 Respectively: Heapsort and Priority Queues
* [http://www.cs.utexas.edu/users/EWD/ewd07xx/EWD796a.PDF A PDF of Dijkstra's original paper on Smoothsort]
* [http://cis.stvincent.edu/html/tutorials/swd/heaps/heaps.html Heaps and Heapsort Tutorial] by David Carlson, St. Vincent College
* [http://www.nicollet.net/2009/01/heaps-of-knowledge/ Heaps of Knowledge]
 
== External links ==
{{wikibooks|Algorithm implementation|Sorting/Heapsort|Heapsort}}
* [http://www.sorting-algorithms.com/heap-sort Animated Sorting Algorithms: Heap Sort] –  graphical demonstration and discussion of heap sort
* [http://olli.informatik.uni-oldenburg.de/heapsort_SALA/english/start.html Courseware on Heapsort from Univ. Oldenburg] - With text, animations and interactive exercises
* [http://www.nist.gov/dads/HTML/heapSort.html NIST's Dictionary of Algorithms and Data Structures: Heapsort]
* [http://www.codecodex.com/wiki/Heapsort Heapsort implemented in 12 languages]
* [http://www.azillionmonkeys.com/qed/sort.html Sorting revisited] by Paul Hsieh
* [http://coderaptors.com/?HeapSort A color graphical Java applet] that allows experimentation with initial state and shows statistics
* [http://employees.oneonta.edu/zhangs/powerPointPlatform/index.php A PowerPoint presentation demonstrating how Heap sort works] that is for educators.
* [http://opendatastructures.org/versions/edition-0.1e/ods-java/11_1_Comparison_Based_Sorti.html#SECTION001413000000000000000 Open Data Structures - Section 11.1.3 - Heap-Sort]
 
{{sorting}}
 
[[Category:Sorting algorithms]]
[[Category:Comparison sorts]]
[[Category:Heaps (data structures)]]
[[Category:Articles with example pseudocode]]
 
[[no:Sorteringsalgoritme#Heap sort]]

Revision as of 11:50, 7 February 2014

My name: Clark Harcus
My age: 21 years old
Country: France
City: La Varenne-Saint-Hilaire
Postal code: 94210
Street: 25 Rue Du Clair Bocage

Feel free to surf to my web site :: Fifa Coin Generator