class Program
{
static void Main(string[] args)
{
int[] a = new int[] { 34, 25, 634, 234, 32, 9, 42 };
RadixSort(a);
foreach (int n in a)
{
Console.WriteLine(n);
}
Console.ReadKey();
}
/// <summary>
/// 1.插入排序(按順序遍歷數(shù)組中的數(shù)逛艰,把他和前面的數(shù)比較灾杰,如果比他大就放到他前面洋措,直到遇到比他小的數(shù))
/// </summary>
public static void InsertSort(int[] array)
{
for (int i = 1; i < array.Length; i++)
{
if (array[i] < array[i - 1])
{
int temp = array[i];
int j;
for (j = i - 1; j >= 0 && temp < array[j]; j--)
{
array[j + 1] = array[j];
}
array[j + 1] = temp;
}
}
}
/// <summary>
/// 2.冒泡排序(每次循環(huán)把最大的數(shù)放到最后一個(gè))
/// </summary>
public static void BubbleSort(int[] array)
{
for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < array.Length - 1 - i; j++)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
/// <summary>
/// 3.快速排序(以中間數(shù)為基準(zhǔn),比他小的放左邊镐躲,比他大的放右邊,在對(duì)左邊右邊遞歸)
/// </summary>
public static void QuickSort(int[] array, int left, int right)
{
if (left < right)
{
int middle = array[(left + right) / 2];
int i = left - 1;
int j = right + 1;
while (true)
{
while (array[++i] < middle && i < right) ;
while (array[--j] > middle && j > left) ;
if (i >= j)
break;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
QuickSort(array, left, i - 1);//遞歸
QuickSort(array, j + 1, right);
}
}
/// <summary>
/// 4.希爾排序(分組的插入排序)
/// </summary>
public static void ShellSort(int[] array)
{
int length = array.Length;
for (int h = length / 2; h > 0; h = h / 2)
{
for (int i = h; i < length; i++)
{
int temp = array[i];
if (temp < array[i - h])
{
for (int j = 0; j < i; j += h)
{
if (temp < array[j])
{
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
}
}
}
/// <summary>
/// 5.簡單選擇排序(選出最小的數(shù)放在依次放在數(shù)組中)
/// </summary>
public static void SimpleSelectSort(int[] array)
{
int tmp = 0;
int t = 0;
for (int i = 0; i < array.Length; i++)
{
t = i;
for (int j = i + 1; j < array.Length; j++)
{
if (array[t] > array[j])
{
t = j;
}
}
tmp = array[i];
array[i] = array[t];
array[t] = tmp;
}
}
/// <summary>
/// 6.堆排序(排序把最大的數(shù)移到堆首,堆首堆尾交換暂筝,重新排序)
/// </summary>
public static void HeapSort(int[] array, int top)
{
List<int> topNode = new List<int>();
for (int i = array.Length / 2 - 1; i >= 0; i--)
{
HeapAdjust(array, i, array.Length);
}
for (int i = array.Length - 1; i >= array.Length - top; i--)
{
int temp = array[0];
array[0] = array[i];
array[i] = temp;
HeapAdjust(array, 0, i);
}
}
/// <summary>
/// 構(gòu)建堆(大根堆,每次根節(jié)點(diǎn)和子節(jié)點(diǎn)交換唆途,該子節(jié)點(diǎn)的大根堆結(jié)構(gòu)會(huì)發(fā)生變化)
/// </summary>
private static void HeapAdjust(int[] array, int parent, int length)
{
int temp = array[parent];
int child = 2 * parent + 1;
while (child < length)
{
if (child + 1 < length && array[child] < array[child + 1])
{
child++;
}
if (temp >= array[child])
{
break;
}
array[parent] = array[child];
parent = child;
child = 2 * parent + 1;
}
array[parent] = temp;
}
/// <summary>
/// 7.歸并排序(遞歸二分成最小樹富雅,比較左子樹和右子樹,較小的加入數(shù)組)
/// </summary>
public static void MergeSort(int[] array, int first, int last)
{
if (first + 1 < last)
{
int mid = (first + last) / 2;
MergeSort(array, first, mid);
MergeSort(array, mid, last);
Merger(array, first, mid, last);
}
}
/// <summary>
/// 歸并
/// </summary>
private static void Merger(int[] array, int first, int mid, int last)
{
Queue<int> tempV = new Queue<int>();
int indexA, indexB;
indexA = first;
indexB = mid;
while (indexA < mid && indexB < last)
{
if (array[indexA] < array[indexB])
{
tempV.Enqueue(array[indexA]);
indexA++;
}
else
{
tempV.Enqueue(array[indexB]);
indexB++;
}
}
while (indexA < mid)
{
tempV.Enqueue(array[indexA]);
indexA++;
}
while (indexB < last)
{
tempV.Enqueue(array[indexB]);
indexB++;
}
int index = 0;
while (tempV.Count > 0)
{
array[first + index] = tempV.Dequeue();
index++;
}
}
/// <summary>
/// 8.基數(shù)排序(個(gè)位余數(shù)篩選肛搬,十位余數(shù)篩選没佑。。)
/// </summary>
public static void RadixSort(int[] array, int array_x = 10, int array_y = 100)
{
for (int i = 0; i < array_x; i++)
{
int[,] bucket = new int[array_x, array_y];
foreach (var item in array)
{
int temp = (item / (int)Math.Pow(10, i)) % 10;
for (int j = 0; j < array_y; j++)
{
if (bucket[temp, j] == 0)
{
bucket[temp, j] = item;
break;
}
}
}
for (int o = 0, x = 0; x < array_x; x++)
{
for (int y = 0; y < array_y; y++)
{
if (bucket[x, y] == 0) continue;
array[o++] = bucket[x, y];
}
}
}
}```
C#實(shí)現(xiàn)8中排序算法
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門枉氮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來志衍,“玉大人暖庄,你說我怎么就攤上這事÷シ荆” “怎么了培廓?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長春叫。 經(jīng)常有香客問我肩钠,道長,這世上最難降的妖魔是什么象缀? 我笑而不...
- 正文 為了忘掉前任蔬将,我火速辦了婚禮,結(jié)果婚禮上央星,老公的妹妹穿的比我還像新娘霞怀。我一直安慰自己,他們只是感情好莉给,可當(dāng)我...
- 文/花漫 我一把揭開白布毙石。 她就那樣靜靜地躺著,像睡著了一般颓遏。 火紅的嫁衣襯著肌膚如雪徐矩。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼豫尽,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了顷帖?” 一聲冷哼從身側(cè)響起美旧,我...
- 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎贬墩,沒想到半個(gè)月后榴嗅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡震糖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年录肯,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吊说。...
- 正文 年R本政府宣布,位于F島的核電站养涮,受9級(jí)特大地震影響葵硕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜贯吓,卻給世界環(huán)境...
- 文/蒙蒙 一懈凹、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧悄谐,春花似錦介评、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至情屹,卻和暖如春坪仇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背垃你。 一陣腳步聲響...
- 正文 我出身青樓雾袱,卻偏偏與公主長得像,于是被迫代替她去往敵國和親官还。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 選擇排序 對(duì)于任何輸入,時(shí)間為O(n*n)勾习; 冒泡排序 最優(yōu)(對(duì)于升序的數(shù)組浓瞪,因?yàn)榧尤肓艘粋€(gè)跳出判斷):O(n),...
- 排序算法 冒泡排序 選擇排序 冒泡排序和選擇排序的核心思路: 冒泡排序是:相鄰兩個(gè)元素兩兩進(jìn)行比較巧婶,小則交換位置乾颁。...
- 本文的算法實(shí)現(xiàn)主要參考兩本書:《算法導(dǎo)論》《大話數(shù)據(jù)結(jié)構(gòu)》 接口 測(cè)試代碼 注意 NRE表示非遞歸版本湿右,RE表示遞...
- 雷.達(dá)里奧(Ray Dalio)的《原則:生活和工作》最初源于李笑來老師的推薦诅妹,搜索卻只有英文版,閱讀較慢诅需。十一期...