快速排序法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 排序法
{
class Program
{
static void Main(string[] args)
{
int[] arr = {5,8,3,6,7,4,9,1,2 }; //待排序数组
QuickSort(arr, 0, arr.Length - 1); //调用快速排序函数。传值(要排序数组,基准值位置,数组长度)
//控制台遍历输出
Console.WriteLine("排序后的数列:");
foreach (int item in arr)
Console.WriteLine(item);
Console.ReadLine();
}
private static void QuickSort(int[] arr, int begin, int end)
{
if (begin >= end) return; //两个指针重合就返回,结束调用
int pivotIndex = QuickSort_Once(arr, begin, end); //会得到一个基准值下标h
QuickSort(arr, begin, pivotIndex - 1); //对基准的左端进行排序 递归
QuickSort(arr, pivotIndex + 1, end); //对基准的右端进行排序 递归
}
private static int QuickSort_Once(int[] arr, int begin, int end)
{
int pivot = arr[begin]; //将首元素作为基准
int i = begin;
int j = end;
while (i < j)
{
//从右到左,寻找第一个小于基准pivot的元素
while (arr[j] >= pivot && i < j)
{
j--; //指针向前移
}
arr[i] = arr[j]; //执行到此,j已指向从右端起第一个小于基准pivot的元素,执行替换
//从左到右,寻找首个大于基准pivot的元素
while (arr[i] <= pivot && i < j)
{
i++; //指针向后移
}
arr[j] = arr[i]; //执行到此,i已指向从左端起首个大于基准pivot的元素,执行替换
}
//退出while循环,执行至此,必定是 i= j的情况(最后两个指针会碰头)
//i(或j)所指向的既是基准位置,定位该趟的基准并将该基准位置返回
arr[i] = pivot;
return i;
}
}
}
冒泡排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 排序法
{
class Program
{
static void Main(string[] args)
{
int[] arry = { 5, 2, 9, 4, 6, 3, 7, 8, 1 };
for (int i = 0; i < arry.Length-1; i++)
{
for (int j = 0; j < arry.Length-1-i; j++) //一定能把最大的数 放到最后一位
{
if (arry[j] > arry[j + 1]) //只要前一位大于后一位 就调换位置
{
int temp = arry[j];
arry[j] = arry[j + 1];
arry[j + 1] = temp;
}
}
}
foreach (int s in arry)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
}
二分查找法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 二分法
{
class Program
{
static void Main(string[] args)
{
int[] arr = {1,2,3,4,5,6,7,8,9,10 };//测试的数组
int value = BinarySearCh(arr,8);//调用方法
Console.WriteLine(value);
Console.ReadLine();
}
public static int BinarySearCh(int[] arr, int value)//传入数组和需要查找的值
{
int fir = 0;//数组下标第一位
int end = arr.Length - 1;//数组下标最后一位
while (fir <= end)//下标第一位不能大于最后一位
{
int num = (fir + end) / 2;//最中间的索引值
if (value == arr[num])//传入的值是否跟最中间的相等,相等就返回这个最中间索引值
{
return num;
}
else if (value > arr[num])//传入的值大于中间的,相当于需要查找的值在比较大的那一半,所以下一次最小的索引从最中间的数加一开始再次循环
{
fir = num + 1;//
}
else//传入的值小于中间的,相当于需要查找的值在比较小的那一半,所以下一次最大的索引从最中间的数减一开始再次循环
{
end = num - 1;
}
}
return -1;//如果没找到就返回-1
}
}
}
快排例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 快排
{
class Program
{
static void Main(string[] args)
{
/*
需求:
排行榜:(排列顺序)
1.按等级排列
2.战力排列
3.money
*/
RankItem liXiaoYao = new RankItem("李逍遥", 199, 16000, 20000);
RankItem zhaoLingEr = new RankItem("赵灵儿", 199, 17000, 12000);
RankItem tangYuXiaoBao = new RankItem("唐钰小宝", 165, 14300, 50000);
RankItem jiuJianXian = new RankItem("酒剑仙", 200, 20000, 0);
RankItem linYueRu = new RankItem("林月如", 160, 12000, 30000);
List<RankItem> ranks = new List<RankItem>();
ranks.Add(liXiaoYao);
ranks.Add(zhaoLingEr);
ranks.Add(tangYuXiaoBao);
ranks.Add(jiuJianXian);
ranks.Add(linYueRu);
foreach (RankItem value in ranks)
{
Console.WriteLine(value.playerName + " " + value.level + " " +
value.AD + " " + value.gold
);
}
ranks.Sort(CompareTo);//调用下面方法开始进行排序
Console.WriteLine("*****************排序后********************");
foreach (RankItem value in ranks)
{
Console.WriteLine(value.playerName + " " + value.level + " " +
value.AD + " " + value.gold
);
}
Console.ReadKey();
}
/// <summary>
/// 比较规则
/// </summary>
/// <param name="back"></param>
/// <param name="before"></param>
/// <returns>
/// 如果需要交换 返回-1;
/// 不需要交换 返回1;
/// 返回0 交不交换不一定
///
/// </returns>
static int CompareTo(RankItem back, RankItem before)
{
if (back.level > before.level)
{
//判断等级 交换
return -1;
}
else
{
//<=
if (back.level == before.level)
{
//等级相同 判断战力
if (back.AD > before.AD)
{
return -1;
}
else
{
if (back.AD == before.AD)
{
//战力相同判断金币
if (back.gold > before.gold)
{
return -1;
}
}
}
}
}
return 1;
}
}
class RankItem
{
public string playerName;
public int level;
public int AD;
public int gold;
public RankItem(string playerName, int level, int AD, int gold)
{
this.playerName = playerName;
this.level = level;
this.AD = AD;
this.gold = gold;
}
}
}