Bubble Sort in C#

Discussion in 'C#' started by pradeep, May 29, 2007.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Implementing Bubble Sort in C#

    Code:
     /*
     **    Simple class to sort an array in ascending order using bubble sort
     **    @author: Tanaz Kerawala
     **    @author: S Pradeep
     **    @date: 5/29/2007
     */
     
     using System;
     
     class AscendingBubbleSort
     {
         public static void Main()
         {
             int i,j,t;
             int []p=new int[10];
             int []q=new int[10];
             int []c=new int[20];
             for(i=0;i<10;i++)
             {
                 Console.WriteLine("Enter Value");
                 p[i]=int.Parse(Console.ReadLine());
             }
             for(i=0;i<10;i++)
             {
                 Console.WriteLine("Enter Value");
                 q[i]=int.Parse(Console.ReadLine());
             }
     
             if(j<10)
                 c[j]=p[j];
             else
                 c[j]=q[j-10];
             
             // Sorting: Bubble Sort
             for(i=0;i<20;i++)
             {
                 for(j=0;j<20;j++)
                 {
                     if(c[i]>c[j])
                     {
                         t=c[i];
                         c[i]=c[j];
                         c[j]=t;
                     }
                 }
             }
     
             // Print the contents of the sorted array
             for(i=0;i<20;i++)
             {
                 Console.WriteLine (c[i]);
             }
         }
          
     }
     
     
  2. sushovan.mukherjee

    sushovan.mukherjee New Member

    Joined:
    Sep 6, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Kolkata
    Boss this code is not working..
     
  3. clocking

    clocking New Member

    Joined:
    Jun 12, 2007
    Messages:
    122
    Likes Received:
    0
    Trophy Points:
    0
    Hi, My friend.
    Can you show me the way programming the C# program?
    Can you write a code to solve some problem( Please, beginning to the End).
    thanks.
    Best regards.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Yes the above code is not working and following are the compile time errors. j is used without being initialized. Also the sorting does not work if we have the j set as zero.
     
  5. ahabeebur

    ahabeebur Banned

    Joined:
    Nov 22, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
  6. Saveera

    Saveera New Member

    Joined:
    Jul 2, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    The above code is not run,I think you should write the Integer.parseInt at the place of Parse
     
  7. Saveera

    Saveera New Member

    Joined:
    Jul 2, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
  8. Maledikt

    Maledikt New Member

    Joined:
    Dec 19, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I believe the fault is the fact that the '[' character is replaced by "c[" due to some error in the
    Code:
     constructor inside the web-script hosting this forum.
    
    Hoping this is the case and that I'm actually of some help,
    Maledikt
     
  9. danhl

    danhl New Member

    Joined:
    Mar 17, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Here comes updated code.:nice:

    Code:
    using System;  
    class AscendingBubbleSort 
    {     
        public static void Main()
        {
            int i = 0,j = 0,t = 0;
            int []c=new int[20];
            for(i=0;i<20;i++)
            {
                Console.WriteLine("Enter Value p[{0}]:", i);
                c[i]=int.Parse(Console.ReadLine());
            }
            // Sorting: Bubble Sort
            for(i=0;i<20;i++)
            {
                for(j=i+1;j<20;j++)
                {
                    if(c[i]>c[j])
                    {
                        Console.WriteLine("c[{0}]={1}, c[{2}]={3}", i, c[i], j, c[j]);
                        t=c[i];
                        c[i]=c[j];
                        c[j]=t;
                    }
                }
            }
            Console.WriteLine("Here comes the sorted array:");
            // Print the contents of the sorted array
            for(i=0;i<20;i++)
            {
                Console.WriteLine ("c[{0}]={1}", i, c[i]);
            }
        }
    } 
     
  10. unni krishnan.r

    unni krishnan.r Member

    Joined:
    Apr 20, 2010
    Messages:
    209
    Likes Received:
    4
    Trophy Points:
    18
    Occupation:
    education
    Location:
    Kerala
    Home Page:
    http://blogofunni.blogspot.com

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice