Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please Explain and show output. (array.c) #include #include #include int* bubbleSort(int arr[], int n){ int temp, i = 0, j = 0; int *s_arr =

image text in transcribedPlease Explain and show output.

(array.c)

#include

#include

#include

int* bubbleSort(int arr[], int n){

int temp, i = 0, j = 0;

int *s_arr = (int*)malloc(n * sizeof(int));

// Copy arr to s_arr

for(i=0; i

s_arr[i] = arr[i];

// Actual sorting using array notations

// Comment this when implementing pointer version.

for (i=0;i

for(j=0;j

if(s_arr[j] > s_arr[j+1]){

temp = s_arr[j+1];

s_arr[j+1] = s_arr[j];

s_arr[j] = temp;

}

}

}

// Actually sorting using pointer notations. i.e. you cannot use "[]"!

// Your code goes here...

return s_arr;

}

void printArray(int arr[], int n){

int i = 0;

printf("[");

for(i=0; i

printf("%d ", arr[i]);

printf("] ");

}

int bSearch(int *arr, int a, int b, int key){

// Binary search function. arr is the array, key is the value to

search for, a and b are the boundaries of arr to be searched within.

// You must use pointer notations. i.e. no "[]"

// Your code goes here:

return 0; // Modify this to return an appropriate value!

}

int main() {

int i = 0, size = 0, key = 0, result = 0;

int *array, *sorted;

printf("How big is your array? ");

scanf("%d", &size);

array = (int*)malloc(size * sizeof(int));

for(i=0; i

printf("Please enter array[%d]: ", i);

scanf("%d", &array[i]);

}

printf("Please wait while I sort your numbers... ");

sorted = bubbleSort(array, size);

printf("Here is your original array: ");

printArray(array, size);

printf("Here is your SORTED array: ");

printArray(sorted, size);

printf("What number are you looking for? ");

scanf("%d", &key);

printf("OK, let me see if I can find a \"%d\" in the array... ",

key);

result = bSearch(sorted, 0, size-1, key);

if(result != -1)

printf("Got it! A \"%d\" is at index %d. ", key, result);

else

printf("I'm sorry, a \"%d\" cannot be found in the array. ",

key);

return 0;

}

1. This program will store integers entered by a user into an array. It then calls bubbleSort to sort the array. Study the code in bubbleSort to refresh your memory on Bubble Sort algorithm and answer the following questions a. Why do we need to pass the size of array to the function? b. Is the original array (the one being passed into the function) changed at the end of this function? c. Why do you think a new array (s_array) is needed to store the result of the sorted values (why not update the array as we sort)? Hint: look at what the main function does 2. Once you remember how Bubble Sort works, re-write the code so that you are accessing the array's content using pointer notations(*s_arr). i.e. you cannot use s arr[i] anymore. Comment out the original code so the algorithm won't be run twice. 3. After the array is sorted, the program will ask user to enter a key to search for in the sorted array. It will then call bSearch to perform a Binary Search on the array. Complete the bSearch function so that it implements Binary Search recursively (no loop!) You must use pointer notations here as well. Pay attention to what is written in main so your bSearch will return an appropriate value

<>

<-1>

<-1>

<>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions

Question

=+Will the assumptions youve made change over time?

Answered: 1 week ago