用c语言编程编写归并排序算法,对n个整数升序排序。归并排序代码结构如下: void recordList::MergeSort(recordList &L){ MSort(L, L, 0, L.currentSize-1); } 编写堆排序函数,对n个整数降序排序。堆排序参考代码如下: void MaxHeap::HeapSort(){ //对表heap[0]到heap[n-1]进行排序,使得表中各个待排序记录按其关键字,非递减排序 for(int i=(CurrentSize-2)/2;i>=0;i--) FilterDown(i,CurrentSize-1); //建立初始大顶堆 for(int i=CurrentSize-1;i>=1;i--){ swap(heap[0],heap[i]); //交换元素 FilterDown(0,i-1); //重新构建最大堆 } } 编写main函数,输入n个整数,利用写好的归并排序算法和堆排序算法将其按升序和降序分别输出排序结果。(输入输出数据间用一个空格分隔) 输入样例格式为2行: 整数个数n n个整数 输出样例格式为2行: 排好序的升序序列 排好序的降序序列
时间: 2024-02-04 10:01:40 浏览: 71
以下是使用C语言编写归并排序和堆排序的代码:
归并排序:
```
#include <stdio.h>
void Merge(int A[], int TmpA[], int L, int R, int RightEnd)
{
int LeftEnd, NumElements, Tmp;
int i;
LeftEnd = R - 1;
Tmp = L;
NumElements = RightEnd - L + 1;
while (L <= LeftEnd && R <= RightEnd) {
if (A[L] <= A[R])
TmpA[Tmp++] = A[L++];
else
TmpA[Tmp++] = A[R++];
}
while (L <= LeftEnd)
TmpA[Tmp++] = A[L++];
while (R <= RightEnd)
TmpA[Tmp++] = A[R++];
for (i = 0; i < NumElements; i++, RightEnd--)
A[RightEnd] = TmpA[RightEnd];
}
void MSort(int A[], int TmpA[], int L, int RightEnd)
{
int Center;
if (L < RightEnd) {
Center = (L + RightEnd) / 2;
MSort(A, TmpA, L, Center);
MSort(A, TmpA, Center + 1, RightEnd);
Merge(A, TmpA, L, Center + 1, RightEnd);
}
}
void MergeSort(int A[], int N)
{
int *TmpA;
TmpA = (int *)malloc(N * sizeof(int));
if (TmpA != NULL) {
MSort(A, TmpA, 0, N - 1);
free(TmpA);
}
else
printf("Error: Out of space!!!\n");
}
int main()
{
int n, i;
int A[100];
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &A[i]);
MergeSort(A, n);
for (i = 0; i < n; i++)
printf("%d ", A[i]);
return 0;
}
```
堆排序:
```
#include <stdio.h>
void Swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void FilterDown(int *heap, int start, int end)
{
int i = start, j = 2 * i + 1;
int tmp = heap[i];
while (j <= end) {
if (j < end && heap[j] < heap[j + 1])
j++;
if (tmp >= heap[j])
break;
else {
heap[i] = heap[j];
i = j;
j = 2 * i + 1;
}
}
heap[i] = tmp;
}
void HeapSort(int *heap, int n)
{
int i;
for (i = n / 2 - 1; i >= 0; i--) {
FilterDown(heap, i, n - 1);
}
for (i = n - 1; i > 0; i--) {
Swap(&heap[0], &heap[i]);
FilterDown(heap, 0, i - 1);
}
}
int main()
{
int n, i;
int A[100];
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &A[i]);
HeapSort(A, n);
for (i = 0; i < n; i++)
printf("%d ", A[i]);
return 0;
}
```
根据题目要求,我们需要对以上代码进行一些修改,使其能够实现对输入的n个整数的升序和降序排序,并输出排序结果。
归并排序修改后的代码:
```
#include <stdio.h>
void Merge(int A[], int TmpA[], int L, int R, int RightEnd)
{
int LeftEnd, NumElements, Tmp;
int i;
LeftEnd = R - 1;
Tmp = L;
NumElements = RightEnd - L + 1;
while (L <= LeftEnd && R <= RightEnd) {
if (A[L] <= A[R])
TmpA[Tmp++] = A[L++];
else
TmpA[Tmp++] = A[R++];
}
while (L <= LeftEnd)
TmpA[Tmp++] = A[L++];
while (R <= RightEnd)
TmpA[Tmp++] = A[R++];
for (i = 0; i < NumElements; i++, RightEnd--)
A[RightEnd] = TmpA[RightEnd];
}
void MSort(int A[], int TmpA[], int L, int RightEnd)
{
int Center;
if (L < RightEnd) {
Center = (L + RightEnd) / 2;
MSort(A, TmpA, L, Center);
MSort(A, TmpA, Center + 1, RightEnd);
Merge(A, TmpA, L, Center + 1, RightEnd);
}
}
void MergeSort(int A[], int N)
{
int *TmpA;
TmpA = (int *)malloc(N * sizeof(int));
if (TmpA != NULL) {
MSort(A, TmpA, 0, N - 1);
free(TmpA);
}
else
printf("Error: Out of space!!!\n");
}
int main()
{
int n, i;
int A[100];
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &A[i]);
MergeSort(A, n);
for (i = 0; i < n; i++)
printf("%d ", A[i]);
printf("\n");
for (i = n - 1; i >= 0; i--)
printf("%d ", A[i]);
printf("\n");
return 0;
}
```
堆排序修改后的代码:
```
#include <stdio.h>
void Swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void FilterDown(int *heap, int start, int end)
{
int i = start, j = 2 * i + 1;
int tmp = heap[i];
while (j <= end) {
if (j < end && heap[j] < heap[j + 1])
j++;
if (tmp >= heap[j])
break;
else {
heap[i] = heap[j];
i = j;
j = 2 * i + 1;
}
}
heap[i] = tmp;
}
void HeapSort(int *heap, int n)
{
int i;
for (i = n / 2 - 1; i >= 0; i--) {
FilterDown(heap, i, n - 1);
}
for (i = n - 1; i > 0; i--) {
Swap(&heap[0], &heap[i]);
FilterDown(heap, 0, i - 1);
}
}
int main()
{
int n, i;
int A[100];
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &A[i]);
HeapSort(A, n);
for (i = 0; i < n; i++)
printf("%d ", A[i]);
printf("\n");
for (i = n - 1; i >= 0; i--)
printf("%d ", A[i]);
printf("\n");
return 0;
}
```
阅读全文