Employers - Post Free Jobs
Forum guidelines
AuthorQuestion
Bubble sort using string array in C#
Posted on: 26 Jan 2012
please help  me to sort the string array in bubble sort method.

Please help me sort [without using Collectons or Array.Sort()] the string array in the following code snippet:


using System;

class MainClass
{
    static void Main()
    {  
        string[] s = {"Bill", "Gates", "James", "Apple", "Net", "Java"};

        try
        {
            for(int i=0; i<s.Length; i++)
            {
                for(int j=0; j<s.Length-1; j++)
                {
                    for(int k=0; k<s.Length; k++)            
                        if(s[j][k] > s[j+1][k])
                        {
                            string temp = s[j];
                            s[j] = s[j+1];
                            s[j+1] = temp;
                        }
               
                }
            }             
        }

        catch(Exception)
        {

        } 

        for(int i=0; i<s.Length; i++ )
        {
            Console.WriteLine(sIdea + " ");
        }               
    }
}




the above code throwing error please help me to sort string array.


AuthorReply
Datta Kharad
  • 0
  • 0
accepted
Re: Bubble sort using string array in C#
Posted on: 27 Jan 2012   Accepted Answer
Hi Rajeswari..

         I have tried your example using string array, i hope it will help you.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Buble_Sort_String
{
class Program
{
static void Main(string[] args)
{
string[] s = { "Bill", "Gates", "James", "Apple", "Net", "Java" };
string temp = string.Empty;
try
{
for (int i = 1; i < s.Length; i++)
{
for (int j = 0; j < s.Length-i; j++)
{
if (s[j].CompareTo(s[j + 1]) > 0)
{
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
for (int i = 0; i < s.Length; i++)
{
Console.WriteLine(s[i] + " ");
}
Console.ReadKey();
}

catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

 

Please mark as Accepted answer if your query resolved.Thanks & RegardsDatta S. Kharad
Re: Bubble sort using string array in C#
Posted on: 27 Jan 2012  
Hi Rajeswari,


Please refer the below link

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=5199&lngWId=2

Thanks

If this post helps you mark it as answer
Thanks

Pavi S
  • 0
  • 0
Re: Bubble sort using string array in C#
Posted on: 27 Jan 2012  
im asking using c#, but thanks i tried but its not working correctly, ii i give input like this

String[] Array={"f","e","g","a"};

it will sort like this
 
String[] Array={"f","e","a","g"};

my code
===========
class
Program{


{

static void Main()
{

String[] Array={"f","e","g","a"};
String temp;
{
if (Array[y].CompareTo(Array[y + 1]) > 0)
{
temp = Array[y];
Array[y] = Array[y + 1];
Array[y + 1] = temp;
}
}

string Arrays = string.Join(",", Array);
Console.ReadKey();
}
}

SPONSORED BY

Offshore Software and Database Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.