Bubble Sort

From Wikipedia

Bubble 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.

[ad code=1]

[ad code=1]

Algorithm

  1. Iterates thru every element of the array, starting with the first 2 elements.
  2. If left element is bigger than right element, swap them.
  3. Repeat step #1 and #2 until there are no more swaps.

[ad code=1]

[ad code=1]

Block Diagram

[ad code=1]

[ad code=1]

Codes


[ad code=1]
[ad code=1]

Perl

#!/apps/perl/bin/perl -w

&main();

sub main
{
   my @array = (1,7,4,9,4,7,2,3,0,8);

   print "Before Sort:-\n";
   print "@array\n";
   &bubbleSort(\@array,);
   print "After Sort:-\n";
   print "@array\n";
} # main

sub bubbleSort
{
   my $aref = shift(@_);
   my $swapHappened = 1;

   while ($swapHappened)
   {
      $swapHappened = 0;
      for (my $i=0; $i<@$aref-1; $i++)
      {
         if ($aref->[$i] > $aref->[$i+1])
         {
            &swap( \$aref->[$i], \$aref->[$i+1] );
            $swapHappened = 1;
         } # if
      } # for
   } # while

} # selectionSort

sub swap
{
   my ($x, $y) = @_;
   my $tmp = $$x;
   $$x = $$y;
   $$y = $tmp;
} # swap

[ad code=1]
[ad code=1]

Python

#!/swdev/tools/python/2.5.1/linux32/bin/python -d

def main():
   array = [1, 7, 4, 9, 4, 7, 2, 3, 0, 8]
   print("Before sort:-")
   print(array)
   bubbleSort(array)
   print("After sort:-")
   print(array)

def bubbleSort(array):
   swapHappened = True

   while swapHappened:
      swapHappened = False

      for x in range(0, len(array)-1):
         if array[x] > array[x+1]:
            # Swap data
            array[x], array[x+1] = array[x+1], array[x]
            swapHappened = True

main()

[ad code=1]
[ad code=1]

C++

#include <iostream>
using namespace std;

// prototype
void bubbleSort(int * const, const int);
void swap(int * const, int * const);
void printArray(const int * const, const int);

// --------------------------------------------
int main()
{
   int size = 10;
   int array[] = {1,7,4,9,4,7,2,3,0,8};

   cout << "Before sort:-" << endl;
   printArray(array, size);
   bubbleSort(array, size);
   cout << "After sort:-" << endl;
   printArray(array, size);

   return 0;
} // main

void bubbleSort(int * const ap, const int size)
{
   int swapHappened = 1;
   while (swapHappened)
   {
      swapHappened = 0;
      for (int i=0; i<size-1; i++)
      {
         if (ap[i] > ap[i+1])
         {
            swap(&ap[i], &ap[i+1]);
            swapHappened = 1;
         }
      } // for
   } // while
} // bubbleSort

void swap(int * const x, int * const y)
{
   int tmp;
   tmp = *x;
   *x = *y;
   *y = tmp;
} // swap

void printArray( const int * const array, const int size)
{
   for (int i=0; i<size; i++)
   {
      cout << array[i] << " ";
   } // for
   cout << endl;
} // printArray

[ad code=1]
[ad code=1]

Recommended Reading



Didn't find what you are looking for?

Trying searching it here ...




  3 Responses to “Bubble Sort”

  1. [...] Bubble Sort [...]

  2. [...] This post was mentioned on Twitter by Yamane Toshiaki, TAN YOKE LIANG. TAN YOKE LIANG said: Bubble Sort http://bit.ly/9A5d4N [...]

 Leave a Reply

(required)

(required)


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

   

Visitors Since Nov 2009

© 2012 Lionel's Blog Suffusion theme by Sayontan Sinha