当前位置:网站首页>快速排序
快速排序
2022-04-23 06:27:00 【老少年】
class Program
{
public class Tree
{
public int data;//数据
public Tree left;//左子树
public Tree right;//右子树
public static void Insert(Tree tree, int num)
{
if (tree.data <= num)
{
if (tree.right != null)
{
Insert(tree.right, num);
}
else
{
Tree right = new Tree { data = num };
tree.right = right;
}
}
else
{
if (tree.left != null)
{
Insert(tree.left, num);
}
else
{
Tree left = new Tree { data = num };
tree.left = left;
}
}
}
//树的遍历
public static void Travel(Tree tree)
{
if (tree.left != null)
Travel(tree.left);
Console.Write(tree.data + " ");
if (tree.right != null)
Travel(tree.right);
}
}
public static Tree tree;
public static void sort(int[] a)
{
tree = new Tree { data = a[0] };
for (int i = 1; i < a.Length; i++)
{
Tree.Insert(tree, a[i]);
}
}
static void Main(string[] args)
{
//string str= Console.ReadLine();
//string[] array = str.Split(',');
//List<int> intArray = new List<int>();
//foreach (string i in array)
//{
// intArray.Add(Convert.ToInt32(i));
//}
int[] a = { 3, 5, 3, 6, 4, 7, 5, 7, 4, 1, 15, 8, 10, 9 };
Console.WriteLine("原始数据");
foreach (var item in a)
{
Console.Write(item + " ");
}
Console.WriteLine();
Console.WriteLine("排序后数据");
sort(a);
Tree.Travel(tree);
Console.ReadLine();
}
private static int DivisionLeft(List<int> list,int left,int right)
{
while (left<right)
{
int num = list[left];//中轴
if(num>list[left+1]){
list[left] = list[left + 1];
list[left + 1] = num;
left++;
}
else
{
int temp = list[left+1];
list[left+1] = list[right];
list[right] = temp;
right--;
}
}
return left;
}
//获取按枢轴值左右分流后枢轴的位置
private static int Division(List<int> list, int left, int right)
{
while (left < right)
{
int num = list[left]; //将首元素作为枢轴
if (num > list[left + 1])
{
list[left] = list[left + 1];
list[left + 1] = num;
left++;
}
else
{
int temp = list[right];
list[right] = list[left + 1];
list[left + 1] = temp;
right--;
}
Console.WriteLine(string.Join(",", list));
}
Console.WriteLine("--------------\n");
return left; //指向的此时枢轴的位置
}
}
版权声明
本文为[老少年]所创,转载请带上原文链接,感谢
https://blog.csdn.net/laoshaonian/article/details/107131521
边栏推荐
猜你喜欢

redis连接出错 ERR AUTH <password> called without any password configured for the default user.

SAP Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。

SAP pi / PO rfc2restful publishing RFC interface is a restful example (proxy indirect method)

Redis connection error err auth < password > called without any password configured for the default user

BTree、B+Tree和HASH索引

SAP PI/PO rfc2RESTful 发布rfc接口为RESTful示例(Proxy间接法)

利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)

SAP PI/PO功能运行状态监控检查

Ogldev reading notes

Custom time format (yyyy-mm-dd HH: mm: SS week x)
随机推荐
SAP PI/PO Soap2Proxy 消费外部ws示例
js之函数的两种声明方式
将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
9. Common functions
h5本地存储数据sessionStorage、localStorage
系统与软件安全研究(五)
利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
Ogldev reading notes
js之DOM事件
Page dynamic display time (upgraded version)
常用的DOS命令
给定区段范围内字符串自生成代码
Date对象(js内置对象)
js之DOM学习获取元素
c#读取INI文件和向ini文件写入数据
游戏辅助脚本开发之旅
Reflection on the systematic design of Android audio and video caching mechanism
3. Sort statement
配置npm
State synchronization and frame synchronization