java算法之冒泡排序

时间:2021-05-25 10:57:05

Android面试时经常也会被问到数据结构算法的问题,最近有时间,写点实例。

我模拟的场景是对一个班上学生成绩进行排名。


冒泡排序原理:

       N个元素相邻的两两元素进行比较,如果哪个元素大一些就调换(往上推),最终一轮下来,最大的元素就被推到了最后(就像气泡冒上来了越来越大一样,所以叫冒泡排序)此时第N个元素是最大的。下一轮就从0到N-1的元素里面两两比较,又将最大的元素(小于第N个元素)推到最后。这样不停的进行,最后数组就变成从小到大有序的了。


1.Person.java 实体类

package com.demo.sort;

public class Person {

/**
* 名字
*/
public String name;

/**
* 分数
*/
public int score;

public Person(String name, int score) {
super();
this.name = name;
this.score = score;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getScore() {
return score;
}

public void setScore(int score) {
this.score = score;
}

public String toString() {
return ""+score+"";
}

}

2.数据准备

package com.demo.sort;
public class DataUtil {
Person person1 = new Person("张一",60);
Person person2 = new Person("张二",82);
Person person3 = new Person("张三",23);
Person person4 = new Person("张四",31);
Person person5 = new Person("张五",55);
Person person6 = new Person("张六",89);
Person person7 = new Person("张七",92);
Person person8 = new Person("张八",48);
Person person9 = new Person("张九",50);
Person person10 = new Person("张十",23);

Person [] persons = {
person1,person2,person3,person4,person5,
person6,person7,person8,person9,person10
};

public Person[] getData(){
return persons;
}
}
3. 冒泡排序

package com.demo.sort;

/**
* 冒泡排序
* @author http://blog.csdn.net/nnmmbb
*
*/
public class SortDemo {

public static void main(String args[]){

Person [] persons = new DataUtil().getData();
beforeSort(persons);
//冒泡排序
BuddleSort(persons);
}


/**
* 冒泡排序
*
* @param persons
*/
private static void BuddleSort(Person[] persons) {
for(int i= persons.length-1;i>0;i--){
for(int j=0;j<i ; j++){
if(persons[j].score > persons[j+1].score){

System.out.println(persons[j].toString()+" == 调换 == "+ persons[j+1].toString());

Person temp =persons[j];
persons[j] = persons[j+1];
persons[j+1] = temp;
}else{
System.out.println(persons[j].toString()+" == 不调换 == "+ persons[j+1].toString());
}
}
afterSort(persons.length-i,persons);
System.out.println();
}

}

private static void afterSort(int index,Person[] persons) {
System.out.print("round " +index+ " sort:");
for(int i=0;i<persons.length;i++){
if(i == index){
System.out.print(" ["+ persons[i].toString() +"]");
}else{
System.out.print(" "+persons[i].toString());
}
}
System.out.println();
}


private static void beforeSort(Person[] persons) {
System.out.print("before sort:");
for(int i=0;i<persons.length;i++){
System.out.print(" "+persons[i].toString());
}
System.out.println();
System.out.println();
}


}

3.排序结果演示:

before sort: 60 82 23 31 55 89 92 48 50 23

60 == 不调换== 82
82 == 调换 == 23
82 == 调换 == 31
82 == 调换 == 55
82 == 不调换== 89
89 == 不调换== 92
92 == 调换 == 48
92 == 调换 == 50
92 == 调换 == 23
round 1 sort: 60 [23] 31 55 82 89 48 50 23 92

60 == 调换 == 23
60 == 调换 == 31
60 == 调换 == 55
60 == 不调换== 82
82 == 不调换== 89
89 == 调换 == 48
89 == 调换 == 50
89 == 调换 == 23
round 2 sort: 23 31 [55] 60 82 48 50 23 89 92

23 == 不调换== 31
31 == 不调换== 55
55 == 不调换== 60
60 == 不调换== 82
82 == 调换 == 48
82 == 调换 == 50
82 == 调换 == 23
round 3 sort: 23 31 55 [60] 48 50 23 82 89 92

23 == 不调换== 31
31 == 不调换== 55
55 == 不调换== 60
60 == 调换 == 48
60 == 调换 == 50
60 == 调换 == 23
round 4 sort: 23 31 55 48 [50] 23 60 82 89 92

23 == 不调换== 31
31 == 不调换== 55
55 == 调换 == 48
55 == 调换 == 50
55 == 调换 == 23
round 5 sort: 23 31 48 50 23 [55] 60 82 89 92

23 == 不调换== 31
31 == 不调换== 48
48 == 不调换== 50
50 == 调换 == 23
round 6 sort: 23 31 48 23 50 55 [60] 82 89 92

23 == 不调换== 31
31 == 不调换== 48
48 == 调换 == 23
round 7 sort: 23 31 23 48 50 55 60 [82] 89 92

23 == 不调换== 31
31 == 调换 == 23
round 8 sort: 23 23 31 48 50 55 60 82 [89] 92

23 == 不调换== 23
round 9 sort: 23 23 31 48 50 55 60 82 89 [92]


5.扩展内容

java算法之冒泡排序