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, …,...
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
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
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...
please write the code in C not c++, and not to use Atoi or parseint to...
please write the code in C not c++, and not to use Atoi or parseint to parse the string, Thank you. #include <stdio.h> #include <stdbool.h> /* * The isinteger() function examines the string given as its first * argument, and returns true if and only if the string represents a * well-formed integer. A well-formed integer consists only of an * optional leading - followed by one or more decimal digits. * Returns true if the given string represents an...
PLEASE DONT COPY FROM INTERNET PLAGIRISM IS PROHIBITED WRITE 2000 WORDS ON THE TOPIC. QUESTION 01:...
PLEASE DONT COPY FROM INTERNET PLAGIRISM IS PROHIBITED WRITE 2000 WORDS ON THE TOPIC. QUESTION 01: World Rankings of UAE and its Implications (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
I am running this code using C to count the words in char array, but somehow...
I am running this code using C to count the words in char array, but somehow it keeps counting the total characters + 1. Any solution to fix this problem? #include <stdio.h> int main() {    char input[1001];    int count =0;    fgets(input, 1001, stdin);    char *array = input;    while(*array != '\0')       {        if(*array == ' '){            array++;        }        else{        count++;        array++;...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who...
Please, write code in c++. Using iostream and cstring library. Your friend is the person who does not like any limitations in the life. And when you said to him that it is totally impossible to work with integer numbers bigger than 4 294 967 296 in C++ he blamed you in time-wasting during the university study.So to prove that you hadn't waste 2 months of your life studying C++ in university you have to solve this issue. Your task...
Write a code using c# Maximum Sub Array.
Write a code using c# Maximum Sub Array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT