데이터 구조 - 선택 클래스 정렬

직접 정렬 선택:
import java.util.Random;

/**
 *       
 * @author longkun.wyb
 *
 */
public class SelectionSort {
	
	
	public static void main(String[] args) {
		int[] list = new int[101];
		Random rand = new Random();
		for(int i = 0 ; i < 100 ; ++i){
			list[i] = rand.nextInt(1000);
			System.out.print(list[i] + " ");
		}
		System.out.println("--------------------------------");
		display(list);
		for(int i = 0 ; i < 100 ; ++i){
			if(i % 10 == 0){
				System.out.println();
			}
			System.out.print(list[i]+ "  ");
		}
	}
	public static void display(int[] list){
		
		for(int i = 0 ; i < list.length; ++i){
			for(int j= 0; j < list.length; ++j){
				if(list[i] < list[j]){
					int tmp = list[i];
					list[i] = list[j];
					list[j] = tmp;
				}
			}
		}
	}
}

더미 정렬:
package sort;

import java.util.Random;

public class HeapSort {
    static int[] list = {0,1,6,999,333,25,89,46,8999};
    public static Random rand = new Random();
    public static int TOTAL = rand.nextInt(10001)+10;
    public static int RANDINT = rand.nextInt(10000)+1;
    public static int[] array;
    public static int[] init(){
        
        array =new int[TOTAL];
        for(int i = 0 ; i < TOTAL ; ++i){
            array[i] = rand.nextInt(RANDINT)+1;
        }
        return array;
    }
    /**
     *   
     * @param array
     * @param begin
     * @param len
     */
    public static void sift(int[] array ,int begin ,int len){
        int tmp = array[begin];
        int next = 2 * begin;
        boolean finished = false;
        int i = begin;
        while(next <= len && !finished){//      ,next<=len     i<=len
//            System.out.println("i:"+i);
            if( next < len && array[next] < array[next+1]){
                next = next + 1;
            }
            if(tmp >= array[next]){
                finished = true;
            }else{
                array[i] = array[next];//      
                i = next;
                next = 2 * i;
            }
            
        }
        array[i] = tmp;
//        System.out.println("----------------------------");
    }
    /**
     *    
     * @param array
     * @param len
     */
    public static void crt_heap(int[] array, int len){
        for(int i = len/2; i > 0 ; --i){
            sift(array,i,len);
        }
    }
    /**
     *       
     * @param array
     * @param len
     */
    public static void sort1(int[] array,int len){
        
        
//        crt_heap(array,len);

        for(int i = len ; i >= 2; --i){
//            System.out.println("   :"+array[1]);
            crt_heap(array,i);//      
            int tmp = array[1];
            array[1] = array[i];
            array[i] = tmp;
            
        }
        
    }
    public static void main(String[] args) {
        list = init();
        long start = System.currentTimeMillis();
        sort1(array,array.length-1);
        System.out.println("     :"+(System.currentTimeMillis()-start));
        for(int i = 1; i < 10;++i){
            System.out.print(list[i]+"  ");
        }
    }

}

좋은 웹페이지 즐겨찾기