堆排序与快速排序

在内排序中,堆排序与快速排序的速度较快,时间复杂度相对稳定,因此经常用于考察对于算法的熟练度,其思想也经常经过变形后运用于其他各种应用场景中。

1、堆排序

堆的数据结构是一种二叉树,一般规定父节点的值小于(或者大于)子节点的值,这样的堆称为小根堆(大根堆)。排好序的堆可以很容易的得到最小值(最大值)。

对于堆排序而言,最主要的操作是对堆的调整操作。堆的调整是指,将不满足小根堆(或大根堆)性质的节点元素,通过交换操作,使得节点逐步满足堆的性质。

调整原则是:从最后的非叶子节点开始(以小根堆为例),判断该节点与其子节点的值大小,若最小,不做操作,指针前移;若不是最小,与子节点中最小值交换。。。

排序一个数组的代码如下:

//堆排序
/*
 * printArry---------打印堆数组
 * exchangeElements--交换数组元素
 * heapSort----------堆排序
 * buildMaxHeap------建立初始堆
 * maxHeap-----------调整堆
 */
public class Heapsort {
	 public static void printArray(int[] array) {
		    System.out.print("{");
		    for (int i = 0; i < array.length; i++) {
			    System.out.print(array[i]);
			    if (i < array.length - 1) {
				    System.out.print(", ");
			    }
		    }
		    System.out.println("}");
	    }

	 public static void exchangeElements(int[] array, int index1, int index2) {
		    int temp = array[index1];
		    array[index1] = array[index2];
		    array[index2] = temp;
	    }
 

	 public static void main(String[] args) {
			int[] array = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3 };

			System.out.println("Before heap:");
			Heapsort.printArray(array);

			heapSort(array);

			System.out.println("After heap sort:");
			Heapsort.printArray(array);
		}

	public static void heapSort(int[] array) {
			if (array == null || array.length <= 1) {
				return;
			}

			buildMaxHeap(array);

			for (int i = array.length - 1; i >= 1; i--) {
				Heapsort.exchangeElements(array, 0, i);

				maxHeap(array, i, 0);
			}
		}

		private static void buildMaxHeap(int[] array) {
			if (array == null || array.length <= 1) {
				return;
			}

			int half = array.length / 2;
			for (int i = half; i >= 0; i--) {
				maxHeap(array, array.length, i);
			}
		}

		private static void maxHeap(int[] array, int heapSize, int index) {
			int left = index * 2 + 1;
			int right = index * 2 + 2;

			int largest = index;
			if (left < heapSize && array[left] > array[index]) {
				largest = left;
			}

			if (right < heapSize && array[right] > array[largest]) {
				largest = right;
			}

			if (index != largest) {
				Heapsort.exchangeElements(array, index, largest);

				maxHeap(array, heapSize, largest);
			}
		}
	
}
2、快读排序

快排是一种性能相对稳定的排序,但不能保证相同元素的相对位置在排序后不发生变化。

/*
 * 快排:首先选取一个数(可随机,可以指定),一次排列过程中确定这个数载数组中的位置(1次排列过程及编码应该熟悉!!!!)
 * 然后,按照分治思想分别排序该位置前面的数组和后面的数组,直到到达结束条件。
 * */
import java.util.Random;
public class QuickSort {
	public static void quicSort(int[]arry,int begin,int end){
		if(begin>=end||arry==null)return;
		int p=partition(arry,begin,end);
		quicSort(arry,begin,p-1);
		quicSort(arry,p+1,end);
	}
	public static int partition(int[]arry,int begin,int end){
		//采用随机数方法确定第一个选择被比较数。或者选定第一个元素或选定最后一个元素                   初始元素的选择会影响排序性能。
		Random random=new Random();
		if(end-begin<=0)return 0;
		int x=random.nextInt(end-begin);
		int index=begin+x;
		swap(arry,index,end);
		index=begin-1;
		for(int j=begin;j<end;j++){
			if(arry[j]<=arry[end]){
				index++;
				if(index!=j){
					swap(arry,index,j);
				}
			}
		}
		if((index+1)!=end){
			swap(arry,end,index+1);
		}
		return index+1;
	}
	public static void swap(int[]arry,int i,int j){
		int temp=0;
		temp=arry[i];
		arry[i]=arry[j];
		arry[j]=temp;
	}
	public static void main(String[]args){
		int a[]={7,10,4,4,2,9};
		int begin=0;
		int end=5;
		quicSort(a,begin,end);
		int i=0;
		while(i<=end){
			System.out.print(a[i]);
			System.out.print(',');
			i++;
		}
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值