Question

In: Computer Science

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.
- sample test for one function.
You are expected to:
1. Complete the function stubs needed to implement the
searches/sorts you can implement.

#include
#include
using namespace std;

//-------------------------------------------------------------------------
// Filename: test_XXXX.cpp
// Purpose: Implement one search or sort functions, and provide a main
// to test the function.
// NOTE: Write a separate program for each function.
// When bubble sort is written:
// complete BubbleSort function.
// complete main to call BubbleSort with various arrays.
// file name is: test_BubbleSort.cpp
// ---------------------------------------------------------------------------

// Utility: Swap values of two variables.
void Swap(int & x, int & y)
{
nt temp=x;
x=y;
y=temp;
}//Swap


// Utility: Display array values ON ONE LINE.
void ShowArray(int X[], int Xsize)
{

}//ShowArray


// Utility: Shift LEFT one position, starting at index PIVOT.

// Utility: Shift RIGHT one position, starting at index PIVOT.
// Note: This INCREMENTS the size of the array.
void ShiftRight(int A[], int & Asize, int PIVOTindex)
{
  
}//ShiftRight


// Utility: Shift LEFT one position, starting at index PIVOT.
// Note: This DECREMENTS size of the array.
void ShiftLeft(int A[], int & Asize, int PIVOTindex)
{

} //ShiftRight


// Utility: Return index of first value in array that is >= key.
int InsertionPoint(int key, int A[], int Asize)
{

} //InsertionPoint

// Bubble sort.
void BubbleSort(int A[], int Asize)
{
for(int i=0; i for(int j=i+1;j if(A[i]>A[j]){
swap(A[i],A[j]);
}
}
}
}//BubbleSort

// Selection sort.
void SelectionSort(int B[], int Bsize)
{

}//SelectionSort

// Insertion sort.
void InsertionSort(int B[], int Bsize)
{

}//InsertionSort

// Linear search.
bool LinearSearch(int key, int C[], int Csize)
{
{
for(int i=0; i if(C[i]==key)return true;
}
return false;
}//LinearSearch

// Binary Search (RECURSIVE).
bool BinarySearch(int key, int C[], int LOindex, int HIindex)
{

}//BinarySearch

//==================== MAIN BELOW ====================

int main()
{
int numbers[] ={56,43,87,99,12,45,23,58,93,23,36,41};
int size = sizeof(numbers)/sizeof(int);
cout<<"Unsorted Array: "< for(int i=0; i cout<   
}//main

Solutions

Expert Solution

// C++ program to implement InsertionSort and BinarySearch functions

#include <iostream>

using namespace std;

// Insertion sort.

void InsertionSort(int B[], int Bsize);

// Binary Search (RECURSIVE).

bool BinarySearch(int key, int C[], int LOindex, int HIindex);

int main()

{

       int numbers[] ={56,43,87,99,12,45,23,58,93,23,36,41};

       int size = sizeof(numbers)/sizeof(int);

       cout<<"Unsorted Array: "<<endl;

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

             cout<<numbers[i]<<" ";

       cout<<endl;

       InsertionSort(numbers,size);

       cout<<"Sorted Array: "<<endl;

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

             cout<<numbers[i]<<" ";

       cout<<endl;

       cout<<boolalpha<<BinarySearch(5,numbers,0,size-1)<<endl;

       cout<<boolalpha<<BinarySearch(41,numbers,0,size-1);

       return 0;

}

// Insertion sort.

void InsertionSort(int B[], int Bsize)

{

       int i,j;

       int key;

       for(i=1;i<Bsize;i++)

       {

             j = i-1;

             key = B[i];

             while((j>=0) && (B[j]>key))

             {

                    B[j+1] = B[j];

                    j--;

             }

             B[j+1] = key;

       }

}

// Binary Search (RECURSIVE).

bool BinarySearch(int key, int C[], int LOindex, int HIindex)

{

       if(LOindex > HIindex)

             return false;

       int mid = (int)((LOindex+HIindex)/2);

       if(C[mid] == key)

             return true;

       else if(C[mid] > key)

             return BinarySearch(key,C,LOindex,mid-1);

       else

             return BinarySearch(key,C,mid+1,HIindex);

}

//end of program

Output:


Related Solutions

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...
Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches...
Question 2 (Function Template) Write a template version of the iterative binary search algorithm that searches an array of arbitrary type for a given key. Declare and implement a class called Student that keeps the student id, name, and grade. Include a default constructor, the overloaded insertion (<<) operator and also the overloaded extraction operator (>>). Declare and implement another class called Book that keeps the book’s title, author, and price. Just like the Student class, Include in class Book...
Implement Library Sort in Java which is a version of Insertion Sort with gaps to speed...
Implement Library Sort in Java which is a version of Insertion Sort with gaps to speed up the computation. If the pseudocode in Wikipedia (https://en.wikipedia.org/wiki/Library_sort) is not enough, you can download the research paper from (https://arxiv. org/pdf/cs/0407003.pdf). Below is the algorithm and pseudo code. Implementation Algorithm Let us say we have an array of n elements. We choose the gap we intend to give. Then we would have a final array of size (1 + ε)n. The algorithm works in...
Implement Library Sort which is a version of Insertion Sort with gaps to speed up the...
Implement Library Sort which is a version of Insertion Sort with gaps to speed up the computation. If the pseudocode in Wikipedia (https://en.wikipedia.org/wiki/Library_sort) is not enough, you can download the research paper from (https://arxiv. org/pdf/cs/0407003.pdf).
ASAP! write a program including   binary-search and merge-sort in Python. You also need to  modify the code posted...
ASAP! write a program including   binary-search and merge-sort in Python. You also need to  modify the code posted and use your variable names and testArrays.  
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do his manually, i.e. step through the algorithm yourself without a computer. This question is related to data structure and algorithm in javascript (.js). Please give your answer keeping this in your mind.
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework....
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework. Evaluate its performance in terms of the numbers of comparisons and exchanges, and compare it to the performance of the two advanced sorting methods that you have previously implemented. Submit your report with detailed empirical results and a thorough explanation of these results. Which of the three advanced sorting method is the best choice for a) ordered data, b) data in reverse order, and...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT