首页 > 编程语言 > 详细

算法——鸡尾酒排序java版

时间:2021-07-15 18:57:14      阅读:23      评论:0      收藏:0      [点我收藏+]

java版本

package Test;

import java.util.Arrays;


public class Test{

    public static void cocktailSort(int[] array) {
        for (int i = 0; i < array.length / 2; i++) {
            // 有序标记,每一轮的初始是true
            boolean isStored = true;
            for (int j = i; j < array.length-i-1; j++) {
                if (array[j] > array[j+1]) {
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                    isStored = false;
                }
            }
            if (isStored) {
                break;
            }
            isStored = true;
            for (int j = array.length-i-1; j > i; j--) {
                if (array[j] < array[j-1]) {
                    int temp = array[j-1];
                    array[j-1] = array[j];
                    array[j] = temp;
                    isStored = false;
                }
            }
            if (isStored) {
                break;
            }
        }   
    }

    public static void main(String[] aStrings) {
        int[] arr = new int[] {54, 26, 44, 17, 77, 26, 31, 93, 55};
        cocktailSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}

算法——鸡尾酒排序java版

原文:https://www.cnblogs.com/pywjh/p/15016716.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!