198冒泡排序原理
排序,将一组数据按固定的规则进行排序
【冒泡排序】,一种排序的方式,对要进行排序的数据中相邻的数据进行两两比较,将较大的数据放在后面,依次对所有数据进行操作,直至所有数据按要求完成排序
例如
5个人,他们身高不等。第一遍排序,从左至右进行,两两比较,比较高的人换到右边,一轮比完,最高的人会换到最右边。第二轮排序,第二高的人换到从右至左第二个位置
199 冒泡排序代码实现
需求
如果有x个数据需要排序,需要比较x-1次,每一次比完,下一次就会少一个数据参与
报错了
为什么调用方法红了?——因为那个方法定义在了main方法里面,方法不能套方法
就挺不理解的
Q,为什么一定要在i-for循环外面套个x-for循环才可以,我看着这么写可以满足需要啊?(实际不能)
for(int i = 0;i<arr.length-1-i;i++){
if(arr[i]>arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
A,只写i-for循环,则执行一次,能够把最大值换到最后。要实现全部的排序,必须套一层for循环,冒泡排序都是用2个嵌套的循环实现的。
--------------------------------------------------------------
myArrays
it01e199.ArrayDemo,it01e19902.ArrayDemo(前一个要把同一段代码复制多次,后一个是用一个循环实现整个的排序)
--------------------------------------------------------------
package it01e199;
public class ArrayDemo {
public static void main(String[] args) {
//define an array
int[] arr = {6,4,3,2,0};
System.out.println("before:"+arrayToString(arr));
//the first round
for(int i = 0;i<arr.length-1-0;i++){
if(arr[i]>arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}//why the for-loop don't work,and the before is the same with the after?_you wrote i as 1 you fool,and there should be one more code:arr[i+1]=temp
System.out.println("R1:"+arrayToString(arr));
//the second round
for(int i = 0;i<arr.length-1-1;i++){
if(arr[i]>arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
System.out.println("R2:"+arrayToString(arr));
//the third round
for(int i = 0;i<arr.length-1-2;i++){
if(arr[i]>arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
System.out.println("R3:"+arrayToString(arr));
//the forth round is end
for(int i = 0;i<arr.length-1-3;i++){
if(arr[i]>arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
System.out.println("R4:"+arrayToString(arr));
}
//arrange a set of data according to specified rules
public static String arrayToString(int[]arr){
StringBuilder sb = new StringBuilder();
sb.append("[");
// public StringBuilder append(String str) {
// super.append(str);
// return this;
// }
for(int i = 0;i<arr.length;i++){
if(i == arr.length-1){
sb.append(arr[i]);
}else{
sb.append(arr[i]).append(",");
}
}
sb.append(",]");
String s = sb.toString();
return s;
}
}
--------------------------------------------------------------
package it01e19902;
public class ArrayDemo {
public static void main(String[] args) {
//define an array
int[] arr = {9,4,3,2,0};
System.out.println("没有x-for循环前:"+arrayToString(arr));
for(int i = 0;i<arr.length-1-i;i++){
if(arr[i]>arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
System.out.println("没有x-for循环后:"+arrayToString(arr));
System.out.println("x-for循环前:"+arrayToString(arr));
for(int x = 0;x<arr.length;x++){
for(int i = 0;i<arr.length-1-x;i++){
if(arr[i]>arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}//do we need two for-loops?_yes,no x-for-loop,no correct result.why?
System.out.println("x-for循环后:"+arrayToString(arr));//output:before:[9,4,3,2,0,]<br>after:[0,2,3,4,9,]
}
//arrange a set of data according to specified rules
public static String arrayToString(int[]arr){
StringBuilder sb = new StringBuilder();
sb.append("[");
// public StringBuilder append(String str) {
// super.append(str);
// return this;
// }
for(int i = 0;i<arr.length;i++){
if(i == arr.length-1){
sb.append(arr[i]);
}else{
sb.append(arr[i]).append(",");
}
}
sb.append(",]");
String s = sb.toString();
return s;
}
}