排序算法之鸡尾酒排序
class Program { static void Main(string[] args) { #region 鸡尾酒排序算法 int[] array = new int[] { 3, 6, 9, 52, 41, 25, 78, 5, 45, 48, 74, 45, 51, 524, 74, 2 };//定义一个一维数组并赋值 Sort(array);//排序 for (int i = 0; i < array.Length; i++)//循环遍历排序后的数组 { Console.Write(array[i]+" ");//逐个输出数组元素 } #endregion Console.ReadKey(); } static int[] intArray; #region 两个数位置变换 /// <summary> /// /// </summary> /// <param name="left"></param> /// <param name="right"></param> static void Change(ref int left, ref int right) { int temp;//临时变量 temp = left;//记录第一个数的值 left = right;//使第一个数的值等于第二个数的值 right = temp;//使第二个数的值等于临时变量的值 } #endregion #region 鸡尾酒排序算法 static void CockSorts(int[] intArray) { int low, up, index;//定义变量 low = 0;//数组的开始索引 up = intArray.Length - 1;//数组的结束索引 index = low;//临时变量 while (up > low)//判断数组中是否存在多个元素 { for (int i = low; i < up; i++)//从上向下扫描 { if (intArray[i] > intArray[i + 1])//比较前后两个数大小 { Change(ref intArray[i], ref intArray[i + 1]);//变换两个数的位置 index = i;//记录当前索引 } } up = index; for (int i = up; i > low; i--)//记录最后一个交换的位置 { if (intArray[i] < intArray[i - 1]) { Change(ref intArray[i], ref intArray[i - 1]);//变换两个数的位置 index = i; } } low = index; } } #endregion #region 对指定数组使用鸡尾酒算法进行排序 static void Sort(int[] intArr) { intArray = intArr;//未数组赋值 CockSorts(intArray);//使用鸡尾酒算法对数组进行排序 } #endregion }
推荐这些技术文章:
import java.util.Stack;
/**
<p>给你一个字符串 <code>s</code> ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 <strong>返回结果的字典序最小</strong>(要求不能打乱其他字符的相对位置)。</p>
<p> </p&...
相当于每次处理两个有序数组的合并操作
/*
-------------------------------------------------
Author: wry
date: 2022/3/1 16:36
Description: MergeSort
----------------------------------------------...
public class MainClass {
public static void main(String[] args)
{
int[] my_array = {1, 2, 5, 5, 6, 6, 7, 2, 9, 2};
findDupicateInArray(my_array);
}
public static...
代码折叠
基于注释实现。
折叠的目标
所有的代码块,方法,class 等,折叠位置可以自定义
如何使用?
//region 开始折叠的位置
boolean auOK =false;
String myName="雷军";
//endregion
// --xml文件折叠的方法
<!--region 开始折叠的位置-->
<dependency>
...
原文链接:十大排序算法总结
在Acwing上看到一位大佬写的,转载一下qwq
排序算法的分类:
插入:插入,折半插入,希尔
交换:冒泡,快速
选择:简单选择,堆
归并:归并(不只二路归并)
基数:
插入排序
void insert_sort()
{
for (int i = 1; i < n; i ++ )
{
int x = a[i];
...
406. 根据身高重建队列
自定义比较函数:
//自定义比较函数
static bool cmp(const vector<int>&a,const vector<int>&b){
if(a[0]==b[0])return a[1]<b[1];
return a[0]>b[0];
}
调...
https://leetcode-cn.com/problems/range-sum-query-mutable/
给你一个数组 nums ,请你完成两类查询。
其中一类查询要求 更新 数组nums下标对应的值
另一类查询要求返回数组nums中索引left和索引right之间(包含)的nums元素的 和,其中left <= right
实现 NumArray 类:
NumArray(int...
文章链接:https://www.dianjilingqu.com/4333.html
本文章来源于网络,版权归原作者所有,如果本站文章侵犯了您的权益,请联系我们删除,联系邮箱:saisai#email.cn,感谢支持理解。