Lunes, Pebrero 20, 2012

Shorting,

  •   Bubble sort, often incorrectly referred to 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. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, some other algorithms are more efficient for sorting large lists.  
  •  
  • Step-by-step example
  • Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in bold are being compared. Three passes will be required.
    First Pass:
    ( 5 1 4 2 8 ) \to ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps them.
    ( 1 5 4 2 8 ) \to ( 1 4 5 2 8 ), Swap since 5 > 4
    ( 1 4 5 2 8 ) \to ( 1 4 2 5 8 ), Swap since 5 > 2
    ( 1 4 2 5 8 ) \to ( 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 ) \to ( 1 4 2 5 8 )
    ( 1 4 2 5 8 ) \to ( 1 2 4 5 8 ), Swap since 4 > 2
    ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
    ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
    Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
     

    Third Pass:
    ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
    ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
    ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )
    ( 1 2 4 5 8 ) \to ( 1 2 4 5 8 )





      
  • Selection Sort
  • a Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion 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. 
  • example
     
    64 25 12 22 11
    
    11 25 12 22 64
    
    11 12 25 22 64
    
    11 12 22 25 64
    
    11 12 22 25 64
     
     
     
     
     
  • Insertion Sort
  •  Insertion sort is a simple sorting algorithm: a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:
  • Simple implementation
  • Efficient for (quite) small data sets
  • Adaptive (i.e., efficient) for data sets that are already substantially sorted: the time complexity is O(n + d), where d is the number of inversions
  • More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort; the best case (nearly sorted input) is O(n)
  • Stable; i.e., does not change the relative order of elements with equal keys
  • In-place; i.e., only requires a constant amount O(1) of additional memory space
  • Online; i.e., can sort a list as it receives it 


  • Shell Sort
  • Shellsort, also known as Shell sort or Shell's method 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.[citation needed] Its first version was published by Donald Shell in 1959.[1][2] The running time of Shellsort is heavily dependent on the gap sequence it uses. For many practical variants, determining their time complexity remains an open problem.
  •  
  • Bucket Sort
  • 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 distribution sort, and is a cousin of radix sort in the most to least significant digit flavour. Bucket sort is a generalization of pigeonhole sort. Since bucket sort is not a comparison sort, the O(n log n) lower bound is inapplicable. The computational complexity estimates involve the number of buckets.
    Bucket sort works as follows:
  • Set up an array of initially empty "buckets."
  • Scatter: Go over the original array, putting each object in its bucket.
  • Sort each non-empty bucket.
  • Gather: Visit the buckets in order and put all elements back into the original array.
  •  
  • Merge Sort
  • 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 is a divide and conquer algorithm that was invented by John von Neumann in 1945.[1] A detailed description and analysis of bottom-up mergesort appeared in a report by Goldstine and Neumann as early as 1948.[2]

     
  • Quicksort
  •  Quicksort is a sorting algorithm developed by Tony Hoare that, on average, makes O(nlog n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare. Quicksort is often faster in practice than other O(nlog n) algorithms.[1] Additionally, quicksort's sequential and localized memory references work well with a cache. Quicksort can be implemented with an in-place partitioning algorithm, so the entire sort can be done with only O(log n) additional space.[2]