所以我是一个完整的初学者,我想尝试一些我无法做到的排序算法。
所以这只是我想开始的事情的蓝图:
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
int[] a = {11,7,8,3,15,13,9,19,18,10,4};
int[] x;
int merker = a[0]; // the merker has the value 11 now
int i = 1;
int n = a.length;
while(n != 0){
while(i < n ) {
if (a[i] < merker)
merker = a[i];
i = i + 1;
}
merker == x[0];
}
}
}
然后以某种方式在每个while循环中切出“ merker”,直到我将其布局为x [1] = 1,x [2] = 2
当n = 0时,第一个while循环将停止,这是我通过切出要归入另一种算法的每个数字而得出的。
现在它根本不起作用,我敢肯定我犯了很多错误。
在“ merker == x [0];”位置处,“变量x可能尚未初始化。
我希望获得帮助,我是一个极端的菜鸟。
答案 0 :(得分:0)
您的代码中有很多错误。首先,您尚未初始化数组x []。其次,您不更新n的值,因此while循环有点像一个无限循环。
您可以使用冒泡排序对元素进行如下排序:
public class Sorting {
public static void main(String[] args) {
int[] a = {11,7,8,3,15,13,9,19,18,10,4};
Arrays.stream(a).forEach(System.out::println);
int n = a.length;
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
// swap temp and arr[i]
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
System.out.println("After sorting");
Arrays.stream(a).forEach(System.out::println);
}
}