编写程序实现下述五种算法,并用以下无序序列加以验证: 49,38,65,97,76,13,27,49 1.简单插入排序2.冒泡排序3.快速排序4.归并排序5.堆排序 C语言版 #include <stdio.h> #include <stdlib.h> typedef struct { int *r; int length; } SqList; void InitialList(SqList &L) { //初始化顺序表L L.r = (int *)malloc(10 * sizeof(int)); L.length = 1; L.r[L.length++] = 49; L.r[L.length++] = 38; L.r[L.length++] = 65; L.r[L.length++] = 97; L.r[L.length++] = 76; L.r[L.length++] = 13; L.r[L.length++] = 27; L.r[L.length++] = 49; } void PrintList(SqList L) { //打印顺序表 int i; for (i = 1;i < L.length;i++) printf("%d ", L.r[i]); } void InsertSort(SqList &L) { //直接插入排序 int i, j; for (i = 2; i <= L.length; ++i) { if (L.r[i] < L.r[i - 1]) { L.r[0] = L.r[i]; // 复制为哨兵 for (j = i - 1; L.r[0] < L.r[j]; --j) L.r[j + 1] = L.r[j]; // 记录后移 L.r[j + 1] = L.r[0]; // 插入到正确位置 } } } void BubbleSort(SqList &L) { //冒泡排序 int i, j; int temp; for (i = 1;i < L.length - 1;i++) { for (j = i + 1;j < L.length;j++) { if (L.r[i] > L.r[j]) { temp = L.r[i]; L.r[i] = L.r[j]; L.r[j] = temp; } } } } int Partition(SqList &L, int low, int high) { int pivotkey; L.r[0] = L.r[low]; pivotkey = L.r[low]; while (low < high) { while (low < high && L.r[high] >= pivotkey) --high; L.r[low] = L.r[high]; while (low < high && L.r[low] <= pivotkey) ++low; L.r[high] = L.r[low]; } L.r[low] = L.r[0]; return low; } void QSort(SqList &L, int low, int high) { //快速排序 int pivotloc; if (low < high) { pivotloc = Partition(L, low, high); QSort(L, low, pivotloc - 1); QSort(L, pivotloc + 1, high); } } void Merge (int SR[], int TR[], int i, int m, int n) { int j, k; for (j = m + 1, k = i; i <= m && j <= n; ++k) { if (SR[i] < SR[j]) TR[k] = SR[i++]; else TR[k] = SR[j++]; } if (i <= m) while (k <= n && i <= m) TR[k++] = SR[i++]; if (j <= n) while (k <= n && j <= n) TR[k++] = SR[j++]; } void MSort(int SR[], int TR1[], int s, int t) { //归并排序 int m; int TR2[20]; if (s == t) TR1[t] = SR[s]; else { m = (s + t) / 2; MSort(SR, TR2, s, m); MSort(SR, TR2, m + 1, t); Merge(TR2, TR1, s, m, t); } } void HeapAdjust(SqList &H, int s, int m) { int rc; rc = H.r[s]; for (j = 2 * s; j <= m; j *= 2) { if (j < m && H.r[j] < H.r[j + 1]) ++j; if (rc >= H.r[j]) break; H.r[s] = H.r[j]; s = j; } H.r[s] = rc; } void HeapSort(SqList &H) { //堆排序 int i; int temp; for (i = H.length / 2; i > 0; --i) HeapAdjust ( H, i, H.length ); for (i = H.length; i > 1; --i) { temp = H.r[i]; H.r[i] = H.r[1]; H.r[1] = temp; HeapAdjust(H, 1, i - 1); } } int main() { SqList L; printf("各种排序算法/n"); // printf("/n%-20s", "直接插入排序:"); InitialList(L); InsertSort(L); PrintList(L); // printf("/n%-20s", "冒泡排序:"); InitialList(L); BubbleSort(L); PrintList(L); // printf("/n%-20s", "快速排序:"); InitialList(L); QSort(L, 1, L.length); PrintList(L); // printf("/n%-20s", "归并排序:"); InitialList(L); MSort(L.r, L.r, 1, L.length); PrintList(L); // printf("/n%-20s", "堆排序:"); InitialList(L); HeapSort(L); PrintList(L); getchar(); return 0; } C#语言版 public class Test2 { public void InitialList(ref SqList L) { //初始化顺序表L L.count = 0; L.r = new int[8]; int length = 0; L.r[length++] = 49; L.r[length++] = 38; L.r[length++] = 65; L.r[length++] = 97; L.r[length++] = 76; L.r[length++] = 13; L.r[length++] = 27; L.r[length++] = 49; } public void PrintList(SqList L) { //打印顺序表 int i; for (i = 0; i < L.r.Length; i++) { Console.Write(L.r[i]+" "); if (i == L.r.Length-1) Console.WriteLine(); } Console.WriteLine("运算次数" + L.count.ToString()); } public void InsertSort(ref SqList L) { //直接插入排序 int i, j, temp; for ( i = 1; i < L.r.Length; i++) { if (L.r[i] < L.r[i - 1]) { j = i - 1; temp = L.r[i]; do { L.r[j + 1] = L.r[j]; L.count++; } while (--j >= 0 && temp < L.r[j]); L.r[j + 1] = temp; } L.count++; } } public void BubbleSort(ref SqList L) { //冒泡排序 int i, j; int temp; for (i = 0;i < L.r.Length;i++) { for (j = i + 1;j < L.r.Length;j++) { if (L.r[i] > L.r[j]) { temp = L.r[i]; L.r[i] = L.r[j]; L.r[j] = temp; } L.count++; } L.count++; } } public void QuickSort(ref SqList L, int left, int right) { //快速排序 int i, j; int middle, temp; i = left; j = right; middle = L.r[left]; while (true) { while ((++i) < right - 1 && L.r[i] < middle) ; while ((--j) > left && L.r[j] > middle) ; if (i >= j) break; temp = L.r[i]; L.r[i] = L.r[j]; L.r[j] = temp; L.count++; } L.r[left] = L.r[j]; L.r[j] = middle; if (left < j) QuickSort(ref L, left, j); if (right > i) QuickSort(ref L, i, right); } private void Merge (int[] SR, int[] TR, int i, int m, int n,ref int count) { int j, k; for (j = m + 1, k = i; i <= m && j <= n; ++k) { if (SR[i] < SR[j]) TR[k] = SR[i++]; else TR[k] = SR[j++]; count++; } if (i <= m) while (k <= n && i <= m) TR[k++] = SR[i++]; if (j <= n) while (k <= n && j <= n) TR[k++] = SR[j++]; } public void MSort(int[] SR, int[] TR1, int s, int t,ref int count) { //归并排序 int m; int[] TR2 = new int[TR1.Length]; if (s == t) TR1[t] = SR[s]; else { m = (s + t) / 2; MSort(SR, TR2, s, m,ref count); MSort(SR, TR2, m + 1, t,ref count); Merge(TR2, TR1, s, m, t,ref count); } count++; } void Adjust(int[] a, int i, int n,ref int count) { int j = 0; int temp = 0; temp = a[i]; j = 2 * i + 1; while (j <= n - 1) { if (j < n - 1 && a[j] < a[j + 1]) j++; if (temp >= a[j]) break; a[(j - 1) / 2] = a[j]; j = 2 * j + 1; count++; } a[(j - 1) / 2] = temp; } public void HeapSort(int[] a,ref int count) { //堆排序 int n = a.Length; int temp = 0; for (int i = n / 2; i > 0; i--) { Adjust(a, i - 1, n,ref count); count++; } for (int i = n - 2; i >= 0; i--) { temp = a[i + 1]; a[i + 1] = a[0]; a[0] = temp; Adjust(a, 0, i + 1,ref count); count++; } } } public struct SqList { public int []r; public int count; } //Main class Program { static void Main(string[] args) { SqList L = new SqList(); Test2 t2 = new Test2(); Console.WriteLine("各种排序算法/n/n原始数据为"); t2.InitialList(ref L); t2.PrintList(L); Console.WriteLine("/n直接插入排序:"); t2.InsertSort(ref L); t2.PrintList(L); Console.WriteLine("/n冒泡排序:"); t2.InitialList(ref L); t2.BubbleSort(ref L); t2.PrintList(L); Console.WriteLine("/n快速排序:"); t2.InitialList(ref L); t2.QuickSort(ref L, 0, L.r.Length); t2.PrintList(L); Console.WriteLine("/n归并排序:"); t2.InitialList(ref L); t2.MSort(L.r, L.r, 0, L.r.Length-1,ref L.count); t2.PrintList(L); Console.WriteLine("/n堆排序:"); t2.InitialList(ref L); t2.HeapSort(L.r,ref L.count); t2.PrintList(L); } }