Question

Implement Insertion Sort and collect data regarding number of key comparisons and data moves for a...

Implement Insertion Sort and collect data regarding number of key comparisons and data moves for a small list of 10 integer values. You must try a sorted list, a descending list, and a random list.

c++ please

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(){
   int size = 10;
   int* arr = new int(size);
   for(int i = 0;i<size;i++){
      arr[i] = rand()%100;
   }
   
   cout<<"Random array: ";
    for(int i = 0;i<size;i++){
       cout<<arr[i]<<" ";
   }
   cout<<endl;
    
   int i, j, key, swapCount = 0, comparisionCount = 0;
       for (i = 1; i < size; i++) 
    {  
        key = arr[i];  
        j = i - 1;
        while (j >= 0 && arr[j] < key) 
        {  
           swapCount++;
           arr[j + 1] = arr[j];  
            j = j - 1;  
            comparisionCount+=2;
        }  
        comparisionCount+=2;
        swapCount++;
        arr[j + 1] = key;  
    }  
    
    cout<<"Sorted array: ";
    for(i = 0;i<size;i++){
       cout<<arr[i]<<" ";
   }
   cout<<endl;
    cout<<"Number of comparisions: "<<comparisionCount<<endl;
   cout<<"Number of swaps: "<<swapCount<<endl;    
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Implement Insertion Sort and collect data regarding number of key comparisons and data moves for a...
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
  • c++ program that finds the total number of comparisons and total number of moves in insertion...

    c++ program that finds the total number of comparisons and total number of moves in insertion sort, bubble sort, selection sort, shell sort, quick sort and merge sort using the same array of numbers.

  • When you look closely at the Insertion Sort you will notice that the insertion of the...

    When you look closely at the Insertion Sort you will notice that the insertion of the next element into the already sorted list basically does a sequential search to determine where the element should be inserted. Consider a variation of the standard Insertion Sort algorithm that does a binary search on the already sorted part of the list to determine the location where the element should be inserted. Assuming that the keys are unique, note that a standard binary search...

  • Recall the insertion sort algorithm as discussed in this chapter. Assume the following list of keys:...

    Recall the insertion sort algorithm as discussed in this chapter. Assume the following list of keys: 30, 20, 35, 27, 96, 82, 60, 48, 75, 5, 80 Exactly how many key comparisons are executed to sort this list using insertion sort? Show the values and the number of comparisons after each iteration for the loop.

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • MIPS insertion sort program......could really use some assistance

    Write a program in MIPS assembly language that implements the DESCENDING insertion sort algorithm to sort a variable-sized array of signed 32-bit integers (words)that are read from the console. Be reminded that in a descending sort, the integers are sorted from the largest to the smallest. A “special value” 99999 will beused to signify the end of the input sequence. This value is not to be considered part of the input data set. However, any value greater than 99999 that...

  • In python - Implement a doubly linked circular linked list of Node objects called CircularDoublyLinkedList. The...

    In python - Implement a doubly linked circular linked list of Node objects called CircularDoublyLinkedList. The data of each Node in the list is an integer. You will collect measurements for: An already sorted linked list An already sorted linked list in descending order A linked list containing random data

  • Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30...

    Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30. What is an execution frame? What is an activation record? What is contained in it? 31. Write a recursive Java method that computes the factorial of a number 2. Linear search may require up to comparisons while binary search will only require roughly comparisons 33. Sort sorts a list of values by repetitively putting a particular value into its final, sorted,...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Implement a binary search function that takes an integer query key and determines whether it is...

    Implement a binary search function that takes an integer query key and determines whether it is in a sorted array of n integers. It should return the index of the key, if found, otherwise it should return a negative number. You should implement the function both iteratively and recursively to be sure you really understand the algorithm. Just for the fun of it, you might want to verify the O(log(n)) complexity of your function by counting the number of comparisons...

  • Implement quicksort and bucket sort. Use the code in your book to help; the partition in...

    Implement quicksort and bucket sort. Use the code in your book to help; the partition in quicksort is tricky. Make sure your implementations are correct — it is easy to gain some confidence in the correctness of your code by writing a program which creates arrays filled with random numbers, sorts them, and then checks that they are sorted. Then time your code (using clock() or similar methods) on both methods for arrays filled with random integers of the following...

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