pleaase I want manual solution for Data Structures and Algor

pleaase I want manual solution for

Data Structures and Algorithm Analysis in C++, Third Edition

By Clifford A. Shaffer

because my question from this book.page270 ,question 7.4

The implementation for Mergesort given in Section 7.4 takes an array as input and sorts that
array. At the beginning of Section 7.4 there is a simple pseudocode implementation for sorting a
linked list using Mergesort. Implement both a linked list-based version of Mergesort and the
array-based version of Mergesort, and compare and analyze their running times.

Solution

Linked list:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node* SortedMerge(struct node* a, struct node* b);
void FrontBackSplit(struct node* source,
struct node** frontRef, struct node** backRef);
void MergeSort(struct node** headRef)
{
struct node* head = *headRef;
struct node* a;
struct node* b;
if ((head == NULL) || (head->next == NULL))
{
return;
}

FrontBackSplit(head, &a, &b);
MergeSort(&a);
MergeSort(&b);


*headRef = SortedMerge(a, b);
}


struct node* SortedMerge(struct node* a, struct node* b)
{
struct node* result = NULL;

if (a == NULL)
return(b);
else if (b==NULL)
return(a);

  
if (a->data <= b->data)
{
result = a;
result->next = SortedMerge(a->next, b);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
}
return(result);
}


void FrontBackSplit(struct node* source,
struct node** frontRef, struct node** backRef)
{
struct node* fast;
struct node* slow;
if (source==NULL || source->next==NULL)
{
*frontRef = source;
*backRef = NULL;
}
else
{
slow = source;
fast = source->next;


while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}


*frontRef = source;
*backRef = slow->next;
slow->next = NULL;
}
}

void printList(struct node *node)
{
while(node!=NULL)
{
printf(\"%d \", node->data);
node = node->next;
}
}

void push(struct node** head_ref, int new_data)
{
  
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
  
  
new_node->data = new_data;
  
  
new_node->next = (*head_ref);

(*head_ref) = new_node;
}
int main()
{

struct node* res = NULL;
struct node* a = NULL;
  


push(&a, 15);
push(&a, 10);
push(&a, 5);
push(&a, 20);
push(&a, 3);
push(&a, 2);

MergeSort(&a);
  
printf(\"\ Sorted Linked List is: \ \");
printList(a);   
  
getchar();
return 0;
}

Time complexity:o(nlogn)
Array :
#include<stdlib.h>
#include<stdio.h>


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

int L[n1], R[n2];


for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
  
i = 0; // Initial index of first subarray
j = 0;
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;

// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);

merge(arr, l, m, r);
}
}
void printArray(int A[], int size)
{
int i;
for (i=0; i < size; i++)
printf(\"%d \", A[i]);
printf(\"\ \");
}
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr)/sizeof(arr[0]);

printf(\"Given array is \ \");
printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf(\"\ Sorted array is \ \");
printArray(arr, arr_size);
return 0;
}
Time complexity:0(nlogn)

pleaase I want manual solution for Data Structures and Algorithm Analysis in C++, Third Edition By Clifford A. Shaffer because my question from this book.page27
pleaase I want manual solution for Data Structures and Algorithm Analysis in C++, Third Edition By Clifford A. Shaffer because my question from this book.page27
pleaase I want manual solution for Data Structures and Algorithm Analysis in C++, Third Edition By Clifford A. Shaffer because my question from this book.page27
pleaase I want manual solution for Data Structures and Algorithm Analysis in C++, Third Edition By Clifford A. Shaffer because my question from this book.page27

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site