Java Selection Sort Exchange无法正常工作(Java Selection Sort Exchange not working as desired)

编程入门 行业动态 更新时间:2024-10-22 13:36:24
Java Selection Sort Exchange无法正常工作(Java Selection Sort Exchange not working as desired)

我的任务:创建一个可以使用字符串和整数的选择排序。

您好,我想知道是否有人可以告诉我为什么我的列表不会排序,无论我输入什么列表,只是以相同的顺序吐出来。 我是StackOverflow和JavaProgramming的新手。 对于我目前正在制作的任何新手错误,我感到很抱歉。

public class selectionSort { public static void main(String[] args) { sort(args); printArray(args); } public static void sort(String[] array) { int outer = 0; while (outer < array.length - 1) { outer++; int minimumIndex = outer; int inner = outer + 1; while (inner < array.length) { inner++; if (inner < minimumIndex) { minimumIndex = inner; } } //exchange String temp = array[minimumIndex]; array[minimumIndex] = array[outer]; array[outer] = temp; } } public static void printArray(String[] array) { for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } } }

My task: Create a selection sort that will work with strings and integers.

Hello, I was wondering if someone could tell me why my list will not sort, whatever list I input, just gets spit back out in the same order. I am new to StackOverflow and JavaProgramming in general. I am sorry for any novice mistakes I am currently making.

public class selectionSort { public static void main(String[] args) { sort(args); printArray(args); } public static void sort(String[] array) { int outer = 0; while (outer < array.length - 1) { outer++; int minimumIndex = outer; int inner = outer + 1; while (inner < array.length) { inner++; if (inner < minimumIndex) { minimumIndex = inner; } } //exchange String temp = array[minimumIndex]; array[minimumIndex] = array[outer]; array[outer] = temp; } } public static void printArray(String[] array) { for (int i = 0; i < array.length; i++) { System.out.println(array[i]); } } }

最满意答案

您没有比较数组中字符串的值:

if (inner < minimumIndex) { minimumIndex = inner; }

也许你需要这个(如果你想用String的int值对字符串进行排序):

if (Integer.parseInt(array[inner]) < Integer.parseInt(array[minimumIndex])) { minimumIndex = inner; }

经过测试的代码:

public static void sort(String[] array) { int outer = 0; while (outer < array.length - 1) { int minimumIndex = outer; int inner = outer + 1; while (inner < array.length) { if (Integer.parseInt(array[inner]) < Integer.parseInt(array[minimumIndex])) { minimumIndex = inner; } inner++; } //exchange String temp = array[minimumIndex]; array[minimumIndex] = array[outer]; array[outer] = temp; outer++; } }

如果要按String排序:

if (array[inner].compareTo(array[minimumIndex]) < 0) { minimumIndex = inner; }

You are not comparing the value of Strings in array:

if (inner < minimumIndex) { minimumIndex = inner; }

Maybe you need this(If you want to sort the Strings by the int value of String):

if (Integer.parseInt(array[inner]) < Integer.parseInt(array[minimumIndex])) { minimumIndex = inner; }

Tested code:

public static void sort(String[] array) { int outer = 0; while (outer < array.length - 1) { int minimumIndex = outer; int inner = outer + 1; while (inner < array.length) { if (Integer.parseInt(array[inner]) < Integer.parseInt(array[minimumIndex])) { minimumIndex = inner; } inner++; } //exchange String temp = array[minimumIndex]; array[minimumIndex] = array[outer]; array[outer] = temp; outer++; } }

If you want to sort it by String:

if (array[inner].compareTo(array[minimumIndex]) < 0) { minimumIndex = inner; }

更多推荐

本文发布于:2023-07-22 10:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:无法正常   工作   Sort   Selection   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!