def swap(array: Array[Int], i: Int, j:Int): Unit ={
val tmp = array(i)
array(i) = array(j)
array(j) = tmp
}
一. 冒泡
def bubblingSort(array: Array[Int]): Array[Int] ={
val arr = array
for(i <- 1 until arr.length){
for(j <- 0 until arr.length-i){
if(arr(j) > arr(j+1)) swap(arr, j, j+1)
}
}
arr
}
二.选择
def choiceSort(array: Array[Int]): Array[Int] ={
val arr = array
for(i <- 0 until arr.length){
var min = i
for(j <- i+1 until arr.length){
if(arr(j) < arr(min)) min = j
}
if(i != min) swap(arr, i, min)
}
arr
}
三.插入
def insertSort(array: Array[Int]): Array[Int] ={
val arr = array
for(i <- 1 until arr.length){
var tmp = arr(i)
var j = i - 1
while (j>=0 && tmp < arr(j)){
arr(j + 1) = arr(j)
j-=1
}
arr(j + 1) = tmp
}
arr
}
四.希尔
def shellSort(array: Array[Int]): Array[Int] = {
val arr = array
var d = arr.length
while (d >= 1) {
for (k <- 0 until d) {
var i = k + d
while (i < arr.length) {
var tmp = arr(i)
var j = i - d
while (j >= 0 && tmp < arr(j)) {
arr(j + d) = arr(j)
j -= d
}
arr(j + d) = tmp
i += d
}
}
d /= 2
}
arr
}
五.归并
def mergeSort(array: Array[Int]): Array[Int] = {
if (array == null || array.length <= 1) {
return array
}
val mid = array.length / 2
val left = java.util.Arrays.copyOfRange(array, 0, mid)
val right = java.util.Arrays.copyOfRange(array, mid, array.length)
merge(mergeSort(left), mergeSort(right))
}
def merge(left: Array[Int], right: Array[Int]): Array[Int] = {
val res = new Array[Int](left.length + right.length)
var index_left = 0
var index_right = 0
for (i <- 0 until res.length) {
if (index_left >= left.length) {
res(i) = right(index_right)
index_right += 1
} else if (index_right >= right.length) {
res(i) = left(index_left)
index_left += 1
} else if (left(index_left) < right(index_right)) {
res(i) = left(index_left)
index_left += 1
} else {
res(i) = right(index_right)
index_right += 1
}
}
res
}
六.快排
def quickSort(array: Array[Int]): Array[Int] = {
val arr = array
quick(arr, 0, arr.length-1)
arr
}
def quick(array: Array[Int], low:Int, hight:Int): Unit ={
var i = low
var j = hight
if(i > j){
return
}
var temp = array(i)
while (i < j){
while (i<j && temp<=array(j)){
j-=1
}
while (i<j && temp>=array(i)){
i+=1
}
if(i < j){
swap(array, i, j)
}
}
swap(array, low, i)
quick(array, low, j-1)
quick(array, i+1, hight)
}
七.计数
def countSort(array: Array[Int]): Array[Int] = {
val arr = array
val max = arr.max
val min = arr.min
val rateArray = new Array[Int](max - min + 1)
for (i <- 0 until arr.length) {
rateArray(arr(i) - min) += 1
}
var index = 0
var index_rate = 0
while (index_rate < rateArray.length) {
if (rateArray(index_rate) != 0) {
arr(index) = min + index_rate
index += 1
rateArray(index_rate) -= 1
} else {
index_rate += 1
}
}
arr
}
八.桶
def bucketSort(array: Array[Int]): Array[Int] = {
var arr = array
val max = arr.max
val min = arr.min
val nums = (max - min) / arr.length + 1
val buckets = new Array[ArrayBuffer[Int]](nums)
(0 until nums).foreach(i => buckets(i) = ArrayBuffer())
for (item <- arr) {
val n = (item - min) / arr.length
buckets(n) += item
}
(0 until nums).foreach(i => buckets(i) = buckets(i).sorted)
arr = buckets.flatten
arr
}
def bucketSort2(array: Array[Int]): Array[Int] = {
val arr = array
val max = arr.max
val min = arr.min
val nums = (max - min) / arr.length + 1
val buckets = new Array[ArrayBuffer[Int]](nums)
for (i <- 0 until nums) {
buckets(i) = new ArrayBuffer[Int]()
}
for (i <- 0 until arr.length) {
val n = (arr(i) - min) / arr.length
buckets(n) += (arr(i))
}
for (i <- 0 until buckets.size) {
buckets(i).sorted
}
var index = 0
for (i <- 0 until buckets.size) {
for (j <- 0 until buckets(i).size) {
arr(index) = buckets(i)(j)
index += 1
}
}
arr
}
九.基数
def radixSort(array: Array[Int]): Array[Int] = {
var arr = array
val max = arr.max
var exp = 1
while ((max / exp) > 0) {
val temp = new Array[Int](arr.length)
val buckets = new Array[Int](10)
for (value <- arr) {
buckets((value / exp) % 10) += 1
}
for (i <- 1 until 10) {
buckets(i) += buckets(i - 1)
}
for (i <- (0 until arr.length).reverse) {
temp(buckets((arr(i) / exp) % 10) - 1) = arr(i)
buckets((arr(i) / exp) % 10) -= 1
}
arr = temp
exp *= 10
}
arr
十.堆
def heapSort(array: Array[Int]): Array[Int] = {
val arr = array
var right = arr.length - 1;
while (right > 0) {
for (i <- (0 to (right - 1) / 2).reverse) {
if (i * 2 + 1 >= 0 && i * 2 + 1 <= right && arr(i * 2 + 1) > arr(i)) {
swap(arr, i * 2 + 1, i)
}
if (i * 2 + 2 >= 0 && i * 2 + 2 <= right && arr(i * 2 + 2) > arr(i)) {
swap(arr, i * 2 + 2, i)
}
}
swap(arr, 0, right)
right -= 1
}
arr
}