Question

In: Computer Science

develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations,...

develop the Binary search with swap count: Use sorted data from insertion sort (part A) Permutations, Insertion Sort – implement algorithms Permutations (Johnson Trotter): {1, 2, 3, 4, 5}; Insertion Sort: {45, 24, 16, 92, 71, 69, 28} develop count of # data “insertions” and develop # of key compares against the following: 16, 77, 24, 92, 44

Solutions

Expert Solution

//C++ program

#include<iostream>
using namespace std;

void insertionSort(int a[],int n){
   for(int i=1;i<n;i++){
       int j=i-1,val=a[i];
       while(j>=0&&a[j]>val){
       a[j+1]=a[j];
       j--;}
       a[++j]=val;
   }
}

int binarySearch(int arr[], int l, int r, int key,int &count)
{
if (r >= l) { count++;
int mid = l + (r - l) / 2;
if (arr[mid] == key)
return mid;

if (arr[mid] > key)
return binarySearch(arr, l, mid - 1, key,count);
  
return binarySearch(arr, mid + 1, r, key,count);
}

return -1;
}

int main(){
   int arr[] = {45, 24, 16, 92, 71, 69, 28};
   int n = sizeof(arr)/sizeof(arr[0]);
   insertionSort(arr,n);
   int count;
  
   int keys[] = {16, 77, 24, 92, 44};
  
   for(int i=0;i<5;i++){
       count=0;
       int index = binarySearch(arr,0,n-1,keys[i],count);
      
       if(index!=-1){
           cout<<arr[i]<<" found at index : "<<index<<"\n";
       }
       else cout<<arr[i]<<" not found \n";
      
       cout<<"Total comparision count : "<<count<<"\n\n";
   }
   return 0;
}

//sample output


Related Solutions

C++ ONLY Binary Search and Selection Sort A binary search first requires a sort. Use a...
C++ ONLY Binary Search and Selection Sort A binary search first requires a sort. Use a selection sort to organize an array, then find the value using a binary search. Input the array, and a value to find. Output the sorted array and the value if contained in the array. /* * File: main.cpp * Author: * Created on: * Purpose: Binary Search */ //System Libraries #include <iostream> //Input/Output Library #include <cstdlib> //Random Functions #include <ctime> //Time Library using namespace...
The Binary Insertion Sort Algorithm is a variation of the Insertion Sort Algorithm that uses a...
The Binary Insertion Sort Algorithm is a variation of the Insertion Sort Algorithm that uses a binary search technique rather than a linear search technique to insert the ith element in the correct place among the previously sorted elements. (i) Express the Binary Insertion Sort Algorithm in pseudocode. (ii) Compare the number of comparisons of elements used by the Insertion Sort Algorithm and the Binary Insertion Sort Algorithm when sorting the list (7,4,3,8,1,5,4,2). (iii) Show that the Insertion Sort Algorithm...
NEED: INSERTION SORT AND BINARY SEARCH IMMEDIATE NEED: Implement 2 searches and 2 sorts, and write...
NEED: INSERTION SORT AND BINARY SEARCH IMMEDIATE NEED: Implement 2 searches and 2 sorts, and write test programs to convince someone that you did them right. DIRECTIONS: You have been provided: 1. Function stubs for searching and sorting algorithms: Utility functions: swap, shift right/left, show array, insertion point. Searching: linear seach, binary search Sorting: bubble sort, insertion sort, selection sort. 2. Main program providing a pattern for testing youour functions, including: - sample arrays, each providing a different test case....
The insertion sort algorithm updates its sorted array as each new data value is entered. It...
The insertion sort algorithm updates its sorted array as each new data value is entered. It is an in-place algorithm, in that the sorted array at each step writes over the array from the previous step. • Let us start with sorting in descending order (largest to smallest). The ascending order case follows as a simple modification to the descending case. • Assume that we have thus far read N values and these are sorted in correct order (largest to...
Data Structure and Algorithm: 1. You are asked to use insertion sort to sort a singly...
Data Structure and Algorithm: 1. You are asked to use insertion sort to sort a singly linked list in ascending order. Why is this a bad idea? How does insertion sort’s runtime complexity change if you were to use it to sort a linked list? 2. Your classmate thinks that input arrays to the merge operation of merge sort don’t need to be in sorted order. Is your classmate right or wrong? Why?
This needs to be in Java implementing Insertion Sort and displaying the floats sorted. Sample Output:...
This needs to be in Java implementing Insertion Sort and displaying the floats sorted. Sample Output: (green is user input) Please select one of the following: 1: Initialize a default array 2: To specify the max size of the array 3: Add value to the array 4: Display values in the array 5: Display the average of the values 6: Enter multiple values 7: Read values from file 8: Save values to file 9: Sort the array 10: To Exit...
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
Rank the algorithms from slowest to fastest. . Bubble Sort Linear Search Binary Search Shellsort
Code using C++ A binary search tree is a data structure designed for efficient item insertion,...
Code using C++ A binary search tree is a data structure designed for efficient item insertion, deletion, and retrieval. These 3 operations share an average run time complexity of O(log(n)). Such time complexities are guaranteed whenever you are working with a balanced binary search tree. However, if you have a tree that begins leaning heavily to either its left or right side, then you can expect the performances of the insertion, deletion, and retrieval operations to degrade. As an example,...
PYTHON Search for an element in a 2D sorted matrix using the binary search technique. Your...
PYTHON Search for an element in a 2D sorted matrix using the binary search technique. Your code should run in O(m + n) where m is the number of rows and n is the number of columns The python file should have a function named binarysearch The function should take two arguments. The first argument of the function will be the element to search for. Second argument of the function will be the matrix as a List[List]. The function should...
Part 2: Insertion Sort Q3. Arrange the following arrays in ascending order using Insertion sort. Show...
Part 2: Insertion Sort Q3. Arrange the following arrays in ascending order using Insertion sort. Show all steps. 7          11        2          9          5          14 Q4. State the number of comparisons for each pass. Pass # comparisons 1st 2nd 3rd 4th 5th
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT