数据结构-选择类排序

直接选择排序:

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]+"  ");
        }
    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值