Question

In: Computer Science

Create a List object that uses the binary search algorithm to search for the string "A"....

Create a List object that uses the binary search algorithm to search for the string "A". Display a message box indicating whether the value was found.


Language: C#

Solutions

Expert Solution

The Collection classes are a group of classes designed specifically for grouping together objects and performing tasks on them. List class is a collection and defined in the System.Collections.Generic namespace and it provides the methods and properties like other Collection classes such as add, insert, remove, search etc. It's the replacement for arrays, linked lists, queues, and most other one-dimensional data structures. This is because it has all kinds of extra functionality, including the ability to grow in size on-demand .

The C# List class represents a strongly typed list of objects that can be accessed by index and it supports storing values of a specific type without casting to or from object.

Step 1

Binary search algorithm:

A binary search algorithm is an algorithm that is more efficient than a sequential search algorithm.

  • It requires the elements in the array should be arranged in ascending order.
  • The searching process starts in the middle of the array.
  • In the searching process, the algorithm finds an item or eliminates half of the array in each comparison.
  • If the search item is less than the middle item, then it eliminates the second half of the array and it continues to search in the first half of the array.
  • If the search item is greater than the middle item, then it eliminates the first half of the array and continues to search in the second half of the array.
  • Once again, it examines the middle and repeats the above steps to find an item.
  • If the middle item is equal to the search item, then the searching process is stopped.
  • The search is stopped when an item is found or the search reaches the empty portion of the array.
  • Divide and conquer is the strategy for binary search.
    • It divides large problems into smaller units.
  • It minimizes the number of elements to be searched for the desired value.

Step 2

Algorithm:

Algorithm to search the string “A” using binary search algorithm in list is as follows:

  • Declare the list object with “String” data type and add the items to list.
  • The BinarySearch() method takes the inputs parameters of list and search value.
    • Check the middle value is equal to search value. If yes, display the message box with value was found.
    • Otherwise, check if the middle value is greater than search value. If yes, set the last value to search the search item. Because, the search value is in lower half of list.
    • Otherwise, set the last value to search the search item. Because, the search value is in upper half of list.

Step 3
Program:

Program to search the string “A” using binary search algorithm in list is as follows:


//Include libraries

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

//Define namespace

namespace WindowsFormsApp4

{

    //Define a class

    public partial class Form1 : Form

    {

          //Define a constructor for class

          public Form1()

          {

              //Call the InitializeComponent() method

              InitializeComponent();

          }

 

          //Declare and initialize list variable

          List<string> bsList = new List<string>();

 

          //Define the Form1_Load() method

private void Form1_Load(object sender, EventArgs e)

          {

              //Add the inputs to list

              bsList.Insert(0, "b");

              bsList.Insert(1, "A");

              bsList.Insert(2, "C");

              bsList.Insert(3, "e");

    
               Console.WriteLine("The Original List is:"); 

              //Loop executes until the list

              foreach(string s in bsList)

              {

                   //Add the items into the list box

                   listBox1.Items.Add(s);

              }

          }

 

          //Define the BinarySearch() method

         public static object BinarySearchDisplay(int[] arr, int key) 
        
         {
   
            int minNum = 0;
   
            int maxNum = arr.Length - 1;

       while (minNum <=maxNum) 
        {
             int mid = (minNum + maxNum) / 2;
      
              if (key == arr[mid])
        {
         
                   return ++mid;
      } 
          else if (key < arr[mid]) {
         
             max = mid - 1;
      }     
              else 
{
         min = mid + 1;
      }
   }
   return "None";
}

        Console.WriteLine("\nThe List in Sorted form"); 
          
        // sort the List 
             
                bslist.Sort(); 
      
  
        Console.WriteLine(); 
        
               foreach(string bs in bslist) 
        { 
            // prints the sorted List 
            
             Console.WriteLine(bs); 
              
        } 
           if (bslist.Contains("A"))
          
           {
        

         MessageBox.Show("Alphabet A exist in the list");
      }
      
                else
      {
         
           MessageBox.Show("Not exist");
}
}
}

output

The Original List is:

b

A

C

e

The List in Sorted form

A

b

C

e

Alphabet A exist in the list


Related Solutions

Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
C++ Instantiate a binary search tree object and create such tree using elements of the sequence...
C++ Instantiate a binary search tree object and create such tree using elements of the sequence 8,3,10, 1,6,9, 14, 4,7, 13 with 8 as root of the tree. Find maximum and minimum elements of the tree, successor(10) and predecessor(13), print the inorder, postorder and preorder traversal of the tree.
Create a Binary Search Tree using the list below: List : Victor, Ralph, Leo, Mya, Eric,...
Create a Binary Search Tree using the list below: List : Victor, Ralph, Leo, Mya, Eric, Elizabeth, Hester, Damian, Willis, Collin, Keira, Marci, Ashlie, Ressie List out the tree created by the add order of the list using post-order traversal.
Design a program which uses functions to sort a list and perform a binary search. Your...
Design a program which uses functions to sort a list and perform a binary search. Your program should: Iinitialize an unsorted list (using the list provided) Display the unsorted list Sort the list Display the sorted list. Set up a loop to ask the user for a name, perform a binary search, and then report if the name is in the list. Use a sentinel value to end the loop. Do not use the Python built in sort function to...
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...
Java String search Design and implement a recursive version of a binary search.  Instead of using a...
Java String search Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array of size n into k subsets each of size n/k and recursively searches only one of these k subsets. Which one of these k subsets should be searched is decided by making (k − 1) comparisons, each of which can be done in constant time. Clearly, the recurrence relation for this k-ary search algorithm can be written as, T(n) = T(n/k) + (k −...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT