Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I've been trying to implement the pseudocode for the sorting algorithms: insertion, merge, heap and quick sort When I run the code below, only

Hello, I've been trying to implement the pseudocode for the sorting algorithms: insertion, merge, heap and quick sort

When I run the code below, only quicksort and insertion sort organize the array, however mergeSort and heapSort give me errors in the ouput window. I used the following pseudocodes to write the codes, you can use them to compare them with mine since they follow the same format.

I have been testing the codes in line 47, between the start time and stop time this project

please help me find the errors in these 2 functions so the array sorts , also can you write the comments so that i know what I did wrong, thank you for your help in advance

For MERGESORT:

image text in transcribed

image text in transcribed

For HEAPSORT I had trouble with HeapSize the most

image text in transcribed

image text in transcribed

image text in transcribed

#include

#include

#include

#include

#include

#include // for function swap

using namespace std;

void InsertionSort(int[], int);

void MergeSort(int[], int, int);

void HeapSort(int[], int);

void QuickSort(int[], int, int);

void MERGE(int[], int, int, int);

int PARTITION(int[], int, int);

void MAX_HEAPIFY(int[], int,int);

void BUILD_MAX_HEAP(int[], int);

int heapSize;

int main()

{

// this is how you make a random array

/*

a.This program randomly generates three integer arrays A1, A2, and A3 of sizes N = 10^3, 10^5, and 10^7

(or 10^6, if your computer cannot handle an integer array of size 107), in that order

b. run all four sorting algorithms and record the number of comparisons between array elements and actual elapsed times in appropriate time

units, for instance, milliseconds.

*/

int RandomArray1[100];

int RandomArray2[10000];

int RandomArray3[100000];

// this function generates random numbers using the clock

srand((unsigned)time(0));

for (int i = 0; i

{

// the first i will in RandomArray1

RandomArray1[i] = rand();

//cout

}

// InsertionSort(RandomArray1, 100); works good! sorts the array

//QuickSort(RandomArray1, 0, 99); works good! sorts the array

auto started = std::chrono::high_resolution_clock::now();

// test the function

MergeSort(RandomArray1, 0, 99);

auto done = std::chrono::high_resolution_clock::now();

cout (done - started).count()

for (int i = 0; i

{

cout

}

/*

for (int i = 0; i

{

RandomArray2[i] = rand();

}

for (int i = 0; i

{

RandomArray3[i] = rand();

}

*/

return 0;

}

void InsertionSort(int arr[], int n)

{

// n is the length of the arraty

auto started = std::chrono::high_resolution_clock::now();

/* Function to sort an array using insertion sort*/

int i, key, j;

for (j = 2; j

{

key = arr[j];

i = j - 1;

/* Move elements of arr[0..i-1], that are

greater than key, to one position ahead

of their current position */

while (i >= 0 && arr[i] > key)

{

arr[i + 1] = arr[i];

i = i - 1;

}

arr[i + 1] = key;

}

auto done = std::chrono::high_resolution_clock::now();

cout (done - started).count()

}

void MergeSort(int arr[], int p, int r)

{

int q;

if (p

{

q = (p + r) / 2;

MergeSort(arr, p, q);

MergeSort(arr, q + 1, r);

MERGE(arr, p, q, r);

}

}

// n is the length of the array

void HeapSort(int arr[], int n)

{

auto started = std::chrono::high_resolution_clock::now();

BUILD_MAX_HEAP(arr,n);

// this for loop shows decrement

for (int i = n; i > n; i--)

{

// 1 is the root of the heap

swap(arr[1], arr[i]);

heapSize = heapSize- 1;

MAX_HEAPIFY(arr, 1, heapSize);

}

auto done = std::chrono::high_resolution_clock::now();

cout (done - started).count()

}

void QuickSort(int arr[], int p, int r)

{

int q;

if (p

{

q = PARTITION(arr, p, r);

QuickSort(arr, p, q - 1);

QuickSort(arr, q + 1, r);

}

}

int PARTITION(int arr[], int p, int r)

{

int x = arr[r];

int i = p - 1;

for (int j = p; j

{

if (arr[j]

{

i = i + 1;

// do exchange

swap(arr[i], arr[j]);

}

}

// do exchange line 7 on pseudocode

swap(arr[i + 1], arr[r]);

return (i + 1);

}

void MERGE(int arr[], int p, int q, int r)

{

// pointers create array because value is changing

int* Larray;

int* Rarray;

int n1 = q - p + 1;

int n2 = r - q;

int i, j;

// assign new dynamic value, use pointers when u do not know the size

Larray = new int[n1 + 1];

Rarray = new int[n2 + 1];

// this is not a nested loop

for (int i = 1; i

Larray[i] = arr[p + i - 1];

for (int j = 1; j

Rarray[j] = arr[q + j];

Larray[n1 + 1] = INFINITY;

Rarray[n2 + 1] = INFINITY;

i = 1;

j = 1;

for (int k = p; k

{

if (Larray[i]

{

arr[k] = Larray[i];

i = i + 1;

}

else {

arr[k] = Rarray[j];

j = j + 1;

}

}

}

void BUILD_MAX_HEAP(int arr[], int n)

{

// n = length[A] on pseudo

int heapSize = n;

// loop is decrementing

for (int i = floor(n / 2); i >1 ; i--)

MAX_HEAPIFY(arr, i, heapSize);

}

void MAX_HEAPIFY(int arr[], int i,int heapSize)

{

int left = 2 * i + 1; // left child

int right = 2 * i + 2; // right child

int largest;

if (left arr[i])

{

largest = left;

}

else

{

largest = i;

}

if (right arr[largest])

{

largest = right;

}

if (largest != i)

{

swap(arr[i], arr[largest]);

MAX_HEAPIFY(arr, largest,heapSize);

}

}

MERGE-SORT(A, p, r) 1 if p

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logidata+ Deductive Databases With Complex Objects Lncs 701

Authors: Paolo Atzeni

1st Edition

354056974X, 978-3540569749

More Books

Students also viewed these Databases questions

Question

What is the difference between trade deficits and balance of trade

Answered: 1 week ago