Design an application that has an array of at least 20 integers. It should call
ID: 674646 • Letter: D
Question
Design an application that has an array of at least 20 integers. It should call a module that uses the sequential search algorithm to locate one of the values. The model should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another module that uses the binary search algorithm too locate the same value. It should also keep a count of the number of comparisons it makes. Display these values on the screen.
I attached a sample pseudocode that I would like it formatted in, so that I can have a better understanding. Thank you
1 Module main) 2Constant for the array size 6 3 Constant Integer SIZE 5 11 An array of Integers 6 Declare Integer numbers[SIZE] -4, 6, 1, 3, 5, 2 8 1 Loop counter 9 Declare Integer index 10 11 IDisplay the array in its original order. 12 Display "Original order: 13 For index 0 To SIZE 1 14 15 End For Display numbers[ index] Sort the numbers 17 18 19 Call selectionSort (numbers, SIZE) 20 21 Display a blank line. Display 23 I Display the sorted array. 24 Display "Sorted order: 25 For index = 0 To SIZE- 1 26 27 End For 28 End Nodule 29 30 // The selectionsort module accepts an array of integers 31 // and the array's size as arguments. When the module is 32 // finished, the values in the array will be sorted in 33 // ascending order. 34 Module selectionsort (Integer Ref arrayl, Integer arraysize) 35 startscan will hold the starting position of the scan. 36 Declare Integer startScan 37 Display numbersl index] 38 I minIndex will hold the subscript of the element with smallest value found in the scanned area. 39 / the smalles 40 Declare Integer minIndex // the minvalue will hold the smallest value found in the 2 m 43 scanned area. 44 Declare Integer minValue 45 46 /1 index is a counter variable used to hold a subscript. 47 Declare Integer index 4 8 49 I The outer loop iterates once for each element in the s0 / array, except the last element. The startscan variable s1 // marks the position where the scan should begin.
Explanation / Answer
Module Main()
{
Constant Int SIZE=20;
Int normalCount=0;
Declare Integer array[size]={1,2,3,4,5,6,7,8,9,11,22,33,44,55,66,77,88,99,111,222}
Int searchValue;
For index=0 to SIZE-1
If array[index]==searchValue
Break;
End if
normalCount++;
End for;
Print “No. of comparisions in Linear Search ”+ normalCount;
End Module;
Module Binary Search(Int ref array[], Int SIZE, searchVal)
Declare int mid, low=0, high=SIZE-1;
Declare int stepCount=0;
//Start search
While(low<=high)
stepCount++;
Mid= (low+high)/2;
If array[mid]>searchVal
High=mid-1;
Else if array[mid]<searchVal
Low=mid+1;
Else
Return mid;
End IFELSE;
End While;
Print “No. of comparisions in Binary Search “+stepCount;
End Module;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.