package com.dw.test;
public class Maopaopaixu {
/**
* dw008算法
*/
private static int[] dwsort(int[] list) {
int out=0;
int in=0;
int ifc=0;
for (int i = 0; i < list.length; i++) {
//关键点
for (int j = i + 1; j < list.length; j++) {
if (list[i] > list[j]) {
int temp = list[i];
list[i] = list[j];
list[j] = temp;
ifc++;
}
in++;
}
out++;
}
System.out.println("\n外层循环了:"+out+"次,内层循环了:"+in+"次,"+"if判断了:"+ifc+"次,总共"+(out+in+ifc)+"次");
System.out.println();
return list;
}
/**
* 正宗的冒泡排序
*/
public static int[] bubbleSort(int[] list) {
int out=0;
int in=0;
int ifc=0;
int temp = 0;
for (int i = 0; i < list.length - 1; i++) {
//关键点
for (int j = 0; j < list.length - 1 - i; j++) {
if (list[j] > list[j + 1]) {
temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
ifc++;
}
in++;
}
out++;
}
System.out.println("\n外层循环了:"+out+"次,内层循环了:"+in+"次,"+"if判断了:"+ifc+"次,总共"+(out+in+ifc)+"次");
System.out.println();
return list;
}
/**
* 改进后的冒泡排序算法的实现:
*
* @param list
* 欲排序的数组
*/
public static int[] improvedBubbleSort(int[] list) {
int out=0;
int in=0;
int ifc=0;
boolean needNextPass = true;
for (int k = 1; k < list.length && needNextPass; k++) {
needNextPass = false;
//关键点
for (int i = 0; i < list.length - k; i++) {
if (list[i] > list[i + 1]) {
int temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
needNextPass = true;
ifc++;
}
in++;
}
out++;
}
System.out.println("\n外层循环了:"+out+"次,内层循环了:"+in+"次,"+"if判断了:"+ifc+"次,总共"+(out+in+ifc)+"次");
System.out.println();
return list;
}
/**
* 产生随机数方法
*
* @return
*/
private static int[] makeRandom() {
int[] array = new int[20];
for (int k = 0; k < array.length; k++) {
array[k] = (int) (Math.random() * 100);
}
return array;
}
/**
* 循环遍历
* @param list
*/
private static void forlist(int[] list) {
for (int bb = 0; bb < list.length; bb++) {
System.out.print(list[bb] + " ");
}
}
/**
* 测试main
* @param args
*/
public static void main(String[] args) {
//dw008算法
//int list[] = makeRandom();
//很多不同的数字时:
int list[]={2,34,21,34,64,23,64,63,63,12,68,98,74,39,87,48,28,12,8,0};
//很多相同的数字时
int listsam[]={94, 72,31, 96, 88, 71 ,52, 88, 71 ,52, 97, 97, 19 ,52, 88, 71 ,52, 97, 19 ,52, 33 ,50 ,99 ,28, 52};
System.out.println("dw008算法:");
System.out.println("循环前:");
forlist(list);
dwsort(list);
forlist(list);
System.out.println("\n");
// 正宗冒泡
//int list1[] = makeRandom();
//很多不同的数字时:
int list1[]={2,34,21,34,64,23,64,63,63,12,68,98,74,39,87,48,28,12,8,0};
//很多相同的数字时
int listsam1[]={94, 72,31, 96, 88, 71 ,52, 88, 71 ,52, 97, 97, 19 ,52, 88, 71 ,52, 97, 19 ,52, 33 ,50 ,99 ,28, 52};
System.out.println("正宗冒泡:");
System.out.println("循环前:");
forlist(list1);
bubbleSort(list1);
forlist(list1);
System.out.println("\n");
// 改进后的冒泡
//int list2[] = makeRandom();
//很多不同的数字时:
int list2[]={2,34,21,34,64,23,64,63,63,12,68,98,74,39,87,48,28,12,8,0};
//很多相同的数字时
int listsam2[]={94, 72,31, 96, 88, 71 ,52, 88, 71 ,52, 97, 97, 19 ,52, 88, 71 ,52, 97, 19 ,52, 33 ,50 ,99 ,28, 52};
System.out.println("改进后的冒泡:");
System.out.println("循环前:");
forlist(list2);
improvedBubbleSort(list2);
forlist(list2);
}
}