冒泡排序法,二分法,拉格朗日法,快速排序法

xiaoxiao2025-04-22  14

十大排序法

冒泡排序法

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main() { Sort(7, 3, 8, 2,9,1); } //params 是C#的关键字, params主要是在声明方法时参数类型或者个数不确定时使用 private static void Sort(params int[] array) { int temp = 0;//定义一个中间变量 for (int i = 0; i < array.Length - 1; i++)//保证每个元素遍历 { for (int j = 0; j < array.Length - 1 - i; j++)//把最大的数换至最后一位并减去这一位的索引 { if (array[j] > array[j + 1])//若前一位大于后一位 { temp = array[j];//前一位赋值给中间变量 array[j] = array[j + 1];//后一位赋值给前一位 array[j + 1] = temp;//前一位的值赋给后一位,即交换位置 } } } foreach (int elements in array) { Console.WriteLine(elements); } Console.ReadKey(); } } }

二分法排序

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 二分法 { class Program { static void Main(string[] args) { int[] a = { 1, 6, 4, 3, 8, 5, 21, 9, 31, 81, 101, 55, 62, 151, 7, 2, 10 }; DichotomySort(a); for (int i = 0; i < a.Length; i++) { Console.WriteLine(a[i]); } Console.Read(); } public static void DichotomySort(int[] array) { for (int i = 0; i < array.Length; i++) { int start = 0; int end = i - 1; int middle = 0; int temp = array[i]; while (start <= end) { middle = (start + end) / 2; if (array[middle] > temp)//要排序元素在已经排过序的数组左边 { end = middle - 1; } else { start = middle + 1; } } for (int j = i - 1; j > end; j--)//找到了要插入的位置,然后将这个位置以后的所有元素向后移动 { array[j + 1] = array[j]; } array[end + 1] = temp; } } } }

二分法查找

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 } } }

拉格朗日插值法

<pre name="code" class="cpp">void search2(int a[M], int num) { int tou = 0; int wei = M - 1; int zhong; int flag = -1;//一开始找不到 int ci = 0; while (tou <= wei) { ci++; //zhong =tou+ ( wei-tou) / 2; zhong = tou + (wei - tou) *1.0*(num - a[tou]) / (a[wei] - a[tou]); printf("%d %d %d %d\n", tou, wei, zhong, ci); if (num == a[zhong]) { printf("一共查找%d次 找到:a[%d]=%d\n", ci, zhong, num); flag = 1;//找到 break; } else if (num > a[zhong]) { tou = zhong + 1; } else { wei = zhong - 1; } } if (-1 == flag) { printf("没有找到\n"); } } //二分查找 拉格朗日插值查找 void main() { int a[M]; for (size_t i = 0; i < M; i++) { a[i] = i; printf("%d ", i); } printf("\n请输入要查找的数据:"); int num; scanf("%d", &num); search2(a, num); system("pause"); }

快速排序法

static void Main(string[] args) { Console.WriteLine("请输入待排序数列(以\",\"分割):"); string _s = Console.ReadLine(); string[] _sArray = _s.Split(",".ToCharArray()); int _nLength = _sArray.Length; int[] _nArray = new int[_nLength]; for (int i = 0; i < _nLength; i++) { _nArray[i] = Convert.ToInt32(_sArray[i]); } var list = _nArray.ToList(); QuickSort(list, 0, _nLength - 1); foreach (var i in list) { Console.WriteLine(i.ToString()); } while (true) { Thread.Sleep(10000); } } //获取按枢轴值左右分流后枢轴的位置 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; //指向的此时枢轴的位置 } private static void QuickSort(List<int> list, int left, int right) { if (left < right) { int i = Division(list, left, right); //对枢轴的左边部分进行排序 QuickSort(list, i + 1, right); //对枢轴的右边部分进行排序 QuickSort(list, left, i - 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) { 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); //会得到一个基准值下标 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[] 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[] array, int low, int high) { if (low > high) { //递归推出条件,非常重要 return; } int first, last, key; first = low; last = high; key = array[first]; while (first < last) { while (first < last && array[last] >= key) //>=表明升序 { --last; } array[first] = array[last]; while (first < last && array[first] <= key) { ++first; } array[last] = array[first]; } array[first] = key; quickSort(array, low, first - 1); quickSort(array, first + 1, high); } } }

冒泡优化,判断是否是最优顺序

static void Sort(int[] array) { int tmp = 0; //记录最后一次交换的位置 int lastExchangeIndex = 0; //无序数列的边界,每次比较只需要比到这里为止 int sortBorder = array.Length - 1; for (int i = 0; i < array.Length; i++) { //有序标记,每一轮的初始值为true bool isStop = true; for (int j = 0; j < sortBorder; j++) { if (array[j]>array[j+1]) { tmp = array[j]; array[j] = array[j + 1]; array[j + 1] = tmp; //有元素交换,所以不是有序,标记变为false isStop= false; //把无序数列的边界更新为最后一次交换元素的位置 lastExchangeIndex = j; } } sortBorder = lastExchangeIndex; if(isStop) { break; } } }```
转载请注明原文地址: https://www.6miu.com/read-5028864.html

最新回复(0)