冒泡排序_鸡尾酒排序
就是双向冒泡排序
此算法与冒泡排序的不同处在于排序时是以双向在序列中进行排序,外层比较左右边界l<r,
内层一个循环从左向右比,取高值置后;一个循环从右向左,取低值置前;
效率上,O(N^2), 不比普通的冒泡快
public class Bubble_CocktailSort { public static void main(String[] args) { int len = 10; int[] ary = new int[len]; Random random = new Random(); for (int j = 0; j < len; j++) { ary[j] = random.nextInt(1000); } /* * 交换次数最小也是1次,最大也是(n^2-n)/2次 */ // ary=new int[]{10,9,8,7,6,5,4,3,2,1}; //测试交换次数 // ary=new int[]{1,2,3,4,5,6,7,8,10,9}; //测试交换次数 System.out.println("-------排序前------"); for (int j = 0; j < ary.length; j++) { System.out.print(ary[j] + " "); } orderAsc1(ary); // orderAsc2(ary); //降序,只需要把判断大小于 置换一下 } static void orderAsc1(int[] ary) { int compareCount = 0;//比较次数 int changeCount = 0;//交换次数 int len = ary.length; int left = 0, right = len -1, tl, tr; while (left < right) {//外层固定循环次数 tl = left + 1; tr = right - 1; for (int k = left; k < right; k++) {//内层从多到少递减比较次数, 从左向右 if (ary[k] > ary[k + 1]) {//前大于后, 置换 ary[k] = ary[k + 1] + (ary[k + 1] = ary[k]) * 0;//一步交换 changeCount++; tr = k; //一轮中最后一比较的时候,将k所在索引赋给tr, tr表示以后比较的结束索引值, 从左向右比后,k表示左边的索引 } compareCount++; } right = tr; for (int k = right; k > left; k--) {//内层从多到少递减比较次数, 从右向左 if (ary[k] < ary[k - 1]) {//后小于前 置换 ary[k] = ary[k - 1] + (ary[k - 1] = ary[k]) * 0;//一步交换 changeCount++; tl = k; //一轮中最后一比较的时候,将k所在索引赋给tl, tl表示以后比较的开始索引值, 从向右向左比后,k表示右边的索引 } compareCount++; } left = tl; } System.out.println("\n-----orderAsc1升序排序后------比较次数:" + compareCount + ", 交换次数:" + changeCount); for (int j = 0; j < len; j++) { System.out.print(ary[j] + " "); } } //跟orderAsc1的思路没有区别 static void orderAsc2(int[] ary) { int compareCount = 0;//比较次数 int changeCount = 0;//交换次数 int len = ary.length; int l = 0, r = len -1, tl, tr; for (; l < r; ) {//外层固定循环次数 tl = l + 1; tr = r - 1; /* * 从左向右比,将大的移到后面 */ for (int k = l; k < r; k++) { if (ary[k] > ary[k + 1]) { int temp = ary[k] + ary[k + 1]; ary[k + 1] = temp - ary[k + 1]; ary[k] = temp - ary[k + 1]; changeCount++; tr = k; } compareCount++; } r = tr; /* * 从右向左比,将小的移到前面 */ for (int k = r; k > l; k--) { if (ary[k] < ary[k - 1]) { int temp = ary[k] + ary[k - 1]; ary[k - 1] = temp - ary[k - 1]; ary[k] = temp - ary[k - 1]; changeCount++; tl = k; } compareCount++; } l = tl; } System.out.println("\n-----orderAsc2升序排序后------比较次数:" + compareCount + ", 交换次数:" + changeCount); for (int j = 0; j < len; j++) { System.out.print(ary[j] + " "); } } }
打印
-------排序前------ 783 173 53 955 697 839 201 899 680 677 -----orderAsc1升序排序后------比较次数:34, 交换次数:22 53 173 201 677 680 697 783 839 899 955
时间: 2024-11-06 06:26:05