Question

In: Computer Science

Reimpelment a function called bubble_sort that has the following prototype. bubble_sort(int *array, int size, pointer to...

Reimpelment a function called bubble_sort that has the following prototype.

bubble_sort(int *array, int size, pointer to a function)
Pre condition
array - a pointer to a an array of size element.
pointer to function - a pointer to a function that compares two values (depending on sorting in ascending order or descending order)
Post condition
Sort the array in ascending or descending based on the the pointer to a function.

Call the function bubble_sort to sort the array in ascending
Display the sorting array.
Call the function bubble_sort to sort the array in descending
Display the sorting array.

use a list of integers from a file called data.txt
Here is the content of the file data.txt.
9
8
4
7
2
9
5
6
1
3

Solutions

Expert Solution

IDE Used: Dev C++

Note: For sorting in descending order, the step  has been mentioned as a comment inside bubble_sort function just above if statement

Program

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

//function to compare the numbers will return true if first>second
bool compare(int x, int y)
{
   bool status;
   if(x>y)
   {
       status=true;
   }
   else
   {
   status=false;  
   }
   return status;
}

//bubble sort function
void bubble_sort(int *arr, int size, bool (*func_ptr)(int, int))
{
int i, j,temp;
  
//pointer to function compare()
func_ptr= compare;
  
   for (i = 0; i < size; i++)   
{
  
  
for (j = 0; j < size-i-1; j++)
  
       //calling the compare() function using pointer
       //for descending order sorting use if((*func_ptr)(arr[j+1],arr[j]))
if ((*func_ptr)(arr[j+1],arr[j]))
{
   //swapping the value if condition true
       temp = arr[j];
           arr[j]=arr[j+1];
           arr[j+1]= temp;
             
       }
}
}
int main()
{
   FILE *file;
  
   //opening the file
   file = fopen("data.txt", "r");
   int i =0;
   int x ;
   int size;
   int arr[20];
   int *arr_ptr = arr;
   void (*func_ptr);
   while(!feof(file))
   {
       //reading the file
       fscanf(file, "%d",&x);
      
       //stroing the file data in array
       arr[i]=x;
       i++;
   }
   size=i;
  
   //calling the bubble sort function
   bubble_sort(arr_ptr,size, func_ptr);
  
   //printing the sorted array
   printf("Sorted array using bubble sort\n");
   for (i = 0; i < size; i++)
printf("%d, ",arr[i]);
  
   //closing the file
   fclose(file);
   return 0;
}

Output for ascending

Output in descending

Sorted array using bubble sort , 2, 3, 4, 5, 6, 7, 8, 9, 9, Process exited after 0.07594 seconds with return value 0 Press any key to continue

Sorted array using bubble sort 9. 9 8 7. 6 5, 4, 3 2, 1, Process exited after 0.04748 seconds with return value 0 Press any key to continue

Data.txt

9
8
4
7
2
9
5
6
1
3


Related Solutions

Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size...
C++ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must write our commands in the middle: Write a C++ Program: #include <iostream> using namespace std; const int ARRAY_SIZE = 15; void printArray(const int x[], int sizeX); int smallestIndex(const int x[], int sizeX); int main() {      int list[ARRAY_SIZE] = {56,...
Element Shifter: Write a function that accepts an int array and the array’s size as arguments....
Element Shifter: Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int A [ ], int n) { M = A[0]; for i = 1 to n – 1     if (A[i] > M)         M = A[i]        //Update the max return M; } Write a recursive version of this program. Let f(n) be the number of key comparisons performed by this algorithm. Write a recurrence equation for f(n). Prove by induction that the solution of...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array.
C++ ProgramWrite a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT