Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Linked List Bubble Sort Not Giving Correct Output In C#

I have made this program but it is not giving correct output..

C#
using System;
using System.Collections.Generic;
using System.Linq;

namespace BubbleSortLinkedList
{
    class Bubble
    {
        int i, j;
        LinkedList<string> x = new LinkedList<string>();

        public Bubble()
        {
            i = 0;
            j = 0;
        }

        static void Main(string[] args)
        {
            Bubble ob = new Bubble();
            
            int n = 10;

            ob.x.AddLast("98");
            ob.x.AddLast("34");
            ob.x.AddLast("77");
            ob.x.AddLast("24");
            ob.x.AddLast("54");
            ob.x.AddLast("50");
            ob.x.AddLast("10");
            ob.x.AddLast("6");
            ob.x.AddLast("1");
            ob.x.AddLast("83");


            ob.bubblesort(ob.x, n);
            Console.Read();
        }

        void bubblesort(LinkedList<string> a, int s)
        {
            for (i = 0; i < s; i++)
            {
                for (j = 0; j < s - 1; j++)
                {
                    if (a.ElementAt(j).CompareTo(a.ElementAt(j + 1)) > 0)
                    {
                        LinkedListNode<string> current = a.Find(a.ElementAt(j));
                        var temp = current.Next.Value;
                        current.Next.Value = current.Value;
                        current.Value = temp;
                    }
                }
            }
            Console.WriteLine("\n-----------------------");
            Console.WriteLine("Sorted Linked List In Ascending Order Is=====>");
            Console.WriteLine("-----------------------");
            i = 1;
            foreach (var item in a)
            {
                Console.Write("<" + (i++) + "> ");
                Console.WriteLine(item);
            }
        }
    }
}


I am getting this output:-

-----------------------
Sorted Linked List In Ascending Order Is=====>
-----------------------
<1> 1
<2> 10
<3> 24
<4> 34
<5> 50
<6> 54
<7> 6
<8> 77
<9> 83
<10> 98


please tell what is wrong with the code and kindly help

What I have tried:

i have tried this program but not getting the desired output..help pls
Posted
Updated 23-Feb-16 4:41am
Comments
Sergey Alexandrovich Kryukov 23-Feb-16 10:54am    
If you want to deal with numbers, why working with strings representing numbers in first place?
—SA
Palash Sachan 23-Feb-16 10:57am    
dear sir, i trying to make a program which can sort numbers and strings both because i don't know what user will input
i am getting this wrong output
For Example:-
Please enter how many names of the student you want to arrange

-----------------------
Enter 10 Names Of Students
-----------------------
<1> palash
<2> 10
<3> 100
<4> 8
<5> 40
<6> sachan
<7> good
<8> boy
<9> 6
<10> 3

-----------------------
Sorted Linked List In Ascending Order Is=====>
-----------------------
<1> 10
<2> 100
<3> 3
<4> 40
<5> 6
<6> 8
<7> boy
<8> good
<9> palash
<10> sachan
Patrice T 23-Feb-16 11:10am    
The second list should be what your program is already doing.
If you want another order, please say what you want.
Palash Sachan 23-Feb-16 11:03am    
please help any

The output is correct

Your mistake is that you sort a list of strings, not a list of numbers.
so "6" fall between "54" and "77"
a sort on strings is alphabetical.
 
Share this answer
 
Comments
Palash Sachan 23-Feb-16 10:37am    
ohh..this little problem i had with this..thanks sir
can u tell me should i have to make another program to sort numbers also??
i cannot sort it with the strings??
Sergey Alexandrovich Kryukov 23-Feb-16 10:52am    
Ha-ha,.. 5ed.
—SA
Patrice T 23-Feb-16 11:05am    
Thank you.
BillWoodruff 23-Feb-16 11:18am    
+5
Patrice T 23-Feb-16 12:41pm    
Thank you
This will convert the string to a number when doing the comparison;

C#
if (int.Parse(a.ElementAt(j)).CompareTo(int.Parse(a.ElementAt(j + 1))) > 0)


However it will error if you add somenone non-numeric to the list. For a number sort you're best using

C#
LinkedList<int> x = new LinkedList<int>();


and changing the rest of the code as appropriate.

C#
class Bubble
    {

        int i, j;
        LinkedList<int> x = new LinkedList<int>();

        public Bubble()
        {
            i = 0;
            j = 0;
        }

        static void Main(string[] args)
        {
            Bubble ob = new Bubble();
            
            int n = 10;
 
            ob.x.AddLast(98);
            ob.x.AddLast(34);
            ob.x.AddLast(77);
            ob.x.AddLast(24);
            ob.x.AddLast(54);
            ob.x.AddLast(50);
            ob.x.AddLast(10);
            ob.x.AddLast(6);
            ob.x.AddLast(1);
            ob.x.AddLast(83);
 

            ob.bubblesort(ob.x, n);
            Console.Read();

        }

        void bubblesort(LinkedList<int> a, int s)
        {
            for (i = 0; i < s; i++)
            {
                for (j = 0; j < s - 1; j++)
                {
                    if (a.ElementAt(j).CompareTo(a.ElementAt(j + 1)) > 0)
                    {
                        LinkedListNode<int> current = a.Find(a.ElementAt(j));
                        var temp = current.Next.Value;
                        current.Next.Value = current.Value;
                        current.Value = temp;
                    }
                }
            }
            Console.WriteLine("\n-----------------------");
            Console.WriteLine("Sorted Linked List In Ascending Order Is=====>");
            Console.WriteLine("-----------------------");
            i = 1;
            foreach (var item in a)
            {
                Console.Write("<" + (i++) + "> ");
                Console.WriteLine(item);
            }
        }
    }
 
Share this answer
 
v3
Comments
Palash Sachan 23-Feb-16 10:43am    
sir sorting the integer linked list also giving wrong output
see this:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SortAndMergeLinkedList
{
class SortAndMerge
{
int i, j;
LinkedList<int> List1 = new LinkedList<int>();
LinkedList<int> List2 = new LinkedList<int>();
LinkedList<int> List3 = new LinkedList<int>();
Random rand = new Random();

public SortAndMerge()
{
i = 0;
j = 0;
}
static void Main(string[] args)
{
SortAndMerge ob = new SortAndMerge();
ob.InputList1();
ob.InputList2();
ob.Merge();
ob.Sort();
ob.Display();
Console.Read();
}

void InputList1()
{
Console.WriteLine("\n-----------------------");
Console.WriteLine("\tList 1");
Console.WriteLine("-----------------------");

int n = 10;
Console.WriteLine("\n-----------------------");
Console.WriteLine("Enter {0} Names Of Students ", n);
Console.WriteLine("-----------------------");

for (int i = 0; i < n; i++)
{
Console.Write("<" + (i + 1) + "> ");
List1.AddLast(rand.Next(1,100));
}
}

void InputList2()
{
Console.WriteLine("\n-----------------------");
Console.WriteLine("\tList 2");
Console.WriteLine("-----------------------");

int n = 10;
Console.WriteLine("\n-----------------------");
Console.WriteLine("Enter {0} Names Of Students ", n);
Console.WriteLine("-----------------------");

for (int i = 0; i < n; i++)
{
Console.Write("<" + (i + 1) + "> ");
List2.AddLast(rand.Next(1,100));
}
}

void Merge()
{
foreach(var item in List1)
{
List3.AddLast(item);
}

foreach(var item in List2)
{
List3.AddLast(item);
}
}

void Sort()
{
for (i = 0; i < List3.Count; i++)
{
for (j = 0; j < List3.Count - 1; j++)
{
if (List3.ElementAt(j) > List3.ElementAt(j + 1))
{
LinkedListNode<int> current = List3.Find(List3.ElementAt(j));
var temp = current.Next.Value;
current.Next.Value = current.Value;
current.Value = temp;
}
}
}
}

void Display()
{
Console.WriteLine("\n-----------------------");
Console.WriteLine("Sorted Linked List In Ascending Order Is=====>");
Console.WriteLine("-----------------------");
i = 1;
foreach (var item in List3)
{
Console.Write("<" + (i++) + "> ");
Console.WriteLine(item);
}
}

}
}
F-ES Sitecore 23-Feb-16 10:48am    
I've updated my solution with an int-based version.
Palash Sachan 23-Feb-16 10:50am    
what is this..pls explain

static DataSet FetchEmailDetails(IDetailsRepository bl)
{
DataSet ds = bl.GetData();
return ds;
}
F-ES Sitecore 23-Feb-16 11:02am    
Sorry, that's just some code from something else I forgot to delete :o
Palash Sachan 23-Feb-16 11:06am    
ok sir..no problem..can u pls help me in this
i trying to make a program which can sort numbers and strings both because i don't know what user will input
i am getting this wrong output
For Example:-
Please enter how many names of the student you want to arrange

-----------------------
Enter 10 Names Of Students
-----------------------
<1> palash
<2> 10
<3> 100
<4> 8
<5> 40
<6> sachan
<7> good
<8> boy
<9> 6
<10> 3

-----------------------
Sorted Linked List In Ascending Order Is=====>
-----------------------
<1> 10
<2> 100
<3> 3
<4> 40
<5> 6
<6> 8
<7> boy
<8> good
<9> palash
<10> sachan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900