Question

In: Computer Science

WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...

WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME

In this lab, you will implement Heap Sort algorithm for the same inputs.

For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …, n; (4) 50 instances of n random numbers generated in the range of [1..n].

Note:

(1) You may have to repeat the algorithm many times, each time you need to initialize the array.

(2) Your running time should exclude the time for initialization.

(3) All measurement should be done in a single run, i.e. you do not need to run once for n=100, another time for n=200, etc

Solutions

Expert Solution



#include <iostream>
 
using namespace std;
 
// A function to heapify the array.
void MaxHeapify(int a[], int i, int n)
{
        int j, temp;
        temp = a[i];
        j = 2*i;
 
        while (j <= n)
        {
                if (j < n && a[j+1] > a[j])
                j = j+1;
                // Break if parent value is already greater than child value.
                if (temp > a[j])
                        break;
                // Switching value with the parent node if temp < a[j].
                else if (temp <= a[j])
                {
                        a[j/2] = a[j];
                        j = 2*j;
                }
        }
        a[j/2] = temp;
        return;
}
void HeapSort(int a[], int n)
{
        int i, temp;
        for (i = n; i >= 2; i--)
        {
                // Storing maximum value at the end.
                temp = a[i];
                a[i] = a[1];
                a[1] = temp;
                // Building max heap of remaining element.
                MaxHeapify(a, 1, i - 1);
        }
}
void Build_MaxHeap(int a[], int n)
{
        int i;
        for(i = n/2; i >= 1; i--)
                MaxHeapify(a, i, n);
}
int main()
{
        int n, i;
        cout<<"\nEnter the number of data element to be sorted: ";
        cin>>n;
        n++;
        int arr[n];
        for(i = 1; i < n; i++)
        {
                cout<<"Enter element "<<i<<": ";
                cin>>arr[i];
        }
        // Building max heap.
        Build_MaxHeap(arr, n-1);
        HeapSort(arr, n-1);
 
        // Printing the sorted data.
        cout<<"\nSorted Data ";
 
        for (i = 1; i < n; i++)
                cout<<"->"<<arr[i];
 
        return 0;
}

Output

Case 1:
 
Enter the number of data element to be sorted: 10
Enter element 1: 9
Enter element 2: 6
Enter element 3: 4
Enter element 4: 3
Enter element 5: 8
Enter element 6: 7
Enter element 7: 5
Enter element 8: 2
Enter element 9: 0
Enter element 10: 1
 
Sorted Data ->0->1->2->3->4->5->6->7->8->9

Code 2

// C++ program for implementation of Heap Sort 
#include <iostream> 
using namespace std; 

// To heapify a subtree rooted with node i which is 
// an index in arr[]. n is size of heap 
void heapify(int arr[], int n, int i) 
{ 
        int largest = i; // Initialize largest as root 
        int l = 2 * i + 1; // left = 2*i + 1 
        int r = 2 * i + 2; // right = 2*i + 2 

        // If left child is larger than root 
        if (l < n && arr[l] > arr[largest]) 
                largest = l; 

        // If right child is larger than largest so far 
        if (r < n && arr[r] > arr[largest]) 
                largest = r; 

        // If largest is not root 
        if (largest != i) { 
                swap(arr[i], arr[largest]); 

                // Recursively heapify the affected sub-tree 
                heapify(arr, n, largest); 
        } 
} 

// main function to do heap sort 
void heapSort(int arr[], int n) 
{ 
        // Build heap (rearrange array) 
        for (int i = n / 2 - 1; i >= 0; i--) 
                heapify(arr, n, i); 

        // One by one extract an element from heap 
        for (int i = n - 1; i >= 0; i--) { 
                // Move current root to end 
                swap(arr[0], arr[i]); 

                // call max heapify on the reduced heap 
                heapify(arr, i, 0); 
        } 
} 

/* A utility function to print array of size n */
void printArray(int arr[], int n) 
{ 
        for (int i = 0; i < n; ++i) 
                cout << arr[i] << " "; 
        cout << "\n"; 
} 

// Driver program 
int main() 
{ 
        int arr[] = { 12, 11, 13, 5, 6, 7 }; 
        int n = sizeof(arr) / sizeof(arr[0]); 

        heapSort(arr, n); 

        cout << "Sorted array is \n"; 
        printArray(arr, n); 
} 

Code 3

// Heap Sort in C++
  
  #include <iostream>
  using namespace std;
  
  void heapify(int arr[], int n, int i) {
    // Find largest among root, left child and right child
    int largest = i;
    int left = 2 * i + 1;
    int right = 2 * i + 2;
  
    if (left < n && arr[left] > arr[largest])
      largest = left;
  
    if (right < n && arr[right] > arr[largest])
      largest = right;
  
    // Swap and continue heapifying if root is not largest
    if (largest != i) {
      swap(arr[i], arr[largest]);
      heapify(arr, n, largest);
    }
  }
  
  // main function to do heap sort
  void heapSort(int arr[], int n) {
    // Build max heap
    for (int i = n / 2 - 1; i >= 0; i--)
      heapify(arr, n, i);
  
    // Heap sort
    for (int i = n - 1; i >= 0; i--) {
      swap(arr[0], arr[i]);
  
      // Heapify root element to get highest element at root again
      heapify(arr, i, 0);
    }
  }
  
  // Print an array
  void printArray(int arr[], int n) {
    for (int i = 0; i < n; ++i)
      cout << arr[i] << " ";
    cout << "\n";
  }
  
  // Driver code
  int main() {
    int arr[] = {1, 12, 9, 5, 6, 10};
    int n = sizeof(arr) / sizeof(arr[0]);
    heapSort(arr, n);
  
    cout << "Sorted array is \n";
    printArray(arr, n);
  }

Code 4

#include <iostream>
using namespace std;
  
// function to heapify the tree
void heapify(int arr[], int n, int root)
{
   int largest = root; // root is the largest element
   int l = 2*root + 1; // left = 2*root + 1
   int r = 2*root + 2; // right = 2*root + 2
  
   // If left child is larger than root
   if (l < n && arr[l] > arr[largest])
   largest = l;
  
   // If right child is larger than largest so far
   if (r < n && arr[r] > arr[largest])
   largest = r;
  
   // If largest is not root
   if (largest != root)
      {
      //swap root and largest
      swap(arr[root], arr[largest]);
  
      // Recursively heapify the sub-tree
      heapify(arr, n, largest);
      }
}
  
// implementing heap sort
void heapSort(int arr[], int n)
{
   // build heap
   for (int i = n / 2 - 1; i >= 0; i--)
   heapify(arr, n, i);
  
   // extracting elements from heap one by one
   for (int i=n-1; i>=0; i--)
   {
      // Move current root to end
      swap(arr[0], arr[i]);
  
      // again call max heapify on the reduced heap
      heapify(arr, i, 0);
   }
}
  
/* print contents of array - utility function */
void displayArray(int arr[], int n)
{
   for (int i=0; i<n; ++i)
   cout << arr[i] << " ";
   cout << "\n";
}
  
// main program
int main()
{
   int heap_arr[] = {4,17,3,12,9,6};
   int n = sizeof(heap_arr)/sizeof(heap_arr[0]);
   cout<<"Input array"<<endl;
   displayArray(heap_arr,n);
  
   heapSort(heap_arr, n);
  
   cout << "Sorted array"<<endl;
   displayArray(heap_arr, n);
}
Let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions.

Thank You!
===========================================================================

Related Solutions

WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
PLEASE DONT FORGET TO SOLVE THE ASSIGNMENT QUESTION MOST IMP: Ex1) Download the code from the...
PLEASE DONT FORGET TO SOLVE THE ASSIGNMENT QUESTION MOST IMP: Ex1) Download the code from the theory section, you will find zipped file contains the code of Singly linked list and Doubly linked list, in addition to a class Student. In Class SignlyLinkedList, 1. There is a method display, what does this method do? 2. In class Test, and in main method, create a singly linked list objet, test the methods of the list. 3. To class Singly linked list,...
Code in C++ please You are going to write a program for Computer test which will...
Code in C++ please You are going to write a program for Computer test which will read 10 multiple choice questions from a file, order them randomly and provide the test to the user. When the user done the program must give the user his final score
Computer Science: Data Structures and Algorithms Practice: Construct a C++ program that takes two SQUARE matrices,...
Computer Science: Data Structures and Algorithms Practice: Construct a C++ program that takes two SQUARE matrices, and multiplies them and produces a new matrix: Example: [Matrix A] * [Matrix B] = [Matrix C] (A 3x3 is the most preferred example to use) (The first two matrices, A and B can be fixed numbers, hard-coded into the program. as opposed to user input) Display Matrix C, also (cout etc.)
PLEASE DONT COPY FROM INTERNET PLAGIRISM IS PROHIBITED WRITE 2000 WORDS ON THE TOPIC. TOPIC: INTERNATIONAL...
PLEASE DONT COPY FROM INTERNET PLAGIRISM IS PROHIBITED WRITE 2000 WORDS ON THE TOPIC. TOPIC: INTERNATIONAL TRADE AND DYNAMIC OF UAE (REQUIRE 2000 WORDS) 1. Introduction of the topic 2. Review of literature 3. Data, methodology 4. Analysis & Findings of the study 5. Conclusion 6. Reference
Please, write this code in c++. Using iostream and cstring library. Write a function that will...
Please, write this code in c++. Using iostream and cstring library. Write a function that will delete all words in the given text that meets more that one time. Also note than WORD is sequence of letters sepereated by whitespace. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000 symbols with whitespaces. Each word is not longer than 30 symbols. Output: Formatted text. example: input: Can you can the can...
Please, write code in c++. Using iostream and cstring library Write a function that will find...
Please, write code in c++. Using iostream and cstring library Write a function that will find and return most recent word in the given text. The prototype of the function have to be the following void mostRecent(char *text,char *word) In char *word your function have to return the most recent word that occurce in the text. Your program have to be not case-sensitive(ignore case - "Can" and "CAN" are the same words) Also note than WORD is sequence of letters...
Please answer QC,D,E,F using the code provided at the end please. A. Write C code to...
Please answer QC,D,E,F using the code provided at the end please. A. Write C code to define a structure called date that has three fields day, month, year, all of which are ints. B. Write C code to define a structure called person that has fields for name (50 char array), dateOfBirth (date struct from Q1), address (200 char array), phoneNum (20 char array), and ID (int). C. Write a C program that uses the person structure from Q2, and...
using C program Assignment Write a computer program that converts a time provided in hours, minutes,...
using C program Assignment Write a computer program that converts a time provided in hours, minutes, and seconds to seconds Functional requirements Input MUST be specified in hours, minutes, and seconds MUST produce the same output as listed below in the sample run MUST correctly compute times Nonfunctional requirements MUST adhere to program template include below MUST compile without warnings and errors MUST follow the code template provided in this assignment MUST NOT change " int main() " function MUST...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT