Question

1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt...

1) You must implement a recursive Quicksort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order.

2)You must implement a recursive Mergesort algorithm that will read integers from the attached MyList.txt file. Your algorithm must sort the list(integers)in ascending order.

My List txt Values

7

3

4

1

4

4

9

9

4

8

4

5

3

9

2

3

7

0

6

4

4

5

0

1

9

2

1

7

4

7

8

7

8

3

6

3

5

9

7

3

7

8

8

2

5

9

1

2

7

2

0

1

7

5

4

3

05

9

2

0

7

8

9

8

4

8

2

9

2

2

1

1

5

7

5

7

5

8

7

3

2

7

8

0

1

5

1

7

6

9

2

9

6

3

9

2

6

0

5

8

9

7

5

3

5

4

2

4

5

7

7

6

9

7

1

9

4

2

9

1

1

6

4

6

5

7

3

5

1

6

8

5

9

3

5

Homework Answers

Answer #1

//IF YOU ARE SATISFIED WITH THE CODE, KINDLY LEAVE A LIKE, ELSE COMMENT TO CLEAR DOUBTS

//I have used the same input in the MyList.txt file

//Quicksort.cpp

//C++ program for QuickSort

#include<iostream>

#include<fstream>

using namespace std;

void swap(int *a, int *b)

{

    int t = *a;

    *a = *b;

    *b = t;

}

int partition(int arr[], int low, int high)

{

    int pivot = arr[high]; // pivot

    int i = (low - 1);     // Index of smaller element

    for (int j = low; j <= high - 1; j++)

    {

        // If current element is smaller than or

        // equal to pivot

        if (arr[j] <= pivot)

        {

            i++; // increment index of smaller element

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

        }

    }

    swap(&arr[i + 1], &arr[high]);

    return (i + 1);

}

void quickSort(int arr[], int low, int high)

{

    if (low < high)

    {

        int pi = partition(arr, low, high);

        quickSort(arr, low, pi - 1);

        quickSort(arr, pi + 1, high);

    }

}

/* Function to print an array */

void printArray(int arr[], int size)

{

    int i;

    for (i = 0; i < size; i++)

        cout<< arr[i]<<"  ";

    cout<<"\n";

}

// Driver program to test above functions

int main()

{

    ifstream inFile;

    inFile.open("MyList.txt");

    int x;

    int size = 0;

    while(inFile>>x){

        size++;

    }

    int arr[size];

    inFile.close();

    inFile.open("MyList.txt");

    int i = 0;

    while(inFile>>x){

        arr[i++] = x;

    }

    quickSort(arr, 0, size - 1);

    cout<<"\n\nSorted array is\n";

    printArray(arr, size);

    return 0;

}

//Mergesort.cpp

// C++ program for Merge Sort

#include <iostream>

#include<fstream>

using namespace std;

void merge(int arr[], int l, int m, int r)

{

    int n1 = m - l + 1;

    int n2 = r - m;

    int L[n1], R[n2];

    // Copy data to temp arrays L[] and R[]

    for (int i = 0; i < n1; i++)

        L[i] = arr[l + i];

    for (int j = 0; j < n2; j++)

        R[j] = arr[m + 1 + j];

    int i = 0;

    int j = 0;

    int 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;

        mergeSort(arr, l, m);

        mergeSort(arr, m + 1, r);

        merge(arr, l, m, r);

    }

}

void printArray(int A[], int size)

{

    for (int i = 0; i < size; i++)

        cout << A[i] << "  ";

}

// Driver code

int main()

{

    ifstream inFile;

    inFile.open("MyList.txt");

    int x;

    int size = 0;

    while (inFile >> x)

    {

        size++;

    }

    int arr[size];

    inFile.close();

    inFile.open("MyList.txt");

    int i = 0;

    while (inFile >> x)

    {

        arr[i++] = x;

    }

    mergeSort(arr, 0, size - 1);

    cout << "\nSorted array is \n";

    printArray(arr, size);

    return 0;

}

CODE OUTPUT:

Know the answer?
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
Similar Questions
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
The bubble sort algorithm discussed in class is used to sort the following sequence of integers:...
The bubble sort algorithm discussed in class is used to sort the following sequence of integers: 47 29 1 9 5 23 • How many passes must the algorithm perform to guarantee the entire sequence is sorted? • What is the list obtained after the first pass? • What is the list obtained after the third pass? • What is the list obtained after the final pass?
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]...
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] W X D T P N R Q K M E If the array was being sorted using the SHELL sort and the halving method, and sorting into ASCENDING order as demonstrated in the course content, list the letters in the resulting array, in order AFTER the FIRST pass. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
CAN YOU PLEASE ANSWER ALL THESE QUSTIONS 1.In Merge sort, how many additional recursive partitioning levels...
CAN YOU PLEASE ANSWER ALL THESE QUSTIONS 1.In Merge sort, how many additional recursive partitioning levels are required for a list of 64 elements compared to a list of 8 elements? a. 3 b. 9 c. 8 d. 6 2. Which function best represents the number of operations in the worst-case for the following code fragment? for (i = 0; i < N; ++i) {    if (numbers[i] % 2 == 1)       factor = 2.5 } a. f(N) = 6N2 b....
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
Give a recursive algorithm to solve the following recursive function.    f(0) = 0;    f(1)...
Give a recursive algorithm to solve the following recursive function.    f(0) = 0;    f(1) = 1; f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
This program is in C++, And please consider " sort pass #" for the output: Write...
This program is in C++, And please consider " sort pass #" for the output: Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT