Lunes, Pebrero 13, 2012

SORTING

Research an application or scenario that the following sorting algorithms can be applied (include a description and an image):

  • Bubble Sort
known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.








  • Selection Sort
 Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

Selection Sort Animation


  • Insertion Sort
 is a simple sorting algorithm: a comparison sort in which the sorted array (or list) is built one entry at a time.

 




 
  • Shell Sort
  is an in-place comparison sort. It generalizes an exchanging sort, such as insertion or bubble sort, by allowing the comparison and exchange of elements that lie far apart

 Step-by-step visualisation of Shellsort



 
  • Bucket 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.
 




  • Merge Sort
 is an O(n log n) comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output.
Merge-sort-example-300px.gif
 
  • Quick sort
  a sorting algorithm developed by Tony Hoare that, on average, makes O(nlog n) comparisons to sort n items.
 Quicksort in action on a list of numbers. The horizontal lines are pivot values.
    BUBBLE SORT

    It is also known as sinking sort.

    It is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.


    Ascending Sort.
     
     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 ), Swap since 5 > 4


    ( 1 4 5 2 8 )  ( 1 4 2 5 8 ), Swap since 5 > 2


    ( 1 4 2 5 8 )  ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), 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 ), Swap since 4 > 2
    ( 1 2 4 5 8 )  ( 1 2 4 5 8 )
    ( 1 2 4 5 8 )  ( 1 2 4 5 8 )
    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 )