Part a) i)
int numseq[21];
Further explanation
21 locations needed for numbers 0 to 20)
| index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| numSeq | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
Part a) ii)
19
Part a) iii)
Suggested response
int binarySearch(int key, int list[], int low, int high) {
int result = -1;
int keyFound = 0; //sentinel to end loop on a successful search
while (low <= high && !keyFound) {
int middle = (high-low) / 2 + low;//determine middle of the range
if (key == list[middle])
{
result = middle;
keyFound=1; // Key found, exit loop
}
else if (key < list[middle])
{
high = middle - 1; // Narrow the search to the lower half
}
else
low = middle + 1; // Narrow the search to the upper half
}
}
return result; //Returns -1 if not found,
//otherwise, the index of the key
}
Further Explanation
(See this note1)
int binarySearch(int key, int list[], int low, int high) {
int result = -1; //result is -1 if not found
int keyFound = 0; //sentinel to end loop on a successful search
while (low <= high && !keyFound) //while there is more locations
//to search and the key has not
//been found
{
int middle = (high-low) / 2 + low;//determine middle of the range
if (key == list[middle]) //if the key is found..
{
result = middle;
keyFound=1; // Key found, exit loop
}
else if (key < list[middle]) //if key may be in the lower half...
{
high = middle - 1; // Narrow the search to the lower half
}
else //else the key may be in the upper half
{
low = middle + 1; // Narrow the search to the upper half
}
}
return result; // Returns -1 if not found, otherwise the key
Response using recursion
int binarySearch(int key, int list[], int low, int high)
{
int result=-1;
int middle = (high-low)/2+low;
if (low<=high)//if low and high have not crossed...
{
if(key==list[middle])//if the key is found....
{
result=middle;
return result
}
else//the key was not found
{
if(key<list[middle])
{
result= binarySearch(key,list,low,middle-1);
return result;
}
else
{
result= binarySearch(key,list,middle+1,high);
return result;
}
}
}
return result;
Further Explanation
int binarySearch(int key, int list[], int low, int high)
{
/*we use low and high to represent a
portion of the array. Initially, low and high
points to the start of the array and the end of
the array respectively. Middle indexes the location
to the middle of the array.
*/
int result=-1;//-1 represents a failed search
int middle = (high-low)/2+low;
if (low<=high)//if low and high have not crossed...
{
if(key==list[middle])//if the key is found....
{
result=middle;
return result;//return the current index and exit function
}
else//the key was not found
{
if(key<list[middle])//if the key would be found
{ //from low to middle
//we now search the lower half
result= binarySearch(key,list,low,middle-1);
return result;
}
else //key would be found from middle to high
{
result= binarySearch(key,list,middle+1,high);//we now search the upper half
return result;
}
}
}
return result;
Part b i)
//assume integer array
void swap(int array[], int a, int b) {
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
Part b ii)
- The array is traversed from index 0 to n and the location of the smallest element is determined.
- The smallest element is swapped with the first element that was traversed.
- This is repeated on the unsorted portion of the array by incrementing the starting element that is traversed to 1 to n, then 2 to n, 3 to n, … until n-1 to n.
Part b iii)
void selectionSort (int arr[], int low, int high)
{
for (int i = low; i<high; i++)
{
int smallest = getSmallest(arr,i,high);
swap(arr,i,smallest);
}
}
Further explanation
The following complete program in C tests that the selection sort is working.
#include <stdio.h>
//function declarations
void selectionSort(int arr[], int low, int high);
void swap(int array[], int a, int b);
int getSmallest(int arr[], int low, int high);
int main()
{
int array[] = {64, 25, 12, 22, 11};
int size = sizeof(array) / sizeof(array[0]);
printf("Original array: ");
for (int i = 0; i < size; i++)
printf("%d ", array[i]);
printf("\n");
//sort the array
selectionSort(array, 0, size-1);
printf("Sorted array: ");
for (int i = 0; i < size; i++)
printf("%d ", array[i]);
printf("\n");
printf("\nEnding program...");
return 0;
}
void selectionSort(int arr[], int low, int high) {
// Traverse up to the
// second-to-last element
for (int i = low; i < high; i++)
{
/*find the index of the smallest
element in the range
*/
int smallest = getSmallest(arr, i, high);
/*if the smallest element
should be put in the first
position that was
traversed ...
*/
if(arr[smallest]<arr[i])
swap(arr, i, smallest);//swap elements
}
}
void swap(int array[], int a, int b) {
int temp = array[a];
array[a] = array[b];
array[b] = temp;
}
int getSmallest(int arr[], int low, int high) {
int smallestIndex = low;
// Include the high in the search
for (int i = low + 1; i <= high; i++)
{
if (arr[i] < arr[smallestIndex]) {
smallestIndex = i;
}
}
return smallestIndex;
}
Part c)
| Location | 0 | 1 | 2 | 3 | 4 |
| Value | 30 | 50 | 40 | 60 | 20 |
| Pass 1 | 30 | 40 | 50 | 20 | 60 |
| Pass 2 | 30 | 40 | 20 | 50 | 60 |
(See this note2)
Footnotes
- https://islandclass.org/2020/11/12/binary-search-implementation/ ↩︎
- https://islandclass.org/2020/11/04/bubble-sort-implementation-demonstration/ ↩︎
Navigation
- Part a) i)
- Part a) ii)
- Part a) iii)
- Part b i)
- Part b ii)
- Part b iii)
- Further explanation
- Part c)
- Footnotes
- Navigation
© 2023 Vedesh Kungebeharry. All rights reserved.