排序算法

代码采用scala实现

插入排序

def insertSort(array: Array[Int]): Unit ={
	    for(i <- 1 until array.length){
		    val value = array(i)
		    var j = i
		    while(j>0 && array(j-1)>value){
			    array(j) = array(j-1)
			    j -=1
		    }
		    array(j) = value
	    }
    }

 

冒泡排序

def bubbleSort(array: Array[Int]): Unit ={
		for(i<- 0 until array.length-1; j<- (i+1 until array.length).reverse if(array(j)<array(j-1)))
			Util.swapElementsOfIndexIAndJInArray(array, j, j-1)
	}

选择排序

def selectSort(array: Array[Int]) {
		for (i <- 0 until array.length - 1) {
			var minIndex = i
			for (j <- i + 1 until array.length) {
				if (array(j) < array(minIndex)) minIndex = j
			}
			if (minIndex != i) Util.swapElementsOfIndexIAndJInArray(array, i, minIndex)
		}
	}
 

计数排序

def countSort(array: Array[Int], upper:Int): Unit ={
		val cntArray = new Array[Int](upper)
		for(element<-array) cntArray(element) +=1
		var i=0
		for(j<- 0 until cntArray.length){
			for(k<- 0 until cntArray(j)){
				array(i) = j
				i += 1
			}
		}
	}

 

堆排序

def heapSort(array: Array[Int]): Unit ={
		buildMaxHeap(array)
		for(i<-(0 until array.length).reverse){
			Util.swapElementsOfIndexIAndJInArray(array, 0, i)
			maxHeapify(array, 0, i)
		}
	}

	def buildMaxHeap(array: Array[Int]): Unit ={
		val mid = (Math.floor(array.length-1)/2).toInt
		for(i<-(0 to mid).reverse) maxHeapify(array, i, array.length)
	}

	def maxHeapify(array: Array[Int], root:Int, end: Int): Unit ={//不包括end
		var large = root
		if(root*2+1<end && array(2*root+1)>array(large)) large = root*2+1
		if(root*2+2<end && array(2*root+2)>array(large)) large = root*2+2
		if(large!=root){
			Util.swapElementsOfIndexIAndJInArray(array, root, large)
			maxHeapify(array, large, end)
		}
	}
 

归并排序

def mergeSort(array: Array[Int], start:Int, end:Int): Unit ={
		if(start<end){
			val middle = (start+end)/2
			mergeSort(array, start,middle)
			mergeSort(array, middle+1, end)
			merge(array, start, middle, end)
		}

	}

	def merge(array: Array[Int], start:Int, middle:Int, end:Int): Unit ={
		val leftArray = new Array[Int](middle-start+1)
		val rightArray = new Array[Int](end-middle)
		for(i<-start to middle) leftArray(i-start) = array(i)
		for(i<- middle+1 to end) rightArray(i-middle-1) =array(i)
		var k = start
		var i = 0
		var j = 0
		while(i<leftArray.length && j<rightArray.length){
			if(leftArray(i)<=rightArray(j)) {
				array(k) = leftArray(i)
				i+=1
			} else {
				array(k) = rightArray(j)
				j+=1
			}
			k += 1
		}
		if(i<leftArray.length){
			for(p<-i until leftArray.length){
				array(k) = leftArray(p)
				k +=1
			}
		}
		if(j<rightArray.length){
			for(p<-j until rightArray.length){
				array(k) = rightArray(p)
				k+=1
			}
		}
	}

快速排序

def quickSort(array: Array[Int], start:Int, end:Int): Unit ={
		if (start < end) {
			val splitPosition = partition(array, start, end)
			quickSort(array, start, splitPosition-1)
			quickSort(array, splitPosition+1, end)
		}
	}

	def partition(array: Array[Int],start:Int,end:Int):Int ={
		val value = array(end)
		var i = start-1
		for(j<- start until end){
			if(array(j)<=value){
				i+=1
				Util.swapElementsOfIndexIAndJInArray(array, i, j)
			}
		}
		Util.swapElementsOfIndexIAndJInArray(array, i+1, end)
		i+1
	}

 

希尔排序

def shellSort(array: Array[Int]): Unit = {
		def innerSort(h: Int): Unit = {
			if (h > 0) {
				for (i <- h until array.length) {
					val tmp = array(i)
					var j = i - h
					while (j >= 0 && array(j)>tmp) {
						array(j + h) = array(j)
						j -= h
					}
					array(j + h) = tmp
				}
				innerSort(h / 2)
			}
		}
		innerSort(array.length / 2)
	}

 

 
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值