1.没有一种排序算法是万能的最快算法,因为最快的排序算法取决于数据的性质和排序要求。然而,对于一般情况下的排序问题,以下算法通常被认为是最快的:
-
快速排序(Quick Sort):这是一种基于分治思想的常见排序算法。其平均时间复杂度为 O(nlogn)。因为其平均情况下时间复杂度相对较快,加上其实现复杂度相对较低。因此,快速排序通常被认为是最快的排序算法之一。
-
归并排序(Merge Sort):也是一种分治思想的排序算法。它的平均时间复杂度也是 O(nlogn)。虽然它在实际应用中的效率相对较低,但是其具有稳定性,而且能够处理大规模的数据量。
-
堆排序(Heap Sort):建立在堆这种数据结构之上的排序算法。时间复杂度为 O(nlogn)。它具有不错的实际应用性能,同时对于数据有一定规模的情况下,它的效率比快速排序还要高。
需要注意的是,虽然上述算法通常被认为是最快的算法,但是它们各有优劣,适用于不同的应用场景。在实际应用中,我们应该根据具体情况选择不同的算法。
以下是用在unity中
第一种 快速排序 代码如下
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 快速排序 /// </summary> public class Quick_Sort : MonoBehaviour { int[] arr = { 12, 11, 13, 5, 6, 7 }; // Start is called before the first frame update void Start() { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); QuickSort(arr, 0, arr.Length - 1,0); string aA = ""; //数组从小到大排列 foreach (var item in arr) { aA += item + ","; } Debug.Log(aA); //QuickSort(arr, 0, arr.Length - 1, 1); stopwatch.Stop(); print(stopwatch.Elapsed.TotalMilliseconds); } static void QuickSort(int[] arr, int left, int right,int aa) { if (left < right) { int pivotIndex = Partition(arr, left, right,aa); QuickSort(arr, left, pivotIndex - 1,aa); QuickSort(arr, pivotIndex + 1, right,aa); } } static int Partition(int[] arr, int left, int right,int aa) { int pivotValue = arr[right]; int i = left - 1; for (int j = left; j < right; j++) { if (aa==0) //从小到大排列 { if (arr[j] < pivotValue) { i++; Swap(arr, i, j); } } else //从大到小排列 { if (arr[j] > pivotValue) { i++; Swap(arr, i, j); } } } Swap(arr, i + 1, right); return i + 1; } static void Swap(int[] arr, int index1, int index2) { int temp = arr[index1]; arr[index1] = arr[index2]; arr[index2] = temp; } }快速排序代码
第二种 归并排序 代码如下
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 归并排序算法 /// </summary> public class Merge_Sort : MonoBehaviour { int[] arr = { 12, 11, 13, 5, 6, 7 }; private void Start() { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); string aA = ""; //数组从小到大排列 MergeSort(arr, 0, arr.Length - 1,0); //foreach (var item in arr) //{ // aA += item + ","; //} //Debug.Log(aA); //string Aa = ""; ////数组从大到小排列 //MergeSort(arr, 0, arr.Length - 1, 1); //foreach (var item in arr) //{ // Aa += item + ","; //} //Debug.Log(Aa); stopwatch.Stop(); print(stopwatch.Elapsed.TotalMilliseconds); } static void Merge(int[] arr, int left, int mid, int right,int aa) { if (aa == 0) //从小到大排列 { int n1 = mid - left + 1; int n2 = right - mid; int[] L = new int[n1]; int[] R = new int[n2]; for (int i = 0; i < n1; ++i) L[i] = arr[left + i]; for (int j = 0; j < n2; ++j) R[j] = arr[mid + 1 + j]; int k = left; int l = 0, r = 0; while (l < n1 && r < n2) { if (L[l] <= R[r]) { arr[k++] = L[l++]; } else { arr[k++] = R[r++]; } } while (l < n1) { arr[k++] = L[l++]; } while (r < n2) { arr[k++] = R[r++]; } } else //从大到小排列 { int n1 = mid - left + 1; int n2 = right - mid; int[] L = new int[n1 + 1]; int[] R = new int[n2 + 1]; for (int i = 0; i < n1; ++i) L[i] = arr[left + i]; for (int j = 0; j < n2; ++j) R[j] = arr[mid + 1 + j]; L[n1] = int.MinValue; R[n2] = int.MinValue; int k = left; int l = 0, r = 0; while (k <= right) { if (L[l] >= R[r]) { arr[k++] = L[l++]; } else { arr[k++] = R[r++]; } } } } static void MergeSort(int[] arr, int left, int right,int aa) { if (left >= right) { return; } int mid = left + (right - left) / 2; MergeSort(arr, left, mid,aa); MergeSort(arr, mid + 1, right,aa); Merge(arr, left, mid, right,aa); } }归并排序代码
第三种 堆排序 代码如下
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// 堆排序 /// </summary> public class Pile_Sort : MonoBehaviour { int[] arr = { 12, 11, 13, 5, 6, 7 }; // Start is called before the first frame update void Start() { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); //数组从小到大排列 HeapSort(arr,0); //数组从大到小排列 HeapSort(arr, 1); string aA = ""; //数组从小到大排列 //foreach (var item in arr) //{ // aA += item + ","; //} //Debug.Log(aA); //QuickSort(arr, 0, arr.Length - 1, 1); stopwatch.Stop(); print(stopwatch.Elapsed.TotalMilliseconds); //计算时间 } static void HeapSort(int[] arr,int aa) { int n = arr.Length; for (int i = n / 2 - 1; i >= 0; i--) Heapify(arr, n, i,aa); for (int i = n - 1; i >= 0; i--) { Swap(arr, 0, i); Heapify(arr, i, 0,aa); } } static void Heapify(int[] arr, int n, int i,int aa) { int largest = i; int left = 2 * i + 1; int right = 2 * i + 2; if (aa == 0) //从小到大堆排序代码 { if (left < n && arr[left] > arr[largest]) largest = left; if (right < n && arr[right] > arr[largest]) largest = right; } else //从大到小堆排序代码 { if (left < n && arr[left] < arr[largest]) largest = left; if (right < n && arr[right] < arr[largest]) largest = right; } if (largest != i) { Swap(arr, i, largest); Heapify(arr, n, largest,aa); } } static void Swap(int[] arr, int index1, int index2) { int temp = arr[index1]; arr[index1] = arr[index2]; arr[index2] = temp; } }堆排序代码
以后有需要会继续加其他排序,喜欢的话请点个赞吧
标签:aa,arr,right,c#,int,算法,排序,left From: https://www.cnblogs.com/qq2351194611/p/17477753.html