博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
冒泡排序及优化(JAVA算法)
阅读量:2159 次
发布时间:2019-05-01

本文共 807 字,大约阅读时间需要 2 分钟。

  • 定义一个静态数组,手动添加数字,利用冒泡排序并遍历排好序后的数组(图1)
public class Instance {
public static void main(String[] args){
//定义一个静态数组 int[] arr = {
3,43,1,56,5,8,9,21}; //使用冒泡排序 for(int i=1; i
arr[j+1]){
int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } //遍历排好序的数组 for(int i=0; i
  • 冒泡排序的优化(图2)
/*		// (如图1) 使用冒泡排序	for(int i=1; i
arr[j+1]){ int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } *///冒泡排序方法static void bubbleSort(int[] arr){
//对冒泡排序内容进行优化 for(int i=1; i
arr[j+1]){
int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; flag = false; } } if(flag){
break; } } }

*定义一个的动态数组,自动生成数字,进行冒泡排序并遍历(图3)

/*  	// 遍历数组方法    static void show(int[] arr){    		// (如图1) 遍历数组        for(int i=0; i

转载地址:http://pkgwb.baihongyu.com/

你可能感兴趣的文章
Leetcode C++《热题 Hot 100-17》461.汉明距离
查看>>
Leetcode C++《热题 Hot 100-18》538.把二叉搜索树转换为累加树
查看>>
Leetcode C++《热题 Hot 100-19》543.二叉树的直径
查看>>
Leetcode C++《热题 Hot 100-21》581.最短无序连续子数组
查看>>
Leetcode C++《热题 Hot 100-22》2.两数相加
查看>>
Leetcode C++《热题 Hot 100-23》3.无重复字符的最长子串
查看>>
Leetcode C++《热题 Hot 100-24》5.最长回文子串
查看>>
Leetcode C++《热题 Hot 100-26》15.三数之和
查看>>
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>
Leetcode C++《热题 Hot 100-29》22.括号生成
查看>>
Leetcode C++《热题 Hot 100-44》102.二叉树的层次遍历
查看>>
Leetcode C++《热题 Hot 100-47》236.二叉树的最近公共祖先
查看>>
Leetcode C++《热题 Hot 100-48》406.根据身高重建队列
查看>>
《kubernetes权威指南·第四版》第二章:kubernetes安装配置指南
查看>>
Leetcode C++《热题 Hot 100-49》399.除法求值
查看>>
Leetcode C++《热题 Hot 100-51》152. 乘积最大子序列
查看>>
Leetcode C++ 《第181场周赛-1》 5364. 按既定顺序创建目标数组
查看>>
Leetcode C++ 《第181场周赛-2》 1390. 四因数
查看>>
阿里云《云原生》公开课笔记 第一章 云原生启蒙
查看>>
阿里云《云原生》公开课笔记 第二章 容器基本概念
查看>>