Skip to content

Course home

Searching algorithms

Searching algorithms

EasyMediumHard
12345678910111213141516171819202122
Question 1

Complete the trace table for the program in Figure 1 if the user input is Spain.

Part of the table has already been filled in.

You may not need to use all the rows in the table.

Figure 1

string[] countries = {"Brazil", "Canada", "Egypt", "France",
"India", "Japan", "Mexico", "Spain"};
Console.Write("Enter a country to find: ");
string countryToFind = Console.ReadLine();
bool validCountry = false;
int start = 0;
int finish = countries.Length - 1;
while (validCountry == false && start <= finish) {
    int mid = (start + finish) / 2;
    if (countries[mid] == countryToFind) {
        validCountry = true;
    }
    else if (countryToFind.CompareTo(countries[mid]) == 1)
    {
        start = mid + 1;
    } else {
        finish = mid - 1;
    }
}
Console.WriteLine(validCountry);
countryToFindvalidCountrystartfinishmid
SpainFalse073
[4]
Markscheme

Searching algorithms Questions

  1. GCSE
  2. /Computer Science
  3. /Searching algorithms

33 exam-style questions on AQA GCSE Computer Science Searching algorithms. Each one has a worked solution and a mark scheme showing where the marks go.

Question bank