用java实现冒泡循环

发布时间:2023-05-22 09:28:40

public class BubbleSort {    public static void main(String[] args) {        int[] array = {5, 3, 8, 2, 1, 4};                System.out.println(原始数组:;        printArray(array);                bubbleSort(array);                System.out.println(排序后的数组:");        printArray(array);    }        public static void bubbleSort(int[] array) {        int n = array.length;        for (int i = 0; i < n - 1; i++) {            for (int j = 0; j < n - i - 1; j++) {                if (array[j] > array[j + 1]) {                    // 交换相邻元素的位置                    int temp = array[j];                    array[j] = array[j + 1];                    array[j + 1] = temp;                }            }        }    }        public static void printArray(int[] array) {        for (int i = 0; i < array.length; i++) {            System.out.print(array[i] + " ");        }        System.out.println();    }}

ps 图灵课堂老师从近一百套最新一线互联网公司面试题中精选而出,涵盖Java架构面试 所有技术栈,包括JVM,Mysql,并发,Spring,Redis,MQ,Zookeeper,Netty, Dubbo,Spring Boot,Spring Cloud,数据结构与算法,设计模式等相关技术领域的大 厂面试题及详解。 详情咨询客服获取全套面经试题。

上一篇 过滤器与拦截器
下一篇 Mybatis配置文件

文章素材均来源于网络,如有侵权,请联系管理员删除。

标签: