C#快速排序算法實例分析
更新時間:2015年04月27日 11:58:15 作者:lele
這篇文章主要介紹了C#快速排序算法,實例分析了C#排序方法的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了C#快速排序算法。分享給大家供大家參考。具體實現(xiàn)方法如下:
public static int[] QuickSort(int[] arr)
{
if (arr.Length <= 1)
return arr;
int pivot = arr.Length - 1;
int[] less = GetLessThanEqualToPivot(arr, pivot);
int[] greater = GetGreaterThanPivot(arr, pivot);
return Concatenate(QuickSort(less),arr[pivot],QuickSort(greater));
}
public static int[] Concatenate(int[] less,int pivotElement,int[] greater)
{
List<int> _result = new List<int>();
_result.AddRange(less);
_result.Add(pivotElement);
_result.AddRange(greater);
return _result.ToArray();
}
public static int[] GetLessThanEqualToPivot(int[] arr, int pivot)
{
List<int> _result = new List<int>();
for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] <= arr[pivot])
{
_result.Add(arr[i]);
}
}
return _result.ToArray();
}
public static int[] GetGreaterThanPivot(int[] arr, int pivot)
{
List<int> _result = new List<int>();
for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] > arr[pivot])
{
_result.Add(arr[i]);
}
}
return _result.ToArray();
}
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
Unity調(diào)用手機(jī)攝像機(jī)識別二維碼
這篇文章主要為大家詳細(xì)介紹了Unity調(diào)用手機(jī)攝像機(jī)識別二維碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
C#中系統(tǒng)時間和UNIX時間戳互相轉(zhuǎn)換
本文主要介紹C#中系統(tǒng)時間和UNIX時間戳相互轉(zhuǎn)換的方法,大家可以直接拿去用,希望有用。2016-05-05
C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實現(xiàn)
本文主要介紹了C#服務(wù)器NFS共享文件夾搭建與上傳圖片文件的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
C#自動類型轉(zhuǎn)換與強(qiáng)制類型轉(zhuǎn)換的講解
今天小編就為大家分享一篇關(guān)于C#自動類型轉(zhuǎn)換與強(qiáng)制類型轉(zhuǎn)換的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
使用mutex實現(xiàn)應(yīng)用程序單實例運(yùn)行代碼分享
本文主要介紹了使用Mutex實現(xiàn)應(yīng)用程序單實例運(yùn)行的方法,實現(xiàn)原理是在程序啟動時,請求一個互斥體,如果能獲取對指定互斥的訪問權(quán),就繼續(xù)運(yùn)行程序,否則就退出程序2014-01-01

