使用 動態陣列 解決 非遞減陣列 歸併問題
阿新 • • 發佈:2018-12-13
文章目錄
題目
已知線性表La和Lb中的資料元素按值非遞減(非遞減就是遞增)有序排列,
現要求將La和Lb歸併為一個新的線性表Lc,且Lc中的資料元素仍按值非
遞減有序排列。
示例輸入:La = {3, 5, 8, 11};
Lb = {2, 6, 8, 9, 11, 15, 20};
示例輸出: Lc = {2, 3, 5, 6, 8, 8, 9, 11, 11, 15, 20};
程式碼
/**
* @author: Simon_z
已知線性表La和Lb中的資料元素按值非遞減(非遞減就是遞增)有序排列,現要求將La和Lb歸併為
一個新的線性表Lc,且Lc中的資料元素仍按值非遞減有序排列。
示例輸入:La = {3, 5, 8, 11};
Lb = {2, 6, 8, 9, 11, 15, 20};
示例輸出: Lc = {2, 3, 5, 6, 8, 8, 9, 11, 11, 15, 20};
*/
// 使用動態陣列分配La,Lb記憶體,需輸入La, Lb的length
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int *CreateDynamicArray(int *p, int arrayLength);
int Compare(int La_array[], int Lb_array[], int La_value, int Lb_value);
int main(int argv,char *argc[]){
int * La, *Lb;
int La_Length, Lb_Length, Lc_Length;
bool Perform;
//構造陣列La,Lb, Lc
printf("Please input La_Length: ");
scanf("%d", &La_Length);
printf("Please input La_array: ");
La = CreateDynamicArray(La, La_Length);
printf("Please input Lb_Length: ");
scanf("%d", & Lb_Length);
printf("Please input Lb_array: ");
Lb = CreateDynamicArray(Lb, Lb_Length);
//構造陣列Lc不需要手動輸入資料,所以直接在main方法裡構造即可,不需要呼叫
//*CreateDynamicArray方法
Lc_Length = La_Length + Lb_Length; //由於不需要去重所以Lc的長度即是La_Length + Lb_Length
int Lc[Lc_Length] = {0};
printf("初始化Lc\n");
for(int i = 0; i < Lc_Length; i++){
printf("%d ", Lc[i]);
}
if(!Lc){
printf("Array creation failed!\n");
}
//輸出插入前的La,Lb
printf("Brfore Merge: \n");
printf("La: ");
for (int i = 0; i < La_Length; i++){
printf("%d ", La[i]);
}
printf("\n");
printf("Lb: ");
for (int i = 0; i < Lb_Length; i++){
printf("%d ", Lb[i]);
}
//執行歸併演算法
printf("\n");
printf("After Merge: \n");
int La_Location = 0, Lb_Location = 0, Lc_Location = 0; //給La, Lb, Lc新增位置指標
while((La_Location <= La_Length) && (Lb_Location <= Lb_Length)){
printf("La_Location:%d La_Length:%d Lb_Location:%d Lb_Length:%d\n", La_Location, La_Length, Lb_Location, Lb_Length);
//判斷Compare方法的返回值,返回true插入Lb的元素,返回false插入La的元素
Perform = Compare(La, Lb, La_Location, Lb_Location);
for(int i = 0; i < Lc_Length; i++){
printf("%d ", Lc[i]);
}
printf("\n");
if (Perform == true){
Lc[Lc_Location] = Lb[Lb_Location];
/*指標下移*/
Lc_Location += 1;
Lb_Location += 1;
}else if (Perform == false){
Lc[Lc_Location] = La[La_Location];
/*指標下移*/
Lc_Location += 1;
La_Location += 1;
}
}
/*將剩餘不用比較的資料插入Lc*/
printf("La_Location:%d La_Length:%d", La_Location, La_Length);
if (La_Location > La_Length){
int i,j;
/*由於while裡主動將Lc指標下移, 所以Lc_Location - 1*/
for (i = Lb_Location, j = Lc_Location - 1; j < Lc_Length; i++, j++){
Lc[j] = Lb[i];
}
}else{
int i,j;
/*由於while裡主動將Lc指標下移, 所以Lc_Location - 1*/
for (i = La_Location, j = Lc_Location - 1; j < Lc_Length; i++, j++){
Lc[j] = La[i];
}
}
//輸出插入後的Lc
printf("\nLc: ");
for (int i = 0; i < Lc_Length; i++){
printf("%d ", Lc[i]);
}
/*釋放記憶體*/
free(La);
free(Lb);
free(Lc);
return 0;
}
//返回頭指標即可
int *CreateDynamicArray(int *p, int arrayLength){
int c;
p = (int *)malloc(arrayLength*sizeof(int)); //在堆中開闢陣列記憶體
for(int i = 0;i < arrayLength; i++)
{
scanf("%d", &c);
p[i] = c;
}
return p;
}
//比較La和Lb的對應元素決定插入哪個陣列的元素
int Compare(int La_array[], int Lb_array[], int La_value, int Lb_value){
bool Judge = true;
if (La_array[La_value] > Lb_array[Lb_value]){
Judge = true;
}else{
Judge = false;
}
return Judge;
}