C 中的八種排序

2021-05-22 16:40:22 字數 3698 閱讀 8136

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace c_sharp_sort

;heapsort(test7, 0, 9);                                         //堆排序

foreach (int a in test7)

console.write(a.tostring().padright(4));

console.writeline();

int test6 = ;

radixsort(test6, 0, 9, 2);                                         //基數排序(第4個引數是陣列中最大數的10的最大次瞑)

foreach (int a in test6)

console.write(a.tostring().padright(4));

console.writeline();

int test0 = ;         

insertsort(test0, 10);                                             //插入排序

foreach (int a in test0)

console.write(a.tostring().padright(4));

console.writeline();

int test1 = ;

newinsertsort(test1, 10);                                          //折半插入排序

foreach (int a in test1)

console.write(a.tostring().padright(4));

console.writeline();

int test2 = ;

shellsort(test2, 10);                                             //希爾排序

foreach (int a in test2)

console.write(a.tostring().padright(4));

console.writeline();

int test3 = ;

paopaosort(test3, 10);                                            //氣泡排序

foreach (int a in test3)

console.write(a.tostring().padright(4));

console.writeline();

int test4 = ;

fastsort(test4, 0, 9);                                            //快速排序

foreach (int a in test4)

console.write(a.tostring().padright(4));

console.writeline();

int test5 = ;

selectsort(test5, 10);                                           //選擇排序

foreach (int a in test5)

console.write(a.tostring().padright(4));

console.writeline();

console.read();

}static public void heapsort(int array, int begin, int end)          //堆排序

else if (array[2 * j + 1] < array[2 * j + 2] && array[2 * j + 2] > array[j])

else

break;

}else

break;}}

}for (length = end; length > begin; length--)                              //首尾交換

else if (array[2 * j + 1] < array[2 * j + 2] && array[2 * j + 2] > array[j])

else

break;}}

}}static public void insertsort(int array, int length)   //直接插入排序

else if (low == high - 1)

else if (array[j] < temp)

else if (array[j] > temp)

else

break;                  

}for (int n = i-1 ; n >= j; n--)

array[n + 1] = array[n];

array[j] = temp;}}

static public void shellsort(int array, int length)                 //希爾排序(基於直接插入排序)

}array[k + delta] = temp;

}j -= delta;

if (array[j] < array[i])                              //2組之間首位進行交換排序

}delta /= 2;}}

static public void paopaosort(int array, int length)          //氣泡排序

}j--;}}

static public void fastsort(int array, int begin,int end)         //快速排序

while (temp > array[left] && right > left)

left++;

if (right > left)

}array[right] = temp;

fastsort(array, right + 1, end);

fastsort(array, begin, right-1);         

}static public void selectsort(int array, int length)        //選擇排序

}temp_array = array[i];

array[i] = array[temp];

array[temp] = temp_array;

i++;

}           

}static public void radixsort(int array, int begin,int last, int pow)     //基數排序

;int x, p = pow, n,i;

while (p >= 0)

sum=sum%10;

switch (sum)

}  //for

x=n=0;

for (i=0;i<10;i++)

}         

p--;}    //while}}

}

C 八種基本排序 選擇排序(2)

選擇排序 原理 從頭至尾掃瞄序列,找出最小的乙個元素,和第乙個元素交換,接著從剩下的元素中繼續這種選擇和交換方式,最終得到乙個有序序列。思路 在長度為n的無序陣列中,第一次遍歷n 1個數,找到最小的數值與第乙個元素交換 第二次遍歷n 2個數,找到最小的數值與第二個元素交換 第n 1次遍歷,找到最小的...

C 八種基本排序 堆排序(6)

知識擴充 完全二叉樹的特點 從作為第一層的根開始,除了最後一層之外,第n層的元素個數都必須是2的n次方 第一層2個元素,第二層4個,第三層8個,以此類推。每一行的元素都從最左邊開始安放 生成的順序是從上往下,從左往右 兩個元素之間不能有空閒 堆的性質 小根堆與大根堆的定義 堆排序原理 堆頂元素 即二...

八種排序演算法

include include 氣泡排序 void boblesort int arr,int n 插入排序 void insertsort2 int arr,int n 希爾排序 void shellsort2 int arr,int n 選擇排序 void selectsort int arr,...