Pages

Wednesday, July 11, 2012

Bubble Sort


Simplest sorting algorithm which is frequently used is Bubble sort. The bubble sort works on a fairly simple algorithm where items are sort by iterating down an array and sorted with the first element to the last, here we compare each set of value and then switch their positions if required. This same method is continue until the array is sorted. The maximum iteration can occur in a case where the array is in reverse order, where the first value needs to be swapped with the last value. In this case the maximum iteration required to sort the complete array will be same as the array length.
Here is a Simple demonstration of the code in C# where the for loop is used to perform the iteration and a temporary array is used to store the sorted values. 

The algorithm works on the principle of swapping the value by comparing them, here is a pictorail demonstration of the task. This will give a clear idea for the whole process.


 Here is the Implementation using the C# code

namespace BubbleSort
{
    class Program
    {
        static void Main(string[] args)
        {

            int i = 0, j = 0, t = 0;
            int[] unSortedArray = new int[20];
            for (i = 0; i < 10; i++)
            {
                Console.WriteLine("Enter Value p[{0}]:", i);
                unSortedArray[i] = int.Parse(Console.ReadLine());
            }
            // Sorting Algorithm  Starts here - Bubble Sort
            for (i = 0; i < 10; i++)
            {
                for (j = i + 1; j < 10; j++)
                {
                    if (unSortedArray[i] > unSortedArray[j])
                    {                       
                        t = unSortedArray[i];
                        unSortedArray[i] = unSortedArray[j];
                        unSortedArray[j] = t;
                    }
                }
            }
            Console.WriteLine("\n The sorted array:\n");

            for (i = 0; i < 10; i++)
            {
                Console.WriteLine("Sorted Array[{0}]={1}", i, unSortedArray[i]);
            }

            Console.WriteLine("n Please Press a Key to Exit");
            Console.ReadLine();
        }
    }
}


Hope this piece of code is very useful for you in creating your sort functionality. Please provide the valuable comment by which I can improve the performance and code structure.


Thanks
Anil Kumar Pandey
Microsoft MVP, Microsoft MCC,DNS MVM

4 comments:

  1. Code is not getting compiled properly, please check code

    There is undefined variable 'c' used in code
    the unassigned variable 'j' is used

    ReplyDelete
    Replies
    1. Sorry Guys..

      By Mistake Wrong code was copied here. I have updated the code. Thanks a lot for raising this issue. Now You can try with this One..

      Thanks
      Anil Pandey

      Delete
  2. Even if above compilation error is corrected still logic does not work

    ReplyDelete
    Replies
    1. Sorry Guys..

      By Mistake Wrong code was copied here. I have updated the code. Thanks a lot for raising this issue. Now You can try with this One..

      Thanks
      Anil Pandey

      Delete

Kontera