我的任务:创建一个可以使用字符串和整数的选择排序。
您好,我想知道是否有人可以告诉我为什么我的列表不会排序,无论我输入什么列表,只是以相同的顺序吐出来。 我是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; }更多推荐
发布评论