我正在尝试实现一个基本的 java 冒泡排序,但我得到了错误的输入..
Code is:
public class BubbleSort{
public static void main(String args[]){
int [] arr_sort=new int [] {5, 10, 50, 32, 52, 25};
System.out.println("Bubble Sort");
System.out.println("Before sorting: ");
int x;
for (x=0; x<6; x++){
System.out.print(arr_sort[x] + " ");
}
System.out.println();
System.out.println("After Sorting");
int n = arr_sort.length;
int temp = 0;
for(int i=0; i<n; i++){
for(int j=1; j<(n-1); j++){
if(arr_sort[j-1] > arr_sort[j]){
temp = arr_sort[j-1];
arr_sort[j-1] = arr_sort[j];
arr_sort[j] = temp;
}
} System.out.print(arr_sort[i] + " ");
}
}
}
Result:
Bubble Sort
Before sorting:
5 10 50 32 52 25
After Sorting
5 10 32 50 52 25
Process completed.
数组的不同整数:
Bubble Sort
Before sorting:
2 10 1 15 62 71
After Sorting
2 2 10 15 62 71
Process completed.
我严重迷失了,我不知道该怎么办..请帮助..
提前致谢。
顺便说一下,这是一份家庭作业。
for(int i = 0; i < n; i++) {
for(int j = i+1; j < n; j++) {
if(arr_sort[j] < arr_sort[i]) {
temp = arr_sort[i];
arr_sort[i] = arr_sort[j];
arr_sort[j] = temp;
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)