## 归并排序 原

老汉-憨憨

``````#include <stdio.h>
#include <stdlib.h>

void printArr(int arr[], int len)
{
int i;
for (i = 0; i < len; i++) {
printf("%d\t", arr[i]);
}
printf("\n");
}

void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;

int L[n1], R[n2];
int i = 0, j = 0;

for (i = 0; i < n1; i++) {
L[i] = arr[l + i];
}

for (j = 0; j < n2; j++) {
R[j] = arr[m + j + 1];
}

i = j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1)  {
arr[k] = L[i];
i++;
k++;
}

while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int l, int r)
{
if (l < r) {
int m = l + (r-l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

int main(int argc, char *argv[])
{
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int len = sizeof(arr)/sizeof(int);

printf("Raw Arr[] = \t");
printArr(arr, len);

mergeSort(arr, 0, len - 1);

printf("Sort Arr[] = \t");
printArr(arr, len);
return 0;
}``````

``````Raw Arr[] = 	38	27	43	3	9	82	10
Sort Arr[] = 	3	9	10	27	38	43	82	``````

### 老汉-憨憨

Lubby
2015/10/08
102
0
java泛型中使用的排序算法——归并排序及分析

9龙
04/29
0
0

dby_freedom
02/26
0
0

2016/04/07
87
0

2012/12/10
61
0

PostgreSQL 11.3 locking

rudi

5
0
Mybatis Plus sql注入器

11
1

4
0

9
0

81
0