if (l < r) 
{ 
    int m = l+(r-l)/2; 
    merge_sort(arr, l, m); 
    merge_sort(arr, m+1, r); 

    merge(arr, l, m, r); 
} 

}

//6.快速排序:
int partition (int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high- 1; j++) 
{ 
    if (arr[j] <= pivot) 
    { 
        i++;   
        swap(&arr[i], &arr[j]); 
    } 
} 
swap(&arr[i + 1], &arr[high]); 
return (i + 1); 

}
void quick_sort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quick_sort(arr, low, pi - 1);
quick_sort(arr, pi + 1, high);
}
}

//7.堆排序:
void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2i + 1;
int r = 2
i + 2;

if (l < n && arr[l] > arr[largest]) 
    largest = l; 

if (r < n && arr[r] > arr[largest]) 
    largest = r; 

if (largest != i) 
{ 
    swap(arr[i], arr[largest]); 
    heapify(arr, n, largest); 
} 

}
void heap_sort(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i–)
heapify(arr, n, i);

for (int i=n-1; i>=0; i--) 
{ 
    swap(arr[0], arr[i]); 
    heapify(arr, i, 0); 
} 

}


#### 9. 请给出七大排序算法的Java代码。



//1. 冒泡排序:
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

//2. 选择排序:
public static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

//3. 插入排序:
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int value = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > value) {
arr[j + 1] = arr[j];
j–;
}
arr[j + 1] = value;
}
}

//4. 希尔排序:
public static void shellSort(int[] arr) {
int n = arr.length;
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int value = arr[i];
int j = i - gap;
while (j >= 0 && arr[j] > value) {
arr[j + gap] = arr[j];
j -= gap;
}
arr[j + gap] = value;
}
}
}

//5. 归并排序:
public static void mergeSort(int[] arr) {
int n = arr.length;
if (n < 2) {
return;
}
int mid = n / 2;
int[] left = new int[mid];
int[] right = new int[n - mid];
for (int i = 0; i < mid; i++) {
left[i] = arr[i];
}
for (int i = mid; i < n; i++) {
right[i - mid] = arr[i];
}
mergeSort(left);
mergeSort(right);
merge(arr, left, right);
}
public static void merge(int[] arr, int[] left, int[] right) {
int i = 0, j = 0, k = 0;
int leftLen = left.length;
int rightLen = right.length;
while (i < leftLen && j < rightLen) {
if (left[i] <= right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}
while (i < leftLen) {
arr[k++] = left[i++];
}
while (j < rightLen) {
arr[k++] = right[j++];
}
}

//6. 快速排序:
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
}
public static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}

//7. 堆排序:
public static void heapSort(int[] arr) {
int n = arr.length;
for (int i = n / 2 - 1; i >= 0; i–) {
heapify(arr, n, i);
}
for (int i = n - 1; i >= 0; i–) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
public static void heapify(int[] arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}


#### 10. 请给出十大排序算法的Golang代码。



//1. 冒泡排序:
func BubbleSort(arr []int) {
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr)-i-1; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
}

//2. 选择排序:
func SelectionSort(arr []int) {
for i := 0; i < len(arr); i++ {
minIndex := i
for j := i + 1; j < len(arr); j++ {
if arr[j] < arr[minIndex] {
minIndex = j
}
}
arr[i], arr[minIndex] = arr[minIndex], arr[i]
}
}

//3. 插入排序:
func InsertionSort(arr []int) {
for i := 1; i < len(arr); i++ {
for j := i; j > 0; j-- {
if arr[j] < arr[j-1] {
arr[j], arr[j-1] = arr[j-1], arr[j]
}
}
}
}

//4. 希尔排序:
func ShellSort(arr []int) {
n := len(arr)
h := 1
for h < n/3 {
h = 3*h + 1
}
for h >= 1 {
for i := h; i < n; i++ {
for j := i; j >= h && arr[j] < arr[j-h]; j -= h {
arr[j], arr[j-h] = arr[j-h], arr[j]
}
}
h /= 3
}
}

//5. 归并排序:
func MergeSort(arr []int) {
if len(arr) <= 1 {
return
}
mid := len(arr) / 2
left := arr[:mid]
right := arr[mid:]
MergeSort(left)
MergeSort(right)
i := 0
j := 0
k := 0
for i < len(left) && j < len(right) {
if left[i] < right[j] {
arr[k] = left[i]
i++
} else {
arr[k] = right[j]
j++
}
k++
}
for i < len(left) {
arr[k] = left[i]
i++
k++
}
for j < len(right) {
arr[k] = right[j]
j++
k++
}
}

//6. 快速排序:
func QuickSort(arr []int) {
if len(arr) <= 1 {
return
}
pivot := arr[0]
left := []int{}
right := []int{}
for i := 1; i < len(arr); i++ {
if arr[i] < pivot {
left = append(left, arr[i])
} else {
right = append(right, arr[i])
}
}
QuickSort(left)
QuickSort(right)
arr = append(append(left, pivot), right…)
}

//7. 堆排序:
func HeapSort(arr []int) {
n := len(arr)
for i := n/2 - 1; i >= 0; i-- {
heapify(arr, n, i)
}
for i := n - 1; i >= 0; i-- {
arr[0], arr[i] = arr[i], arr[0]
heapify(arr, i, 0)
}
}
func heapify(arr []int, n, i int) {
largest := i
l := 2i + 1
r := 2
i + 2
if l < n && arr[l] > arr[largest] {
largest = l
}
if r < n && arr[r] > arr[largest] {
largest = r
}
if largest != i {
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
}
}

//8. 计数排序:
func CountingSort(arr []int) {
max := arr[0]
for i := 1; i < len(arr); i++ {
if arr[i] > max {
max = arr[i]
}
}
count := make([]int, max+1)
for i := 0; i < len(arr); i++ {
count[arr[i]]++
}
k := 0
for i := 0; i <= max; i++ {
for j := 0; j < count[i]; j++ {
arr[k] = i
k++
}
}
}

//9. 桶排序:
func BucketSort(arr []int) {
n := len(arr)
buckets := make([][]int, n)
for i := 0; i < n; i++ {
index := arr[i] / n
buckets[index] = append(buckets[index], arr[i])
}
k := 0
for i := 0; i < n; i++ {
bucketLen := len(buckets[i])
if bucketLen > 0 {
InsertionSort(buckets[i])
copy(arr[k:], buckets[i])
k += bucketLen
}
}
}

//10. 基数排序:
func RadixSort(arr []int) {
max := arr[0]
for i := 1; i < len(arr); i++ {
if arr[i] > max {
max = arr[i]
}
}
for exp := 1; max/exp > 0; exp *= 10 {
countSort(arr, exp)
}
}
func countSort(arr []int, exp int) {
output := make([]int, len(arr))
count := make([]int, 10)
for i := 0; i < len(arr); i++ {
count(arr[i]/exp)%!++
}
for i := 1; i < 10; i++ {
count[i] += count[i-1]
}
for i := len(arr) - 1; i >= 0; i-- {
output[count(arr[i]/exp)%!-1] = arr[i]
count(arr[i]/exp)%!
}
copy(arr, output)
}


回答的篇幅够长吧,比CSDN的网页版强。但上面代码中的**基数排序**出现了(MISSING)错误代码,再来问一次:


**请用Golang写出基数排序的代码**



// 基数排序
func RadixSort(arr []int) []int {
// 获取最大值
max := arr[0]
for _, v := range arr {
if v > max {
max = v
}
}
// 计算最大值的位数
maxDigit := 0
for max != 0 {
max /= 10
maxDigit++
}
// 创建桶
buckets := make([][]int, 10)
// 将数据放入桶中
mod := 10
dev := 1
for i := 0; i < maxDigit; i++ {
for _, v := range arr {
bucketIndex := (v %!m(MISSING)od) / dev
buckets[bucketIndex] = append(buckets[bucketIndex], v)
}
// 将桶中的数据放回原数组
index := 0
for _, bucket := range buckets {
for _, v := range bucket {
arr[index] = v
index++
}
bucket = bucket[:0]
}
mod *= 10

Logo

更多推荐