快速排序
public class FastSort {
public static void main(String[] args) {
int [] array=new int [10000000];
for (int i=0;i<10000000;i++){
array[i]= (int) (1+Math.random()*10000);
}
long starTime=System.nanoTime();
fastpre(array);
long endTime=System.nanoTime();
long Time=endTime-starTime;
System.out.println("快速排序千万随机数:"+Time/1000000.0+"毫秒");
}
public static void fastsort(int [] array,int start,int end){
int key=array[start];
int temp=0;
int starttemp=start;
int endtemp=end;
while (start<end){
boolean flag1=true;
boolean flag2=true;
while (flag1){
if (array[end]<=key){
temp=array[end];
array[end]=array[start];
array[start]=temp;
start++;
flag1=false;
}else {
end--;
}
}
while (flag2){
if (array[start]>=key){
temp=array[end];
array[end]=array[start];
array[start]=temp;
end--;
flag2=false;
}else {
start++;
}
}
}
if (starttemp<start-1){
fastsort(array,starttemp,start-1);
}
if (end+1<endtemp){
fastsort(array,end+1,endtemp);
}
}
}
归并排序
public class MergeSort {
public static void main(String[] args) {
int [] array=new int [1000000];
for (int i=0;i<1000000;i++){
array[i]= (int) (1+Math.random()*10000000);
}
long starTime=System.nanoTime();
Sort(array, 0, array.length - 1);
long endTime=System.nanoTime();
long Time=endTime-starTime;
System.out.println("归并排序百万随机数:"+Time/1000000.0+"毫秒");
}
public static int[] Sort(int[] array,int start,int end) {
if (start==end){
return new int []{array[start]};
}
int mid=(start+end)/2;
int [] l=Sort(array,start,mid);
int [] r=Sort(array,mid+1,end);
return merge(l,r);
}
private static int[] merge(int[] l, int[] r) {
int [] arr=new int [l.length+r.length];
int k=0;
int i=0;
int j=0;
while (i<l.length&&j<r.length){
if(l[i]<r[j]){
arr[k++]=l[i];
i++;
}
else{
arr[k++] = r[j];
j++;
}
}
while (i<l.length){
arr[k++]=l[i++];
}
while (j<r.length){
arr[k++]=r[j++];
}
return arr;
}
}
基数排序
public class radixsort {
public static void main(String[] args) {
int [] array=new int [1000000];
for (int i=0;i<1000000;i++){
array[i]= (int) (1+Math.random()*10000000);
}
long starTime=System.nanoTime();
function(array);
long endTime=System.nanoTime();
long Time=endTime-starTime;
System.out.println("基数排序百万随机数:"+Time/1000000.0+"毫秒");
}
public static void function(int [] array){
int [][] arr=new int [10][array.length];
int max=0;
for (int i=0;i<array.length;i++){
if (array[i]>max)max=array[i];
}
int len=String.valueOf(max).length();
for (int w=0;w<len;w++){
int [] arrlen=new int[10];
double pow = Math.pow(10, w);
for (int j = 0; j < array.length; j++) {
int l = (int) ((array[j]/pow) % 10);
arr[l][arrlen[l]++] = array[j];
}
int h = 0;
for (int k = 0; k < 10; k++) {
for (int n = 0; n < arrlen[k]; n++) {
array[h] = arr[k][n];
h++;
}
}
}
}
}
桶排序
public class BucketSort {
public static void main(String[] args) {
int [] array=new int [1000000];
for (int i=0;i<1000000;i++){
array[i]= (int) (1+Math.random()*10000);
}
long starTime=System.nanoTime();
Sort(array);
long endTime=System.nanoTime();
long Time=endTime-starTime;
System.out.println("桶排序百万随机数:"+Time/1000000.0+"毫秒");
}
public static void Sort(int [] array){
int [] arr=new int[array.length];
int max=array[0];
int min=array[0];
for (int i=1;i<array.length;i++){
max=max>array[i]?max:array[i];
min=min<array[i]?min:array[i];
}
ArrayList<ArrayList<Integer>> Buchet=new ArrayList<>((max-min)/array.length+1);
for (int j=0;j<(max-min)/array.length+1;j++){
Buchet.add(new ArrayList<>());
}
for (int j=0;j<array.length;j++){
int num=(array[j]-min)/array.length;
Buchet.get(num).add(array[j]);
}
for (int k=0;k<(max-min)/array.length+1;k++){
Collections.sort(Buchet.get(k));
}
int h=0;
for (int n=0;n<Buchet.size();n++){
for (int m=0;m<Buchet.get(n).size();m++){
arr[h++]=Buchet.get(n).get(m);
}
}
}
}
冒泡排序
public static void fastsort2(int [] array){
for (int i=0;i<array.length;i++){
for (int j=0;j<array.length-i;j++){
if (array[i]>array[j]){
int temp=array[j];
array[j]=array[i];
array[i]=temp;
}
}
}
}
插入排序
public void sort(int arr[])
13/04/2018 Page 234 of 283
{
for(int i =1; i<arr.length;i++)
{
int insertVal = arr[i];
int index = i-1;
while(index>=0&&insertVal<arr[index])
{
arr[index+1]=arr[index];
index--;
}
arr[index+1]=insertVal;
}
}
希尔排序
public class InsertSort {
public static void main(String[] args) {
int [] array=new int [10000000];
for (int i=0;i<10000000;i++){
array[i]= (int) (1+Math.random()*10000000);
}
long starTime=System.nanoTime();
int mid=array.length/2;
while(mid>0) {
Sort(array,mid);
mid=mid/2;
}
long endTime=System.nanoTime();
long Time=endTime-starTime;
System.out.println("希尔排序计算一千万随机数:"+Time/1000000.0+"毫秒");
}
public static void Sort(int [] array,int mid){
for (int i=mid;i<array.length;i++){
if (array[i]<array[i-mid]){
int temp=array[i];
int j=i;
while(j-mid>0&&temp<array[j-mid]){
array[j]=array[j-mid];
j=j-mid;
}
array[j]=temp;
}
}
}
}
堆排序
public static int[] heapSort(int[] array) {
for (int i = array.length / 2 - 1; i >= 0; i--) {
adjustHeap(array, i, array.length);
}
for (int j = array.length - 1; j > 0; j--) {
swap(array, 0, j);
adjustHeap(array, 0, j);
}
return array;
}
public static void adjustHeap(int[] array, int i, int length) {
int temp = array[i];
for (int k = 2 * i + 1; k < length; k = 2 * k + 1) {
if (k + 1 < length && array[k] < array[k + 1]) {
k++;
}
if (array[k] > temp) {
swap(array, i, k);
i = k;
} else {
break;
}
}
}
public static void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
二分查找
public static int biSearch(int []array,int a){
int lo=0;
int hi=array.length-1;
int mid;
while(lo<=hi){
mid=(lo+hi)/2;
if(array[mid]==a){
return mid+1;
}else if(array[mid]<a){
lo=mid+1;
}else{
hi=mid-1;
} }
return -1;
}