当前位置:网站首页>Quick sort
Quick sort
2022-04-23 07:47:00 【youth who behaves like an adult】
class Program
{
public class Tree
{
public int data;// data
public Tree left;// The left subtree
public Tree right;// Right subtree
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;
}
}
}
// Tree traversal
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(" Raw data ");
foreach (var item in a)
{
Console.Write(item + " ");
}
Console.WriteLine();
Console.WriteLine(" Data after sorting ");
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];// Central axis
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;
}
// Gets the position of the pivot after shunting left and right according to the pivot value
private static int Division(List<int> list, int left, int right)
{
while (left < right)
{
int num = list[left]; // Use the first element as the pivot
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; // Point to the position of the pivot at this time
}
}
版权声明
本文为[youth who behaves like an adult]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230626190882.html
边栏推荐
- Mongodb 启动警告信息处理
- C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)
- js之节点操作,为什么要学习节点操作
- unity 屏幕自适应
- js之DOM学习三种创建元素的方式
- Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods
- UnityShader基础
- electron-builder打包报错:proxyconnect tcp: dial tcp :0: connectex
- ABAP CDS VIEW WITH ASSOCIATION示例
- RGB颜色转HEX进制与单位换算
猜你喜欢
SAP PI/PO rfc2RESTful 发布rfc接口为RESTful示例(Proxy间接法)
H5 local storage data sessionstorage, localstorage
js之DOM事件
基于NLP的软件安全研究(二)
Django uses MySQL database to solve error reporting
SAP SALV14 后台输出SALV数据可直接保存文件,发送Email(带排序、超链接、筛选格式)
Custom time format (yyyy-mm-dd HH: mm: SS week x)
Redis connection error err auth < password > called without any password configured for the default user
对js中argumens的简单理解
ABAP CDS VIEW WITH ASSOCIATION示例
随机推荐
快速排序
【NLP笔记】CRF原理初探
SVG中Path Data数据简化及文件夹所有文件批量导出为图片
中间人环境mitmproxy搭建
2022.3.14 Ali written examination
5. Sql99 standard: internal connection and external connection
Simple random roll call lottery (written under JS)
Dropping Pixels for Adversarial Robustness
设置了body的最大宽度,但是为什么body的背景颜色还铺满整个页面?
异步的学习
.NET 5 的新功能 What‘s new in .NET 5
反转链表练习
SAP RFC_CVI_EI_INBOUND_MAIN BP主数据创建示例(仅演示客户)
移动布局(flex布局、视口标签)
Two threads print odd and even numbers interactively
手游的热更方案与动态更新策略
驼峰命名对像
系统与软件安全研究(二)
保研准备经验贴——18届(2021年)中南计科推免到浙大工院
unity UGUI判断点击在UI上和3D物体上的解决方案