Write a version of the sequential search algorithm that can be used to search a sorted list. Write a program to test the sequential search algorithm. Use either the function bubbleSort or insertionSort to sort the list before the search. Your program should prompt the user to enter 10 digits and then prompt the user to enter a digit to search - if the list contains this digit, output its position to the console:

Respuesta :

Answer:

#include <iostream>

using namespace std;

void insertionSort(int myArr[]){

  int currentvalue, position;

  for (int i = 1; i < myArr.size(); ++i){

    currentvalue = myArr[i] ;

    position = i;

    while (position>0 and myArr[position-1]>currentvalue){

        myArr[position]= myArr[position-1] ;

        position = position-1 ;

     }

    myArr[position]= currentvalue;

  }

}

int main(){

   int numberList[10], search;

   for (int x = 0; x < 10; ++x){

       cin>> numberList[x];

   }

   insertionSort(numberList);

   cout << "Enter the search term/number: ";

   cin>> search;

   for (int i = 0; < 10; ++x){

       if (search == number[i]){

           cout<< i;

           break;

       } else {

           cout<< "Number does not exist.";

       }

}

}

Explanation:

The C++ source code defines the insertionSort void function, then prompts the user ten times to fill the array numberList. It uses the insertion sort algorithm to sort the array of ten items and then a number is searched using a sequential search algorithm.