954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

bubble sort

In the code given below m not getting ,
1) wht he has written in "Console.WriteLine("Enter Value p[{0}]:", i);" wht is P[{0}] n wht is i ,,,,,

2)
Console.WriteLine("c[{0}]={1}, c[{2}]={3}", i, c[i], j, c[j]);
wht is c[{0}] = {1},c[{2}]={3} " , i , c[i]

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]);
        }
    }
}
virendra_sharma
Light Poster
45 posts since Jun 2010
Reputation Points: 9
Solved Threads: 0
 

In Console.WriteLine, {0} acts as a placholder for a value to be put in a string.
So Console.WriteLine("Enter Value p[{0}]:", i); (Let i in the for loop be 3) would print out: Enter Value p[ 3 ]:

I would prefer to change this to
Console.WriteLine("Enter Value Number {0} :", i);
which in the same case( i = 3 ) would print out: Enter Value Number 3 :

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: