JAVA排序算法:插入、选择、冒泡

来源:互联网 发布:lol视频教学软件 编辑:程序博客网 时间:2024/05/16 07:12
package com.bonree.test0901;

import java.util.ArrayList;
import java.util.List;

/*******************************************************************************
 * 版权信息:有限公司 Copyright: Copyright (c) 1907有限公司,Inc.All Rights Reserved.
 * BidPlanStructForm.java Created on Sep 1, 2011 
* @Author: <a href=mailto:linfenliang@126.com>linfenliang</a>
 * @Title: SortAlgorithm.java
 * @Package com.bonree.test0901 Description: Version: 1.0
 ******************************************************************************/
public class SortAlgorithm {
/*--------------------JAVA排序算法-------------------------*/
/**
* 概述:插入排序算法
* 思想:排序值列中的前2个值,并在必要时交换它们。 在相对于前2个值(有序的)的适当位置插入值列的第三个值。
* 然后,在相对于前3个值(有序的)的适当位置插入值列的第4个值。 每进行一次插入操作,有序子集中的数值个数将递增1。
* 重复该过程,直至值列中的所有值都按照次序排列为止。
* @Title: insertSort
* @param value
* @return int[]
* @user <a href=mailto:linfenliang@126.com>linfenliang</a>
*/
public static int[] insertSort(int[] value) {
int num = value.length;
int now;
int tempValue;
for (int i = 1; i < num; i++) {
now = i;
tempValue = value[i];
while (now > 0 && value[now - 1] <= tempValue) {
value[now] = value[now - 1];
now--;
}
value[now] = tempValue;

}
return value;
}

/**
* 概述:选择排序算法
* 思想:搜索整个值列,以找到最小值。 将该值与值列中第一个位置上的值进行交换。 搜索剩下的值列(第一个除外),以找到其中的最小值,
* 然后将其与值列中第二个位置上的值进行交换。对值列中的每个位置重复该过程。
* @Title: selectSort
* @param value
* @return int[]
* @user <a href=mailto:linfenliang@126.com>linfenliang</a>
*/
public static int[] selectSort(int[] value) {
int max;
int tempValue;
int num = value.length;
for (int i = 0; i < num - 1; i++) {
max = i;
for (int j = i + 1; j < value.length; j++)
if (value[j] > value[max])
max = j;

tempValue = value[i];
value[i] = value[max];
value[max] = tempValue;

}
return value;
}

/**
* 概述:冒泡排序算法
* 思想:搜索整个值列,比较相邻元素,如果两者的相对次序不对,则交换它们, 其结果是最大值“想水泡一样”移动到值列的最后一个位置上,
* 这也是它在最终完成排序的值列中合适的位置。 然后再次搜索值列,将第二大的值移动至倒数第二个位置上,重复该过程, 直至将所有元素移动到正确的位置上。
* @Title: bubbleSort
* @param value
* @return int[]
* @user <a href=mailto:linfenliang@126.com>linfenliang</a>
*/
public static int[] bubbleSort(int[] value) {
int tempValue;
int num = value.length;
for (int i = 1; i < num; i++) {
for (int j = 0; j < i; j++) {
if (value[i] > value[j]) {
tempValue = value[i];
value[i] = value[j];
value[j] = tempValue;
}
}

}
return value;
}

/**
* 概述:快速排序
* 思想:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,
* 然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。
* @Title: quickSort
* @param array
*            void
* @user <a href=mailto:linfenliang@126.com>linfenliang</a>
*/
public static int[] quickSort(int[] value) {
quickSort(value, 0, value.length - 1);
return value;
}

private static void quickSort(int[] value, int low, int high) {
if (low < high) {
int p = partition(value, low, high);
quickSort(value, low, p - 1);
quickSort(value, p + 1, high);
}

}
//分割
private static int partition(int[] value, int low, int high) {
int tempValue;
int s = value[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (value[j] < s) {
tempValue = value[++i];
value[i] = value[j];
value[j] = tempValue;
}
}
tempValue = value[++i];
value[i] = value[high];
value[high] = tempValue;
return i;
}

/**
* 概述:test
* @Title: main
* @param args
*            void
* @user <a href=mailto:linfenliang@126.com>linfenliang</a>
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> l = new ArrayList<String>();
int[] a = { 
1, 9, 2, 8, 3, 7, 4, 0, 5, 6, 5, 3, 2, 7, 2};
int[] a1 = new int[a.length];
for(int i=0;i<a.length;i++)
a1[i] = a[i];
int[] a2 = new int[a.length];
for(int i=0;i<a.length;i++)
a2[i] = a[i];
int[] a3 = new int[a.length];
for(int i=0;i<a.length;i++)
a3[i] = a[i];
int[] a4 = new int[a.length];
for(int i=0;i<a.length;i++)
a4[i] = a[i];
double t1 = System.nanoTime();
int[] b = insertSort(a1);
double t2 = System.nanoTime();
int[] c = selectSort(a2);
double t3 = System.nanoTime();
int[] d = bubbleSort(a3);
double t4 = System.nanoTime();
int[] e = quickSort(a4);
double t5 = System.nanoTime();
for (int t = 0; t < a.length; t++)
System.out.print(a[t] + "  ");
System.out.println("原始数据");
for (int t = 0; t < b.length; t++)
System.out.print(b[t] + "  ");
System.out.println("插入排序"+(t2-t1));
for (int t = 0; t < c.length; t++)
System.out.print(c[t] + "  ");
System.out.println("选择排序"+(t3-t2));
for (int t = 0; t < d.length; t++)
System.out.print(d[t] + "  ");
System.out.println("冒泡排序"+(t4-t3));
for (int t = 0; t < e.length; t++)
System.out.print(e[t] + "  ");
System.out.println("快速排序"+(t5-t4));
}

}

0 0
原创粉丝点击