简体   繁体   English

尝试在 Java 中进行冒泡排序

[英]Trying to Bubble Sort in java

Trying to code so that a list of numbers that the user inputs is sorted through bubble sorting.尝试编码,以便通过冒泡排序对用户输入的数字列表进行排序。 This is what I have so far:这是我到目前为止:

import java.util.Scanner;

public class BubbleSort
{    
    public static void main(String[] args)
    {
        int n; 

        int[] list[];
        System.out.println("Please enter number of the elements to be sorted");
        Scanner keyboard = new Scanner(System.in);
        n = keyboard.nextInt();

        for ( int pass = 1; pass < n; pass++)
            for (int i = 0; i < n - pass ; i++)
                {
                    if (list[i] > list[i + 1]){
                        int temp = list[n];
                        list[i] = list[i + 1];
                        list[i + 1] = temp;
                    }
                }

    }

}

I get the following error that says我收到以下错误消息

The operator > is undefined for the argument type(s) int[], int[]" for the line: if (list[i] > list[i + 1])

type mismatch: cannot convert from int[] to int" for line: int temp = list[n];

Type mismatch: cannot convert from int to int[]" for line: list[i + 1] = temp;

Thank you so much in advance for your time and help.非常感谢您的时间和帮助。

You just declared a 2D array using this int[] list[];你刚刚使用这个int[] list[];声明了一个2D数组int[] list[]; . . 2D array is an array of arrays and can be declared like this二维数组是数组的数组,可以这样声明

int list[][];
int []list[];
int[] list[];

but your requirement is of 1D array which should be declared like this但您的要求是一维数组,应该像这样声明

 int[] list;
 int list[];

plus you forgot the initialization of your array and taking array data values from user, To initiliaze array加上您忘记了数组的初始化并从用户那里获取数组数据值,以初始化数组

 n = keyboard.nextInt();
 list=new int[n]; // initialize array length  

To take array value from user , you need to again loop n times and take input using keyboard.nextInt();要从 user 获取数组值,您需要再次循环n次并使用keyboard.nextInt();获取输入keyboard.nextInt(); to take all array values取所有数组值

see this example to properly implement your logic请参阅此示例以正确实现您的逻辑

package com.borntoinnovation.datastructure;

import java.util.Arrays;

//Sort between value like 0-1, 1-2, 2-3, 3-4, 4-5 :Suppose end is 5
//0-1, 1-2, 2-3, 3-4 ( 5th already sorted into their position
//0-1, 1-2, 2-3 (4th sorted)
//.... like that
public class SortingBubble {

    public static void main(String[] args) {
        int[] array = new int[] { 40, 10, -30, 45, 39, 32 };

        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] < array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
            System.out.println(Arrays.toString(array));
        }
    }
}

/*
 * [40, 10, 45, 39, 32, -30] 
 * [40, 45, 39, 32, 10, -30] 
 * [45, 40, 39, 32, 10, -30]
 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM