用户输入的 Int 值的冒泡排序数组
Bubble sort Arrays of User Input of Int values
我正在尝试解决我在使用此代码时遇到的两个问题。首先,我想将我的整数输入设置为最大值 100,其次,我试图获取输入的整数数组并按绝对值对它们进行排序。因此,如果用户使用此程序,我希望他们只能输入 99 作为他们想要排序的整数数量。当它最终排序而不是降序或升序时,我希望它是这样的; -1,2,-6,10,-20。
public static void main(String args[])
{
int n, i, j, temp;
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);
//Input area for user data, setting the number of integers to sort
System.out.print("Enter Total Number of Integers you would like to sort : ");
n = scan.nextInt();
for (n=0, n < 100, n++)
{
}
//Input area for user data, asking user to input the ints into an array for sorting
System.out.print("Enter " +n+ " Numbers : ");
for(i=0; i<n; i++)
{
arr[i] = scan.nextInt();
}
// Sorting Array using Bubble Sort Technique
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// Command line output from user, sorted by absolute value
System.out.print("Sorted List : \n");
for(i=0; i<n; i++)
{
System.out.print(java.lang.Math.abs(arr[i])+ " ");
}
}
}
我有一些建议(主要是次要的)和一个可能的解决方案:
代码风格很重要!使用 appropriate indentation and spacing.
不要在函数顶部声明一堆索引变量。这对 C89 没问题,但在 Java 中没有必要。将变量保持在尽可能小的范围内可以防止错误。
在您的排序例程中使用 Math.abs()
而不是您的打印代码来获得您想要的结果。
当您提前不确定长度时,请考虑使用 ArrayList 而不是原始数组。复制填充后的数组以消除未使用的尾部是另一种合理的方法。否则,arr.length
是不准确的,您将依赖随附的长度变量,这可能会在以后导致错误。
考虑使用函数来模块化您的代码,尤其是对于明显可重用和模块化的组件,例如排序。
考虑使用冒泡排序以外的其他方法,尽管它适用于此类教学示例,但效率低下。像 Arrays.sort()
这样的内置排序库是工业强度方法。
for (n=0, n < 100, n++)
是语法错误。用;
分隔for
。此行还将您刚刚从用户那里收集的 n
值覆盖为 0。使用 i
在这里计数或执行 n--
直到 n == 0
迭代 n
次.
如果您最多允许使用 99 个整数,请使用长度为 99 而不是 50 的数组,这样您就可以容纳所有内容。
如果用户输入非整数,nextInt()
将崩溃。查看 error handling 来处理此类情况。
这是一些代码:
import static java.lang.System.out;
import java.util.*;
class Main {
public static void main(String args[]) {
int arr[] = new int[99];
Scanner scan = new Scanner(System.in);
out.print("Enter total number of integers you would like to sort: ");
int n = scan.nextInt();
while (n < 1 || n > 99) {
out.print("Please enter a number between 1 and 99: ");
n = scan.nextInt();
}
for (int i = 0; i < n; i++) {
out.print("Input a number less than 100: ");
arr[i] = scan.nextInt();
if (arr[i] > 100) {
out.println("That number was over 100. Try again.");
i--;
}
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (Math.abs(arr[j]) > Math.abs(arr[j + 1])) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
out.println("Sorted List :");
for (int i = 0; i < n; i++) {
out.print(arr[i] + " ");
}
}
}
样本运行
Enter total number of integers you would like to sort: 6
Input a number less than 100: 1
Input a number less than 100: -9
Input a number less than 100: 3
Input a number less than 100: 4
Input a number less than 100: -5
Input a number less than 100: 6
Sorted List :
1 3 4 -5 6 -9
我正在尝试解决我在使用此代码时遇到的两个问题。首先,我想将我的整数输入设置为最大值 100,其次,我试图获取输入的整数数组并按绝对值对它们进行排序。因此,如果用户使用此程序,我希望他们只能输入 99 作为他们想要排序的整数数量。当它最终排序而不是降序或升序时,我希望它是这样的; -1,2,-6,10,-20。
public static void main(String args[])
{
int n, i, j, temp;
int arr[] = new int[50];
Scanner scan = new Scanner(System.in);
//Input area for user data, setting the number of integers to sort
System.out.print("Enter Total Number of Integers you would like to sort : ");
n = scan.nextInt();
for (n=0, n < 100, n++)
{
}
//Input area for user data, asking user to input the ints into an array for sorting
System.out.print("Enter " +n+ " Numbers : ");
for(i=0; i<n; i++)
{
arr[i] = scan.nextInt();
}
// Sorting Array using Bubble Sort Technique
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// Command line output from user, sorted by absolute value
System.out.print("Sorted List : \n");
for(i=0; i<n; i++)
{
System.out.print(java.lang.Math.abs(arr[i])+ " ");
}
}
}
我有一些建议(主要是次要的)和一个可能的解决方案:
代码风格很重要!使用 appropriate indentation and spacing.
不要在函数顶部声明一堆索引变量。这对 C89 没问题,但在 Java 中没有必要。将变量保持在尽可能小的范围内可以防止错误。
在您的排序例程中使用
Math.abs()
而不是您的打印代码来获得您想要的结果。当您提前不确定长度时,请考虑使用 ArrayList 而不是原始数组。复制填充后的数组以消除未使用的尾部是另一种合理的方法。否则,
arr.length
是不准确的,您将依赖随附的长度变量,这可能会在以后导致错误。考虑使用函数来模块化您的代码,尤其是对于明显可重用和模块化的组件,例如排序。
考虑使用冒泡排序以外的其他方法,尽管它适用于此类教学示例,但效率低下。像
Arrays.sort()
这样的内置排序库是工业强度方法。for (n=0, n < 100, n++)
是语法错误。用;
分隔for
。此行还将您刚刚从用户那里收集的n
值覆盖为 0。使用i
在这里计数或执行n--
直到n == 0
迭代n
次.如果您最多允许使用 99 个整数,请使用长度为 99 而不是 50 的数组,这样您就可以容纳所有内容。
如果用户输入非整数,nextInt()
将崩溃。查看 error handling 来处理此类情况。
这是一些代码:
import static java.lang.System.out;
import java.util.*;
class Main {
public static void main(String args[]) {
int arr[] = new int[99];
Scanner scan = new Scanner(System.in);
out.print("Enter total number of integers you would like to sort: ");
int n = scan.nextInt();
while (n < 1 || n > 99) {
out.print("Please enter a number between 1 and 99: ");
n = scan.nextInt();
}
for (int i = 0; i < n; i++) {
out.print("Input a number less than 100: ");
arr[i] = scan.nextInt();
if (arr[i] > 100) {
out.println("That number was over 100. Try again.");
i--;
}
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (Math.abs(arr[j]) > Math.abs(arr[j + 1])) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
out.println("Sorted List :");
for (int i = 0; i < n; i++) {
out.print(arr[i] + " ");
}
}
}
样本运行
Enter total number of integers you would like to sort: 6
Input a number less than 100: 1
Input a number less than 100: -9
Input a number less than 100: 3
Input a number less than 100: 4
Input a number less than 100: -5
Input a number less than 100: 6
Sorted List :
1 3 4 -5 6 -9