Sunday, March 22, 2009

Sorting Algorithms

1.) Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

First Pass:
( 5 1 4 2 8 ) ---> ( 1 5 4 2 8 ) Here, algorithm compares the first two elements,
and swaps them.
( 1 5 4 2 8 ) ---> ( 1 4 5 2 8 )
( 1 4 5 2 8 ) ---> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) ---> ( 1 4 2 5 8 ) Now, since these elements are already in order,
algorithm does not swap them.
Second Pass:
( 1 4 2 5 8 ) ---> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) ---> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ---> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ---> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed.
Algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) ---> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ---> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ---> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ---> ( 1 2 4 5 8 )
Finally, the array is sorted, and the algorithm can terminate.

[wiki] http://en.wikipedia.org/wiki/Bubble_sort

2.) Insertion sort- is a simple sorting algorithm that is relatively efficient for small lists and mostly-sorted lists, and often is used as part of more sophisticated algorithms. It works by taking elements from the list one by one and inserting them in their correct position into a new sorted list. In arrays, the new list and the remaining elements can share the array's space, but insertion is expensive, requiring shifting all following elements over by one.


http://web.syr.edu/~tjhaskel/hmk8a.gif


web.syr.edu/~tjhaskel/hmk8a.gif

image from: web.syr.edu/~tjhaskel/

[wiki] http://en.wikipedia.org/wiki/Sorting_algorithm#Insertion_sort


3.) Shell sort- was invented by Donald Shell in 1959. It improves upon bubble sort and insertion sort by moving out of order elements more than one position at a time. One implementation can be described as arranging the data sequence in a two-dimensional array and then sorting the columns of the array using insertion sort. Although this method is inefficient for large data sets, it is one of the fastest algorithms for sorting small numbers of elements (sets with fewer than 1000 or so elements). Another advantage of this algorithm is that it requires relatively small amounts of memory.

http://oopweb.com/Algorithms/Documents/Sman/Volume/ShellSort_files/title_data/fig22.gif
26 x 284 - 4k - gif - oopweb.com/.../title_data/fig22.gif

image at: www.oopweb.com/.../Sman/Volume/ShellSort.html

[wiki] http://en.wikipedia.org/wiki/Sorting_algorithm#Shell_sort

4.) Merge sort- takes advantage of the ease of merging already sorted lists into a new sorted list. It starts by comparing every two elements (i.e., 1 with 2, then 3 with 4...) and swapping them if the first should come after the second. It then merges each of the resulting lists of two into lists of four, then merges those lists of four, and so on; until at last two lists are merged into the final sorted list.

http://upload.wikimedia.org/wikipedia/commons/6/60/Mergesort_algorithm_diagram.png


upload.wikimedia.org/wikipedia/commons/6/60/M...

image at: commons.wikimedia.org/wiki/File:Mergesort_alg...

[wiki] http://en.wikipedia.org/wiki/Sorting_algorithm#Merge_sort

5.) Heapsort- is a much more efficient version of selection sort. It also works by determining the largest (or smallest) element of the list, placing that at the end (or beginning) of the list, then continuing with the rest of the list, but accomplishes this task efficiently by using a data structure called aheap, a special type of binary tree. Once the data list has been made into a heap, the root node is guaranteed to be the largest element. When it is removed and placed at the end of the list, the heap is rearranged so the largest element remaining moves to the root. Using the heap, finding the next largest element takes O(log n) time, instead of O(n) for a linear scan as in simple selection sort. This allows Heapsort to run in O(n log n) time.

http://upload.wikimedia.org/wikipedia/commons/thumb/6/64/HeapsortSampleBefore.svg/625px-HeapsortSampleBefore.svg.png

625 x 375 - 23k - png - upload.wikimedia.org/wikipedia/commons/thumb/...

image at: commons.wikimedia.org/wiki/File:HeapsortSampl...

[wiki] http://en.wikipedia.org/wiki/Sorting_algorithm#Heapsort

6.) Quicksort is a divide and conquer algorithm which relies on a partition operation: to partition an array, we choose an element, called a pivot, move all smaller elements before the pivot, and move all greater elements after it. This can be done efficiently in linear time andin-place We then recursively sort the lesser and greater sublists. Efficient implementations of quicksort (with in-place partitioning) are typically unstable sorts and somewhat complex, but are among the fastest sorting algorithms in practice.

http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site/Database/algorithm/Quick_Sort?action=download&value=quick.png

295 x 281 - 8k - www.joinc.co.kr/modules/moniwiki/wiki.php/Sit...

image at: teamblog.joinc.co.kr/yundream/127

[wiki] http://en.wikipedia.org/wiki/Sorting_algorithm#Quicksort

7.) Bucket sort, or bin sort, is a sorting algorithm that works by partitioning an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a cousin of radix sort in the most to least significant digit flavour. Since bucket sort is not a comparison sort, the Ω(n log n) lower bound is inapplicable. Estimates involve the number of buckets.

http://upload.wikimedia.org/wikipedia/commons/6/61/Bucket_sort_1.png

11 x 131 - 4k - png - upload.wikimedia.org/.../6/61/Bucket_sort_1.png

image at: commons.wikimedia.org/wiki/File:Bucket_sort_1.png

[wiki] http://en.wikipedia.org/wiki/Sorting_algorithm#Bucket_sort

No comments:

Post a Comment