Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C# (http://www.go4expert.com/articles/c-sharp-tutorials/)
-   -   Bubble Sort in C# (http://www.go4expert.com/articles/bubble-sort-c-sharp-t4461/)

pradeep 29May2007 10:54

Bubble Sort in C#
 
Implementing Bubble Sort in C#

Code: CSharp

/*
 **    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]);
         }
     }
     
 }


sushovan.mukherjee 6Sep2007 02:19

Re: Bubble Sort in C#
 
Boss this code is not working..

clocking 25Sep2007 20:27

Re: Bubble Sort in C#
 
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.

shabbir 26Sep2007 09:21

Re: Bubble Sort in C#
 
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.

ahabeebur 2Jan2008 22:54

Re: Bubble Sort in C#
 
5n

Saveera 2Jul2008 12:09

Re: Bubble Sort in C#
 
The above code is not run,I think you should write the Integer.parseInt at the place of Parse

Saveera 2Jul2008 12:12

Re: Bubble Sort in C#
 
hello

Maledikt 20Dec2008 10:20

Re: Bubble Sort in C#
 
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

danhl 17Apr2009 14:56

Re: Bubble Sort in C#
 
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]);
        }
    }
}


unni krishnan.r 1Jun2010 13:54

Re: Bubble Sort in C#
 
nice


All times are GMT +5.5. The time now is 17:41.