110,571
社区成员
发帖
与我相关
我的任务
分享
using System;
class Sort
{
static void Main()
{
int[] a={3,2,5,4,10,6,8,1};
Console.WriteLine("排序前顺序:\n");
for(int i=0;i<a.Length;i++)
{
Console.Write("{0} ",a[i]);
}
Console.WriteLine();
for(int i=0;i<a.Length-1;i++)
{
for(int j=0;j<a.Length-1-i;j++)
{
int temp;
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
Console.WriteLine("排序后的顺序:\n");
for(int i=0;i<a.Length;i++)
{
Console.Write("{0} ",a[i]);
}
}
}
namespace COM.ZTE.RUCL.PTag.Test.Cmd
{
class Program
{
static void Main(string[] args)
{
#region 构建结构
LinkedList LL;
LinkedList[] Temp = new LinkedList[6];
// 给 LL 赋点值。。。
LL = new LinkedList();
LL.data = 10;
Temp[0] = LL;
LL = new LinkedList();
LL.data = 34;
LL.node = Temp[0];
Temp[1] = LL;
LL = new LinkedList();
LL.data = 6;
LL.node = Temp[1];
Temp[2] = LL;
LL = new LinkedList();
LL.data = 52;
LL.node = Temp[2];
Temp[3] = LL;
LL = new LinkedList();
LL.data = 6;
LL.node = Temp[3];
Temp[4] = LL;
LL = new LinkedList();
LL.data = 7;
LL.node = Temp[4];
Temp[5] = LL;
#endregion
ArrayList list = new ArrayList();
// 构建排序二叉树
for (int i = 0; i < 6; i++)
list.Add(Temp[i]);
// 【重点】排序 【一句话搞定】
list.Sort(new LinkedListSort());
Console.WriteLine("over");
Console.Read();
}
}
/// <summary>
/// 实体类
/// </summary>
public class LinkedList
{
public LinkedList node = null;
public int data;
}
/// <summary>
/// 排序类
/// </summary>
public class LinkedListSort : IComparer
{
#region IComparer Members
public int Compare(object x, object y)
{
LinkedList lX = (LinkedList)x;
LinkedList lY = (LinkedList)y;
if (lX.data > lY.data)
{
return 1;
}
else if (lX.data < lY.data)
{
return -1;
}
return 0;
//throw new NotImplementedException();
}
#endregion
}
}
namespace WindowsFormsApplication34
{
public partial class Form1 : Form
{
public class LinkedList
{
public LinkedList node = null;
public int data;
}
class Tree
{
public Tree Left = null;
public Tree Right = null;
public int Value;
}
LinkedList LL, Head;
Tree T = null;
public Form1()
{
InitializeComponent();
LinkedList Temp = null;
// 给 LL 赋点值。。。
LL = new LinkedList();
LL.data = 10;
Temp = LL;
LL = new LinkedList();
LL.data = 34;
LL.node = Temp;
Temp = LL;
LL = new LinkedList();
LL.data = 6;
LL.node = Temp;
Temp = LL;
LL = new LinkedList();
LL.data = 52;
LL.node = Temp;
Temp = LL;
LL = new LinkedList();
LL.data = 6;
LL.node = Temp;
Temp = LL;
LL = new LinkedList();
LL.data = 7;
LL.node = Temp;
Head = LL;
// 构建排序二叉树
for (LL = Head; LL != null; LL = LL.node)
AddToTree(ref T, LL.data);
// 输出排序结果
String S = String.Empty;
GetResult(ref T, ref S);
MessageBox.Show(S);
}
void AddToTree(ref Tree T, int value)
{
if (T == null)
{
T = new Tree();
T.Value = value;
}
else if (value < T.Value) // 比当前值小于的往左
AddToTree(ref T.Left, value);
else if (value >= T.Value) // 比当前值大于等于的往右
AddToTree(ref T.Right, value);
}
void GetResult(ref Tree T, ref String S)
{
if (T == null)
return; // 已经没有节点了
if (T.Left != null)
GetResult(ref T.Left, ref S); // 向左走
S += T.Value.ToString() + " ";
if (T.Right != null)
GetResult(ref T.Right, ref S); // 向右走
}
}
}