链接列表冒泡排序没有在c#中给出正确的输出 [英] Linked list bubble sort not giving correct output in c#
本文介绍了链接列表冒泡排序没有在c#中给出正确的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
链接列表冒泡排序没有在C#中提供正确的输出
我已经制作了这个程序,但它没有提供正确的输出..
Linked List Bubble Sort Not Giving Correct Output In C#
I have made this program but it is not giving correct output..
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
请告诉代码有什么问题,请帮助
我尝试了什么:
i尝试过这个程序,但没有得到想要的输出..help pls
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
推荐答案
输出正确
您的错误是您对字符串列表进行排序,而不是数字列表。
所以6介于54和77之间
a排序字符串是按字母顺序排列的。
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.
这会在进行比较时将字符串转换为数字;
This will convert the string to a number when doing the comparison;
if (int.Parse(a.ElementAt(j)).CompareTo(int.Parse(a.ElementAt(j + 1))) > 0)
但是如果你将一些非数字加到t中会出错他列出了。对于数字排序,你最好使用
However it will error if you add somenone non-numeric to the list. For a number sort you're best using
LinkedList<int> x = new LinkedList<int>();
并更改其余代码视情况而定。
and changing the rest of the code as appropriate.
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);
}
}
}
这篇关于链接列表冒泡排序没有在c#中给出正确的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文