您的位置:首页 > 其它

复杂数据的冒泡排序

2015-07-27 16:30 393 查看
前段时间,有个排序的功能就是做离我最近的那个,一可以想用冒泡,之前用冒泡都是简单的数据结构,一般都是数组来实现。在项目中往往不会这么简单,数据结构复杂得多。

本人在项目中使用的还不算太复杂,直接看代码把

/**
* list里面放个类 ,PublishVo是个泛型类,里面继续放一个hashmap
* 这里排序是拿的 PublishVo里面的属性 Count 来排序
* 并不是 PublishVo 属性里面 publishTheme 里面的值
*
* publishVos2为临时变量,暂时存储数据用于交换的
*
* 这里需要说明的 list 用于交换是set()方法 注意
*
* 本案例在项目中拿出来的·
*/
List<PublishVo<HashMap<String, Object>>> publishVos2 = new ArrayList<PublishVo<HashMap<String, Object>>>();
int size2 = publishVos.size();
for (int i = 0; i < size2 - 1; i++) {
for (int j = i + 1; j < size2; j++) {
if (publishVos.get(i).getCount() < publishVos.get(j).getCount()) {
publishVos2.add(publishVos.get(i));
publishVos.set(i, publishVos.get(j));
publishVos.set(j, publishVos2.get(0));
//需要清楚里面的数据 不然第二次拿的时候值就不对了
publishVos2.clear();
}
}
}
return publishVos;


注释都有,基本都看得懂。

package sorter;

import java.util.Comparator;

public class BubbleSorter implements Sorter {

@Override
public <T extends Comparable<T>> void sort(T[] list) {
boolean swapped = true;
for (int i = 1, len = list.length; i < len && swapped; ++i) {
swapped = false;
for (int j = 0; j < len - i; ++j) {
if (list[j].compareTo(list[j + 1]) > 0) {
T temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
swapped = true;
}
}
}
}

@Override
public <T> void sort(T[] list, Comparator<T> comp) {
boolean swapped = true;
for (int i = 1, len = list.length; i < len && swapped; ++i) {
swapped = false;
for (int j = 0; j < len - i; ++j) {
if (comp.compare(list[j], list[j + 1]) > 0) {
T temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
swapped = true;
}
}
}
}
public static void main(String[] args) {
Integer score[] = { 67, 69,11,98, 75, 87, 89, 10, 99, 100 };
BubbleSorter h = new BubbleSorter();
h.sort(score);
for (int i = 0; i < score.length; i++) {
System.out.println(score[i]);
}
}
}


以上有个接口

public interface Sorter {
/**
* 排序
*
* @param list
*            待排序的数组
*/
public <T extends Comparable<T>> void sort(T[] list);

/**
* 排序
*
* @param list
*            待排序的数组
* @param comp
*            比较两个对象的比较器
*/
public <T> void sort(T[] list, Comparator<T> comp);
}


还有其他方法 可以参考下

public class Sort {

public static void main(String[] args) {
mao1();
}

public static void mao() {
int score[] = { 67, 69,11,98, 75, 87, 89, 10, 99, 100 };
for (int i = 0; i < score.length - 1; i++) { // 最多做n-1趟排序
// 对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)
for (int j = 0; j < score.length - i - 1; j++) {
if (score[j] > score[j + 1]) { // 把小的值交换到后面
int temp = score[j];
score[j] = score[j + 1];
score[j + 1] = temp;
}
}
System.out.print("第" + (i + 1) + "次排序结果:");
for (int a = 0; a < score.length; a++) {
System.out.print(score[a] + "\t");
}
System.out.println("");
}
System.out.print("最终排序结果:");
for (int a = 0; a < score.length; a++) {
System.out.print(score[a] + "\t");
}
}

public static void mao1() {
int score[] = { 67, 69,11,98, 75, 87, 89, 10, 99, 100 };
for (int i = 0; i < score.length - 1; i++) { // 最多做n-1趟排序
for (int j = i+1; j < score.length; j++) {
if (score[i] > score[j]) { // 把大的值交换到后面
int temp = score[j];
score[j] = score[i];
score[i] = temp;
}
}
System.out.print("第" + (i + 1) + "次排序结果:");
for (int a = 0; a < score.length; a++) {
System.out.print(score[a] + "\t");
}
System.out.println("");
}
System.out.print("最终排序结果:");
for (int a = 0; a < score.length; a++) {
System.out.print(score[a] + "\t");
}
}
}
mao的感觉好一点 直观 mao1是个人比较习惯用的一种
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: