Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C, please include your Pseudocode 2. Using Pointers and Arrays 2.1 Write a fu

ID: 3718563 • Letter: I

Question

In C, please include your Pseudocode

2. Using Pointers and Arrays 2.1 Write a function bubblesort which will accept an array of double precision floating point numbers of length N and sort the array. Ref. https://en.wikipedia org/wiki/Bubble_sort (I can verify this is accurate) 2.2 Write a program which exercises the bubblesort. For a good test, the program should have more than 20 values. Optional Extra Credit #2 (worth double extra credit points, this is a real challenge) Write a function called quicksort which will accept an array of double precision floating point numbers of length N and sort the array. Ref. https:len.wikipedia.org/wiki/Quicksort (Bubblesort is a terrible sorting algorithm, but easy to grasp and a decent challenge for required work. Quicksort is fast, but a bit of a brainbender) Replace bubblesort with quicksort in your program in 2.2 and verify function

Explanation / Answer

// File Name: BubbleSortDouble.c

#include <stdio.h>

// Function to swap two numbers
void swapNumber(double *first, double *second)
{
// Stores the first number in temp
double temp = *first;

// Stores the second number in first
*first = *second;
// Stores the temp number in second
*second = temp;
}// End of function

// Function to implement bubble sort
void bubbleSort(double numbers[], int len)
{
int c, d;
double temp;
// Loops till length of the array minus one times
for (c = 0; c < len - 1; c++)
{
// Loops till length of the array minus outer loop variable value minus one times
// Minus outer loop variable value because after each iteration one number is sorted
for (d = 0; d < len - c - 1; d++)
{
// C hecks if the array d index position data is greater than the d + 1 index position data
if (numbers[d] > numbers[d + 1])
{
// Calls the function to swap the numbers at d with d + 1 index position
swapNumber(&numbers[d], &numbers[d + 1]);
}// End of if condition
}// End of inner for loop
}// End of outer for loop
}// End of function

// Function to print an array
void displayArray(double numbers[], int len)
{
int c;
// loops till length of the array
for (c = 0; c < len; c++)
printf("%8.2f ", numbers[c]);
}// End of function

// main function definition
int main()
{
// Creates an array and assigns value to it
double numbers[] = {6.4, 3.4, 2.5, 1.2, 22.6, 11.9, 90.56, 14.38, 1.29, 1.9, 37.77, 78.31, 98.77, 22.12, 17.7, 70.5, 88.1, 39.78, 6.99, 21.06};
// Calculates the length of the array
int length = sizeof(numbers)/sizeof(numbers[0]);
printf(" Original array: ");
displayArray(numbers, length);
// Calls the function to sort the numbers
bubbleSort(numbers, length);
printf(" Sorted array: ");
// Calls the function to display the numbers
displayArray(numbers, length);
return 0;
}// End of function

Sample Output:

Original array:
6.40 3.40 2.50 1.20 22.60 11.90 90.56 14.38 1.20 1.90 37.77 78.31 98.77 22.12 17.70 70.50 88.10 39.78 6.99 21.06

Sorted array:
1.20 1.29 1.90 2.50 3.40 6.40 6.99 11.90 14.38 17.70 21.06 22.12 22.60 37.77 39.78 70.50 78.31 88.10 90.56 98.77

---------------------------------------------------------------------

// File Name: QuickSortDouble.c
#include<stdio.h>

// Function to swap two elements
void swapNumber(double *first, double *second)
{
double temp = *first;
*first = *second;
*second = temp;
}

/* Function takes last element as pivot, places the pivot element at its correct position in sorted
array, and places all smaller (smaller than pivot) to left of pivot and all greater elements to right of pivot */
int partition (double numbers[], int low, int high)
{
// pivot value
double pivot = numbers[high];
// Index of smaller element
int c = (low - 1);
int d;
for (d = low; d <= high - 1; d++)
{
// If current element is smaller than or equal to pivot
if (numbers[d] <= pivot)
{
// increment index of smaller element
c++;
// Call the function to swap number stored at c index position with number stored at d index position
swapNumber(&numbers[c], &numbers[d]);
}// End of if condition
}// End of for loop
// Call the function to swap number stored at c plus one index position with number stored at high index position
swapNumber(&numbers[c + 1], &numbers[high]);
// Returns c plus one value
return (c + 1);
}// End function

/* Function to implements QuickSort
numbers[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(double numbers[], int low, int high)
{
// Checks if low is less than high
if (low < high)
{
// pi is partitioning index, number[p] is now at right place
int pi = partition(numbers, low, high);

// Separately sort elements before
// partition and after partition
quickSort(numbers, low, pi - 1);
quickSort(numbers, pi + 1, high);
}// End if condition
}// End function

// Function to display the numbers
void displayArray(double numbers[], int len)
{
int c;
// Loops till length of the array
for (c = 0; c < len; c++)
// Display each element
printf("%6.2f", numbers[c]);
}// End function

// main function definition
int main()
{
// Creates an array and assigns value to it
double numbers[] = {6.4, 3.4, 2.5, 1.2, 22.6, 11.9, 90.56, 14.38, 1.29, 1.9, 37.77, 78.31, 98.77, 22.12, 17.7, 70.5, 88.1, 39.78, 6.99, 21.06};
// Calculates the length of the array
int length = sizeof(numbers)/sizeof(numbers[0]);
printf(" Before sorting array: ");
// Calls the function to display the numbers
displayArray(numbers, length);

// Calls the function to sort the numbers
quickSort(numbers, 0, length - 1);

printf(" After sorting array: ");
// Calls the function to display the numbers
displayArray(numbers, length);
return 0;
}// End main function

Sample Output:

Before sorting array:
6.40 3.40 2.50 1.20 22.60 11.90 90.56 14.38 1.29 1.90 37.77 78.31 98.77 22.12 17.70 70.50 88.10 39.78 6.99 21.06
After sorting array:
1.20 1.29 1.90 2.50 3.40 6.40 6.99 11.90 14.38 17.70 21.06 22.12 22.60 37.77 39.78 70.50 78.31 88.10 90.56 98.77