Java实现 插入排序!!!

package net.okren.java.sort;
import java.util.*;
public class InsertSort {
	
	
	public static void insertSort(Comparable[] data){
		int j;
		for(int p = 1; p < data.length; p++){
			Comparable temp = data[p];
			for(j = p; j > 0 && temp.compareTo(data[j - 1]) < 0; j--){
				data[j] = data[j - 1];
			}
			data[j] = temp;
		}
	}
	
	public static void main(String[] args){
		Random r = new Random();
		Comparable[] data = new Comparable[10];
		for(int i = 0; i < data.length; i++){
			data[i] = r.nextInt(data.length);
		}
		
		for(int i = 0; i < data.length; i++)
			System.out.print(data[i]);
		
		insertSort(data);
		System.out.println();
		
		for(int i = 0; i < data.length; i++)
			System.out.print(data[i]);
	}

}

你可能感兴趣的:(java,.net,J#)