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...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
I need to write a method that sorts a provided Linked list with bubble sort and...
I need to write a method that sorts a provided Linked list with bubble sort and using ONLY Java List Iterator Methods (Link Below) https://docs.oracle.com/javase/8/docs/api/java/util/ListIterator.html     public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception {     // first line to start you off     ListIterator<E> iit = c.listIterator(), jit;     /**** Test code: do not modify *****/     List cc = new LinkedList(c);     Collections.sort(c);     ListIterator it1 = c.listIterator(), it2 = cc.listIterator(); while (it1.hasNext()) { if (!it1.next().equals(it2.next()))         throw new Exception("List not sorted");...
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.
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT