- /**
- * 作者:于陈
- * 功能:演示各种排序
- * 时间:2012年5月5日 19:49:34
- */
- package com.paixu;
- import java.util.*;
- public class Demo1 {
- public static void main(String[] args) {
- int len = 50;
- int arr[] = new int[len];
- for (int i = 0; i < len; i++) {
- int t = (int) (Math.random() * 10000);
- arr[i] = t;
- }
- Boo boo = new Boo();
- Calendar car = Calendar.getInstance();
- System.out.println("排序前时间:" + car.getTime());
- boo.maopaoPaixu(arr);
- car = Calendar.getInstance();
- System.out.println("排序后时间:" + car.getTime());
- System.out.println();
- car = Calendar.getInstance();
- System.out.println("排序前时间:" + car.getTime());
- Selent selent = new Selent();
- selent.selentPaixu(arr);
- car = Calendar.getInstance();
- System.out.println("排序后时间:" + car.getTime());
- }
- }
- // 选择排序
- class Selent {
- public void selentPaixu(int arr[]) {
- int temp = 0;
- for (int i = 0; i < arr.length - 1; i++) {
- int min = arr[i];
- int minIndex = i;
- for (int j = i + 1; j < arr.length; j++) {
- if (min > arr[j]) {
- min = arr[j];
- minIndex = j;
- }
- }
- temp = arr[i];
- arr[i] = arr[minIndex];
- arr[minIndex] = temp;
- }
- }
- }
- // 冒泡排序
- class Boo {
- public void maopaoPaixu(int[] arr) {
- int temp = 0;
- for (int i = 0; i < arr.length - 1; i++) {
- for (int j = 0; j < arr.length - 1 - i; j++) {
- if (arr[j] > arr[j + 1]) {
- temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- }
- }
复制代码 我想简单的测试下各种排序算法的时间复杂度,但是用这个Calendar只能弄出精确到秒的系统时间,虽然也能实现功能,但是貌似不太方便,请问有没有能精确到毫秒的时间方法?怎么用的? |
|