Question

pleaase I want manual solution for Data Structures and Algorithm Analysis in C++, Third Edition By...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

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("\n Sorted Linked List is: \n");

printList(a);    

getchar();

return 0;

}

Output: Sorted Linked List is: 2 3 5 10 15 20

Merge sort using 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("\n");
}
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr)/sizeof(arr[0]);

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

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");
printArray(arr, arr_size);
return 0;
}

Output: gcc version 4.6.3 Given array is 12 11 13 5 6 7 Sorted array is 5 6 7 11 12 13

Add a comment
Know the answer?
Add Answer to:
pleaase I want manual solution for Data Structures and Algorithm Analysis in C++, Third Edition By...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout...

    Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...

  • C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use...

    C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation. Problem...

  • Do the following project: Following is the file to be programmed in Linux kernel. Run this...

    Do the following project: Following is the file to be programmed in Linux kernel. Run this program. Include the screenshot of the results. Multi threaded Sorting Application Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sub list using a sorting algorithm of your choice. The two sub lists are then merged by a third thread—a...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • CS 215 Program Design, Abstraction, and Problem Solving Programming Project #3 INTRODUCTION The goal of this...

    CS 215 Program Design, Abstraction, and Problem Solving Programming Project #3 INTRODUCTION The goal of this programming project is to enable the student to practice designing a program that solves a problem using a class and a linked-list and developing a C++ program to implement the solution. PROJECT TASKS 1. Read the problem definition below and then analyze it before designing a solution. You will produce a document (external documentation) of this definition, analysis, and design. 2. Write a C++...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT