各种排序

插入排序
package sort;
import static print.Print.*;
public class InsertSort {

	public static void main(String[] args){
		int[] data = {9,8,7,6,5,4,3,2,1,0};
		InsertSort insertSort = new InsertSort();
		long begin = System.currentTimeMillis();
		for (int i = 0 ; i < 100 ; i++){
			insertSort.sort(data);
		}
		long end = System.currentTimeMillis();
		printhh ("插入排序所用时间:"+(end-begin)/1000);
		printsz (data);
	}
	
	public void sort(int[] data){
		for (int i = 1 ; i < data.length ; i++)
			for (int j = i ; (j>0) && (data[j]<data[j-1]) ; j--)
				swap(data,j,j-1);
	}
	
	public void swap(int[] data , int i , int j){
		int temp = data[i];
		data[i] = data[j];
		data[j] = temp;
	}

}

冒泡排序
package sort;

import static print.Print.printhh;
import static print.Print.printsz;

public class BubbleSort {
	
	public static void main(String[] args){
		int[] data = {9,8,7,6,5,4,3,2,1,0};
		BubbleSort bubbleSort = new BubbleSort();
		long begin = System.currentTimeMillis();
		for (int i = 0 ; i < 100000000 ; i++){
			bubbleSort.sort(data);
		}
		long end = System.currentTimeMillis();
		printhh ("冒泡排序所用时间:"+(end-begin)/1000);
		printsz (data);
	}
	
	public void sort(int[] data){
		for(int i = 0 ; i < data.length ; i++)
			for (int j = data.length-1 ; j > i ; j--){
				if (data[j] < data[j-1])
					swap (data,j,j-1);
			}
	}
	
	public void swap(int[] data , int i , int j){
		int temp = data[i];
		data[i] = data[j];
		data[j] = temp;
	}

}


选择排序:
package sort;

import static print.Print.*;

public class SelectSort {
	
	public static void main(String[] args){
		int[] data = {9,8,7,6,5,4,3,2,1,0};
		SelectSort selectSort = new SelectSort();
		long begin = System.currentTimeMillis();
		for (int i = 0 ; i < 100000000 ; i++){
			selectSort.sort(data);
		}
		long end = System.currentTimeMillis();
		printhh ("选择排序所用时间:"+(end-begin)/1000);
		printsz (data);
	}
	
	public void sort(int[] data){
		for (int i =0 ; i < data.length ; i++){
			int lowIndex = i ;
			for (int j = data.length - 1 ; j > i ; j--){
				if (data[j] < data[lowIndex])
					lowIndex = j ;
			}
			swap(data,i,lowIndex);
		}
	}
	
	public void swap(int[] data , int i , int j){
		int temp = data[i];
		data[i] = data[j];
		data[j] = temp;
	}

}


希尔排序:
package sort;

import static print.Print.printhh;
import static print.Print.printsz;

public class ShellSort {

	public static void main(String[] args) {
		int[] data = {9,8,7,6,5,4,3,2,1,0};
		ShellSort shellSort = new ShellSort();
		long begin = System.currentTimeMillis();
		for (int i = 0 ; i < 100000000 ; i++){
			shellSort.sort(data);
		}
		long end = System.currentTimeMillis();
		printhh ("希尔排序所用时间:"+(end-begin)/1000);
		printsz (data);
	}
	
	public void sort(int[] data){
		for (int i = data.length/2 ; i > 0 ; i/=2)
			for (int j = 0 ; j < i ; j++)
				insertSort (data,j,i);
	}
	
	public void insertSort(int[] data , int start , int inc){
		for (int i = start + inc ; i < data.length ; i += inc)
			for (int j = i ; (j >= inc) && (data[j] < data[j-inc]) ; j -= inc)
				swap(data,j,j-inc);
	}
	
	public void swap(int[] data , int i , int j){
		int temp = data[i];
		data[i] = data[j];
		data[j] = temp;
	}
	
}

你可能感兴趣的:(J#)