Question: Code: #include #include #include int* bubbleSort(int arr[], int n){ int temp, i = 0, j = 0; int *s_arr = (int*)malloc(n * sizeof(int)); // Copy

Code: #include #include #include int* bubbleSort(int arr[], int n){ int temp, iCode:

#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;

}

(Assignment 1, individual) Arrays and pointers As we have discussed in lecture, we can use array names as if they are pointers. Open array.c and complete the following tasks: 1. This program will store integers entered by a user into an array. It then calls bubleSort 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. end uising pointer notatio 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). .e. you cannot use s_arr[j] anymore. Comment out the original code so the algorithm won't be run twice. algorthm 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!