标签:time() index lin rand ++ i++ space white pre
package com.sl.ch02;
import java.util.*;
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
// int arr1[] = {3, 6, 5, 10, 4, 7, 1, 9, 8, 2};
// int arr1[] = {2,1,3};
// Bubble b1 = new Bubble();
// b1.sort(arr1);
int len = 10;
int arr1[] = new int[len];
for(int i=0;i<len;i++) {
arr1[i]=(int)(Math.random()*100);
}
// Calendar c1 = Calendar.getInstance();
// System.out.println("选择排序:排序前:"+c1.getTime());
// Select s1 = new Select();
// s1.sort(arr1);
InsertSort i1 = new InsertSort();
i1.sort(arr1);
// c1 = Calendar.getInstance();
// System.out.println("选择排序:排序后:"+c1.getTime());
//
// c1 = Calendar.getInstance();
// System.out.println("冒泡排序:排序前:"+c1.getTime());
// Bubble b1 = new Bubble();
// b1.sort(arr1);
// c1 = Calendar.getInstance();
// System.out.println("冒泡排序:排序后:"+c1.getTime());
}
}
class Bubble{
public void sort(int arr1[]) {
int tem = 0;
for(int i = 0; i<arr1.length-1; i++) {
for(int j = 0; j<arr1.length - 1 - i;j++) {
if(arr1[j]>arr1[j+1]) {
tem=arr1[j];
arr1[j] = arr1[j+1];
arr1[j+1]=tem;
}
}
}
// System.out.print("(冒泡排序)数组从小到大排序:");
//
// for(int i = 0; i<arr1.length; i++) {
// System.out.print(arr1[i]+" ");
// }
//
// System.out.println();
}
public void test(int a){
a=a+1;
}
}
class Select{
public void sort(int arr1[]) {
for(int i = 0; i<arr1.length-1; i++) {
int minNum = arr1[i];
int minIndex = i;
for(int j = i+1; j<arr1.length;j++) {
if(arr1[j]<minNum) {
minNum = arr1[j];
minIndex = j;
}
}
int tem = arr1[i];
arr1[i] = arr1[minIndex];
arr1[minIndex] = tem;
}
System.out.print("(选择排序)数组从小到大排序:");
for(int i = 0; i<arr1.length; i++) {
System.out.print(arr1[i]+" ");
}
System.out.println();
}
}
class InsertSort{
public void sort(int arr1[]) {
for(int i=1;i<arr1.length;i++) {
int insertValue = arr1[i];
int index = i-1;
while(index>=0&&insertValue<arr1[index]) {
arr1[index+1] = arr1[index];
index--;
}
arr1[index+1]=insertValue;
}
System.out.print("(插入排序)数组从小到大排序:");
for(int i = 0; i<arr1.length; i++) {
System.out.print(arr1[i]+" ");
}
System.out.println();
}
}
标签:time() index lin rand ++ i++ space white pre
原文地址:http://www.cnblogs.com/christine0000/p/7682666.html