Wednesday, April 2, 2014

Technique: Bubble Sort Animation

    Bubble sort step by step animation, for the programming beginners to get a better understanding of bubble sort algorithm.(Click snail button to start animation and click it again to go back to the first page)



Cost: O(n^2)
Worst case: O(n^2)
Best case: O(n)

Bubble sort code in C++:

void BubbleSort(int a[],int n)
{
    for(int i=1; i<n; i++
    {
                  for(int j=0; j<n-i; j++)
                  {
                      if(a[j] > a[j+1])
                       {
                         int temp;
                               temp = a[j];
                               a[j] = a[j+1];
                                    a[j+1] = temp;
                      }
                  }
    }
}

int main()
{
    int a[]={25,49,8,25,21,16,32};
    BubbleSort(a,7);

}


More technique animations:

Heap Sort
Binary Search
Merge Sort
Quick Sort
Selection Sort