标签:ar for 数据 sp on c amp new res
protected void SelectSort(int[] array) { int tmp;//暂存要交换的数据 int t;//交换数据的位置 for (int i = 0; i < array.Length; i++) { t = i; for (int j = i + 1; j < array.Length; j++) { if (array[j] < array[t]) { t = j; } } tmp = array[i]; array[i] = array[t]; array[t] = tmp; } } /// <summary> /// 选择排序 /// </summary> public void SmallToarge() { int temp = 0; int[] iArry = new int[] { 49, 5, 13, 6, 10, 55, 99}; for (int i = 0; i < iArry.Length; i++) { for (int j = i+1; j < iArry.Length; j++) { if (iArry[i] > iArry[j]) { //A-----B C---A B-----C temp = iArry[j]; //这里实现互换如:A B C三个瓶子 B是空瓶 A给B(A空,B满)C 给A(C空A满)B给C(B空C满)实现交换 iArry[j] = iArry[i]; iArry[i] = temp; } } } //输出 for (int f = 0; f < iArry.Length; f++) { Response.Write(iArry[f]+"<br/>"); } } /// <summary> /// 冒泡排序 /// </summary> /// <param name="list"></param> public void sort(int[] list) { int i, j, temp; //定义变量 j = 1;//赋初始值 while (j < list.Length) { for (i = 0; i < list.Length - j; i++) //因为i的比较项是 i 和i+1 一个数组有6项那么i的最大值是5 { // int[] iArry = new int[] { 49, 5, 13, 6, 10, 55, 99 }; //49>5 49>13 49>6 49>10 49<55 55<99 //第一个和第二个比较,第二个和第三个比较,3和4,4和5,5和6,6和7 //int[] iArry = new int[] {10,9,8,7,6 }; //10>9 10>8 10>7 10>6 所以10是最大的 if (list[i] > list[i + 1]) //加以加较 { temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; } } j++; //每加一次少比较一项 } //输出 for (int f = 0; f < list.Length; f++) { Response.Write(list[f] + "<br/>"); } } /// <summary> /// 冒泡排序2 /// </summary> public void sortTwo() { int i,j,t; int[]array={4,2,6,9,22,1,100}; for (i = 0; i < array.Length - 1; i++) { for (j = 0; j < array.Length - i - 1; j++) { if (array[j] > array[j + 1]) { t = array[j]; array[j] = array[j + 1]; array[j + 1] = t; } } } //输出 for (int f = 0; f < array.Length; f++) { Response.Write(array[f] + "<br/>"); } } /// <summary> /// 直接插入排序 /// </summary> /// <param name="array"></param> public void InsertSort(int[] array) { int i, j; int temp; for (i = 1; i < array.Length; i++) { temp = array[i]; j = i - 1; //与已排序的数逐一比较, 大于temp时, 该数移后 while ((j >= 0) && (array[j] > temp)) { array[j + 1] = array[j]; j--; } array[j + 1] = temp; } for (int f = 0; f < array.Length; f++) { Response.Write(array[f] + "<br/>"); } }
标签:ar for 数据 sp on c amp new res
原文地址:http://www.cnblogs.com/lschenblog/p/3968260.html