void QuickSort(CArray array, int low, int high) { if (low < high) { int mid = Partition(array, low, high); QuickSort(array, low, mid - 1); QuickSort(array, mid + 1, high); } }
/// <summary> /// 交换数值 /// </summary> /// <param name="a"></param> /// <param name="b"></param> public static void Swap(CArray array, int iIndex, int jIndex) { if (array.ValidIndex(iIndex) && array.ValidIndex(jIndex)) { int tmp = array.arr[iIndex]; array.arr[iIndex] = array.arr[jIndex]; array.arr[jIndex] = tmp; } }
/// <summary> /// 初始化固定大小的数组 /// </summary> /// <param name="seed"></param> /// <param name="size">数组大小</param> /// <returns></returns> public static CArray InitInstanceByRand(int seed, int size) { CArray array = new CArray(size); Random rnd = new Random(seed); for (int i = 0; i <= array.Upper; i++) { array.Insert((int)(rnd.NextDouble() * size)); } return(array); }
/// <summary> /// 冒泡排序具体实现 /// </summary> /// <param name="array"></param> public void Sort(CArray array) { //外层将最大值置后 for (int i = array.Upper; i >= 1; i--) { for (int j = 0; j <= array.Upper - 1; j++) { if (array[j] > array[j + 1]) { CArray.Swap(array, j, j + 1); } } } }
void RecMerge(CArray array, int low, int high) { if (low == high) { return; } else { int mid = (low + high) / 2; int midPlus = mid + 1; RecMerge(array, low, mid); RecMerge(array, midPlus, high); Merge(array, low, midPlus, high); } }
public void Sort(CArray array) { if (array == null) { throw new Exception("请初始化Array"); } for (int i = 0; i <= array.Upper; i++) { tmpArray.Insert(((CHeapArray)array).RemoveRoot()); } for (int i = tmpArray.NumElements - 1, j = 0; i >= 0; i--, j++) { array[j] = tmpArray[i]; } }
public void Sort(CArray array) { for (int i = 1; i <= array.Upper; i++) { int tmp = array[i]; //要插入的数据 int j = i; //从i处开始 while (j > 0 && array[j - 1] > tmp) //比插入数据大的往右移动 { array[j] = array[j - 1]; //往右移动,腾出空间插入temp j--; } array[j] = tmp; } }
public void Sort(CArray array) { for (int i = 0; i <= array.Upper; i++) { //在i处设置最小值 int minIndex = i; //遍历寻找最小值 for (int j = i + 1; j <= array.Upper; j++) { if (array[j] < array[minIndex]) { minIndex = j; } } CArray.Swap(array, i, minIndex); } }
/// <summary> /// 合并 /// </summary> /// <param name="array"></param> /// <param name="lowp">低index起始</param> /// <param name="highp">高index起始</param> /// <param name="ubound">高index顶部</param> void Merge(CArray array, int lowp, int highp, int ubound) { int mid = highp - 1; int lp_tmp = lowp; int hp_tmp = highp; int[] arrayTmp = new int[array.Count]; int j = 0; while (lp_tmp <= mid && hp_tmp <= ubound) { if (array[lp_tmp] < array[hp_tmp]) { arrayTmp[j] = array[lp_tmp]; lp_tmp++; j++; } else { arrayTmp[j] = array[hp_tmp]; hp_tmp++; j++; } } while (lp_tmp <= mid) { arrayTmp[j] = array[lp_tmp]; lp_tmp++; j++; } while (hp_tmp <= ubound) { arrayTmp[j] = array[hp_tmp]; hp_tmp++; j++; } for (int i = lowp, k = 0; i <= ubound; i++, k++) { array[i] = arrayTmp[k]; } }
/// <summary> /// 分隔2边左小右大,返回分隔index /// </summary> /// <param name="array"></param> /// <param name="low"></param> /// <param name="high"></param> /// <returns></returns> int Partition(CArray array, int low, int high) { //选取high作为中间值 //i保存与high交换的index,即中间值的index //i 保存的值始终比highda int i = low; int pivot = array[high]; for (int j = i; j <= high - 1; j++) { if (array[j] < pivot) { //i的左边保存的都是小于pivot的 //右边以及自身保存的都大于等于pivot的 CArray.Swap(array, i, j); i++; } } //互换时pivot处于中间 CArray.Swap(array, i, high); return(i); }
public void Sort(CArray array) { //gap定义 int[] gaps = new int[] { 701, 301, 132, 57, 23, 10, 4, 1 }; for (int gi = 0; gi < gaps.Length; gi++) { //针对当前gap的所有元素进行插入排序 for (int i = gaps[gi]; i <= array.Upper;) { int tmp = array[i]; //要插入的数据 int j = i; //从i处开始 while (j - gaps[gi] >= 0 && array[j - gaps[gi]] > tmp) //比插入数据大的往右移动 { array[j] = array[j - gaps[gi]]; //往右移动,腾出空间插入temp j = j - gaps[gi]; } array[j] = tmp; i += gaps[gi]; } } }
public void Sort(CArray array) { RecMerge(array, 0, array.Upper); }
public CHeapSort(int size) { tmpArray = new CArray(size); }
public void Sort(CArray array) { QuickSort(array, 0, array.NumElements - 1); }