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排序算法-------------------------*/
/**
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;
}
/**
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;
}
/**
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;
}
/**
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;
}
/**
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));
}
}