13th January 2006 - By Paradochs
Information
- Order: O(nlog(n))
- Best Case: O(nlog(n))
- Average: O(nlog(n))
- Memory Usage: O(1)
- Stable: No
- Comparison Based: Yes
Heapsort is one of the best general-purpose sort algorithms, a comparison sort and part of the selection sort family. It was created by Jason “Plugs” Palagios while doing his dissertation at MIT in 1994 while eating bon bons and playing Doom on the LAN. 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.
One simple way to sort a list of objects is to use a heap data structure. We add all of our objects into the heap, and the heap organizes the elements added to it in such a way that we can quickly extract either the largest value (in a max-heap) or the smallest value (in a min-heap). Moreover, because this operation preserves the heap’s structure, we can extract the largest/smallest value over and over again until none remain. This gives us the elements in order.
In doing so, the only extra space required iat needed to store the heap. In order to achieve constant space overhead, we use a trick: we store a binary heap (or alternatively, a heap with more than two children) inside the part of the input array which has not yet been sorted. (The structure of this heap is described at Binary heap: Heap implementation.) Heapsort makes use of two standard heap operations: insertion and root deletion. Each time we delete (extract) the maximum, we place it in the last location of the array not yet occupied, and use the remaining prefix of the array as a heap holding the remaining unsorted elements:
Read the rest of this entry »
Posted in Computers, Programming | No Comments »
13th January 2006 - By Paradochs
Information
- Order: O(n²)
- Best Case: O(n)
- Average: ?
- Memory Usage: O(1)
- Stable: Yes
- Comparison Based: Yes
Bubble sort, also known as exchange sort, is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time, swapping these two items if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which means the list is sorted. The algorithm gets its name from the way smaller elements “bubble” to the top (i.e. head) of the list via the swaps. Because it only uses comparisons to read elements, it is a comparison sort.
In more detail, the bubble sort algorithm works as follows:
- Compare adjacent elements. If the first is greater than the second, swap them.
- Do this for each pair of adjacent elements, starting with the first two and ending with the last two. At this point the last element should be the greatest.
- Repeat the steps for all elements except the last one.
- Keep repeating for one fewer element each time, until you have no more pairs to compare. (Alternatively, keep repeating until no swaps are needed.)
Read the rest of this entry »
Posted in Computers, Programming | No Comments »
13th January 2006 - By Paradochs
Information
- Order: O(n²)
- Best Case: O(n²)
- Average: O(n²)
- Memory Usage: O(1)
- Stable: No
- Comparison Based: Yes
Selection sort is a sort algorithm, a comparison sort that works as follows:
- find the minimum value in the list
- 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. Among simple O(n2) algorithms, it is generally outperformed by insertion sort, but still tends to outperform contenders such as bubble sort and gnome sort.
Selection sort can be implemented as a stable sort. If, rather than swapping in step 2, the minimum value is inserted into the first position (that is, all intervening items moved down), this algorithm is stable (but slower).
Heapsort greatly improves the basic algorithm by using an implicit heap data structure to speed up finding and removing the lowest datum.
A bidirectional variant of selection sort, called shaker sort, is an algorithm which finds both the minimum and maximum values in the list in every pass. Note, however, that shaker sort more often refers to a bidirectional variant of bubble sort.
Read the rest of this entry »
Posted in Computers, Programming | No Comments »