标签:div amp 个数 port main i++ 基本原理 span 递归
归并排序的基本原理是,先将一个数组递归地二分,然后在分到一定程度之后将它们重新组合,并且在组合的过程中按顺序排列,达到排序的目的。
import java.util.* fun merge(a:Array<Int>, low:Int, mid:Int, high:Int){ var temp = IntArray(high-low+1) var i:Int = low var j:Int = mid + 1 var k:Int = 0
//合并过程 while( i<= mid && j<= high){ temp[k++] = if(a[i]< a[j]) a[i++] else a[j++] } while(i<= mid) temp[k++] = a[i++] while(j<= high) temp[k++] = a[j++] for(k2 in temp.indices) a[k2+ low] = temp[k2] } fun mergeSort(a:Array<Int>, low:Int, high:Int){ var mid:Int = (low+ high)/ 2 if( low< high){ mergeSort(a, low, mid) mergeSort(a, mid+1, high) merge(a, low, mid, high) } } fun main(args:Array<String>){ var a = arrayOf(37, 26, 12, 8, 63, 5, 9, 57, 60) mergeSort(a, 0, a.size-1) println("结果:${Arrays.toString(a)}") }
标签:div amp 个数 port main i++ 基本原理 span 递归
原文地址:https://www.cnblogs.com/jason31/p/9680683.html