鱼C论坛

 找回密码
 立即注册
查看: 1859|回复: 5

[已解决]冒泡 为啥问我的不行 报错的意思是啥

[复制链接]
发表于 2022-5-19 18:07:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import java.util.Arrays;

  2. public class ArraysDemon07 {
  3.     public static void main(String[] args) {
  4.         int[] a = {4,34,5,234,986,865};
  5.         int[] maoPao = maoPao(a);
  6.         System.out.println(Arrays.toString(a));

  7.     }
  8.         //冒泡排序
  9.     public static int[] maoPao(int[] array) {
  10.         int c = 0;
  11.         for (int i = 0; i < array.length - 1; i++) {
  12.             for (int j = 0; j < array.length - i; j++) {
  13.                 if (array[j + 1] > array[j]) {
  14.                     c = array[j];
  15.                     array[j] = array[j + 1];
  16.                     array[j + 1] = c;

  17.                 }
  18.             }
  19.         }
  20.         return array;

  21.     }
  22. }
复制代码
最佳答案
2022-5-19 22:54:08

你冒泡排序第二个 for 循环需要 -1 ,否则就会导致第一次循环超出索引

参考代码:

  1. package pers.uang;

  2. import java.util.Arrays;

  3. public class ArraysDemon07 {

  4.     public static void main(String[] args) {
  5.         int[] a = {4,34,5,234,986,865};
  6.         int[] maoPao = maoPao(a);
  7.         System.out.println(Arrays.toString(a));

  8.     }
  9.     //冒泡排序
  10.     public static int[] maoPao(int[] array) {
  11.         int c = 0;
  12.         for (int i = 0; i < array.length - 1; i++) {
  13.             for (int j = 0; j < array.length - i - 1; j++) {
  14.                 if (array[j + 1] > array[j]) {
  15.                     c = array[j];
  16.                     array[j] = array[j + 1];
  17.                     array[j + 1] = c;

  18.                 }
  19.             }
  20.         }
  21.         return array;

  22.     }
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-19 18:11:15 | 显示全部楼层
解决了 咋删帖求问
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-19 18:35:46 | 显示全部楼层

给人最佳答案就完了,会自己沉下去
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-19 18:49:04 | 显示全部楼层
豆嘉木 发表于 2022-5-19 18:35
给人最佳答案就完了,会自己沉下去

不是 ,你都高级会员了
要技术值有啥用
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-19 19:06:01 | 显示全部楼层
柿子饼同学 发表于 2022-5-19 18:49
不是 ,你都高级会员了
要技术值有啥用

我要c币啊啊啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-5-19 22:54:08 | 显示全部楼层    本楼为最佳答案   

你冒泡排序第二个 for 循环需要 -1 ,否则就会导致第一次循环超出索引

参考代码:

  1. package pers.uang;

  2. import java.util.Arrays;

  3. public class ArraysDemon07 {

  4.     public static void main(String[] args) {
  5.         int[] a = {4,34,5,234,986,865};
  6.         int[] maoPao = maoPao(a);
  7.         System.out.println(Arrays.toString(a));

  8.     }
  9.     //冒泡排序
  10.     public static int[] maoPao(int[] array) {
  11.         int c = 0;
  12.         for (int i = 0; i < array.length - 1; i++) {
  13.             for (int j = 0; j < array.length - i - 1; j++) {
  14.                 if (array[j + 1] > array[j]) {
  15.                     c = array[j];
  16.                     array[j] = array[j + 1];
  17.                     array[j + 1] = c;

  18.                 }
  19.             }
  20.         }
  21.         return array;

  22.     }
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-7 16:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表