我正在为课程编写程序,以显示编写冒泡排序的能力。我已经工作了几天,似乎无法得到它。至少现在它编译,但抛出异常 我评论了我遇到问题的部分,即数组中元素的实际交换。
该程序应该生成一个包含20个random整数的数组,然后使用冒泡排序对它们进行排序,打印出每个遍,直到它完成。
import java.util.*;
public class BubbleSorting {
public static void bubbleSort(ArrayList<Integer> arr) {
int n = arr.size();
int temp = 0;
for (int i = 0; i < n; i++) {
//this is the chunk of code that I am having problems with
for (int j = i; j < (n-1); j++) {
if (arr.get(n-1) < arr.get(j))
temp = arr.get(j-1);
arr.set(j-1, arr.get(j));
arr.set(j, temp);
}
}
}
private static void printOut(int pass, ArrayList<Integer> array) {
System.out.print("Pass " + pass + ": ");
for (int i = 0; i < array.size() - 1; i++) {
System.out.print(array.get(i) + ", ");
}
System.out.print(array.get(array.size() - 1) + ".");
System.out.println();
}
public static void main(String[] args) {
ArrayList<Integer> array = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
String userInput = "";
boolean endLoop = false;
do{
try{
for (int i = 0; i < 20; i++) {
int element = (int)(1000.0 * Math.random());
array.add(element);
}
System.out.print("\nUnsorted Array: ");
//Displays the unsorted ArrayList
for (int i = 0; i < array.size() - 1; i++) {
System.out.print(array.get(i) + ", ");
}
System.out.print(array.get(array.size() - 1) + ".");
System.out.println();
bubbleSort(array);
}
catch (IndexOutOfBoundsException e) {
System.out.println("\nThere is an out of bounds error in the ArrayList.");
}
System.out.print("\nEnter Y to continue or N to quit: ");
userInput = sc.nextLine();
if (userInput.equalsIgnoreCase("Y")) {
endLoop = false;
}
else if (userInput.equalsIgnoreCase("N")) {
endLoop = true;
}
else {
System.out.println("\nYou did not enter Y or N.");
System.out.println("Please try again.");
}
}while(endLoop == false);
}
}
答案 0 :(得分:0)
请尝试以下代码:
for(int j = 1; j < (list.size() -i); j++)
你犯了2个错误...在你写int j=i
的时候,第一个for循环它protocol Base {}
protocol Extended: Base {}
struct Instance:Extended {}
let anInstance = Instance()
let instanceOfBase = anInstance as Base
let instanceOfExtended = anInstance as Extended
func aMethod<T:Base>(_ instance:T) {}
aMethod(anInstance)
aMethod(instanceOfBase) // Error - Cannot invoke 'aMethod' with an argument list of type '(Base)'
aMethod(instanceOfExtended) // Error - Cannot invoke 'aMethod' with an argument list of type '(Extended)'
,第二个是你没有覆盖if循环条件中的{}括号。欢呼
答案 1 :(得分:0)
男孩你错过了if语句中的括号。
//this is the chunk of code that I am having problems with
for (int j = i; j < (n - 1); j++) {
if (arr.get(n - 1) < arr.get(j)) {//<------here this one
temp = arr.get(j - 1);
arr.set(j - 1, arr.get(j));
arr.set(j, temp);
}//<----this too
}
如果你没有放置括号,只考虑if之后的第一个语句。
答案 2 :(得分:0)
您可能对冒泡排序的工作方式存在误解。以下是冒泡排序的工作原理示例(取自this link)
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest
number to greatest number using bubble sort. In each step, elements written
in bold are being compared. Three passes will be required.
First Pass
( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm
compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order
(8 > 5), algorithm does not swap them.
Second Pass
( 1 4 2 5 8 ) ( 1 4 2 5 8 )
( 1 4 2 5 8 ) ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
Now, the array is already sorted, but the algorithm does not know if it is
completed. The algorithm needs one whole pass without any swap to know it is
sorted.
Third Pass
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
( 1 2 4 5 8 ) ( 1 2 4 5 8 )
现在,将这条思路与您的代码进行比较:
for (int i = 0; i < n; i++) {
//this is the chunk of code that I am having problems with
for (int j = i; j < (n-1); j++) {
if (arr.get(n-1) < arr.get(j))
temp = arr.get(j-1);
arr.set(j-1, arr.get(j));
arr.set(j, temp);
}
}
现在,如果您在示例中使用该数组(5 1 4 2 8)并将其插入到您的代码中,它将最终将8与每个给定的数字进行比较,并且因为8已经是最大的,if语句总是假的;不会发生排序。相反,一次比较两个相邻的索引并使用布尔值来指示是否发生了交换。鉴于此类功能,排序将继续尽可能地将最大数量交换到数组的末尾。因此,一旦没有交换,您就知道数组已经排序。因此,您希望内部循环仅上升到已排序的部分,不再进一步。如果比较排序值,排序将提前结束。尝试使用这种思维方式并将其应用于您的代码。