当前位置:网站首页>快速排序
快速排序
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
边栏推荐
- 公共依赖模块common的处理
- ‘npm‘不是内部或外部命令,也不是可运行的程序 或批处理文件
- Reflect on the limitations of event bus and the design and implementation of communication mechanism in component development process
- 13. User and authority management
- 系统与软件安全研究(三)
- 12. Constraints
- int a = 1存放在哪
- 'NPM' is not an internal or external command, nor is it a runnable program or batch file
- FSM finite state machine
- keytool: command not found
猜你喜欢

SAP PI/PO登录使用及基本功能简介

Django使用mysql数据库报错解决

Authorization server (simple construction of authorization server)

SAP pi / PO rfc2soap publishes RFC interface as WS example

如何判断点是否在多边形内(包含复杂多边形或者多边形数量很多的情况)

Nacos / sentinel gateway current limiting and grouping (code)

Implementation of MySQL persistence

Date object (JS built-in object)

SAP PI/PO rfc2Soap 发布rfc接口为ws示例

redis连接出错 ERR AUTH <password> called without any password configured for the default user.
随机推荐
js之排他思想及案例
SAP PI/PO rfc2RESTful 發布rfc接口為RESTful示例(Proxy間接法)
SAP PI/PO功能运行状态监控检查
Super classic & Programming Guide (red and blue book) - Reading Notes
Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods
ABAP 实现发布RESTful服务供外部调用示例
公共依赖模块common的处理
ABAP CDS VIEW WITH ASSOCIATION示例
超级宝典&编程指南(红蓝宝书)-读书笔记
int a = 1存放在哪
2022.3.14 Ali written examination
js之DOM事件
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
Thorough inquiry -- understanding and analysis of cocos2d source code
手游的热更方案与动态更新策略
[self motivation series] you'll never be ready
King glory - unity learning journey
颜色转换公式大全及转换表格(31种)
王者荣耀-unity学习之旅
Authorization+Token+JWT