package com.lppz.shop.sys;
import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
/**
* @author: 20014915
* @create: 2019-06-26
* @description: 描述
**/
public class TestHashMap {
@Test
public void test(){
try {
Random random = new Random();
int[] arr1 = new int[100000];
for (int i = 0; i < 100000; i++) {
arr1[i] = random.nextInt(100000);
}
long t1 = System.currentTimeMillis();
quickSort(arr1,0,arr1.length-1);
System.out.println(arr1);
System.out.println("快速排序耗时:"+(System.currentTimeMillis() - t1));
int[] arr2 = new int[100000];
for (int i = 0; i < 100000; i++) {
arr2[i] = random.nextInt(100000);
}
long t2 = System.currentTimeMillis();
sort(arr2);
System.out.println(arr2);
System.out.println("冒泡排序耗时:"+(System.currentTimeMillis() - t2));
} catch (Exception e) {
e.printStackTrace();
}
}
private void sort(int[] arr){
for (int i = 0;i<arr.length;i++){
int temp = arr[i];
for (int j =i;j < arr.length;j++){
if (arr[j]<arr[i]){
arr[i] = arr[j];
arr[j] = temp;
temp = arr[i];
}
}
}
}
private void quickSort(int [] arr,int left,int right){
int i = left,j = right;
int temp = arr[left];
int tempIndex = left;
while (i != j){
for (;j>i;j--){
if (arr[j] < temp){
arr[tempIndex] = arr[j];
arr[j] = temp;
tempIndex = j;
break;
}
}
for (;i<j;i++){
if (arr[i]>arr[tempIndex]){
arr[tempIndex] = arr[i];
arr[i] = temp;
tempIndex = i;
break;
}
}
}
if (i - left>1){
quickSort(arr,left,i);
}
if (right - j> 1 ){
quickSort(arr,j+1,right);
}
}
}
总结:
快速排序时间复杂度为nlogn,冒泡排序时间复杂度为n2,