Question
Your tasks are: 1. Create a function printArray that prints an array of any length. This is needed to illustrate that the sort is working.
Your tasks are: 1. Create a function printArray that prints an array of any length. This is needed to illustrate that the sort is working. It can also be used to help debug the code.
2. Create a function called fillArray that can fill an array of any specified length with random numbers with values from -1000 to 1000. (Roughly half of the numbers should be negative). HINT: Remember #include and rand()
3. You may wish to create a function (swapElements?) to perform the swap.
4. After you have the sort function working, build a user interface that asks the user to enter the size of the array to work with (less than the length allocated). Then use fillArray to populate an array of that size. Display the unsorted array. Then display the sorted array.
5. Report the total number of comparisons as well as the number of swaps. (See pairsCompared as well as totalSwaps )
6. The number of pairs compared is likely (N-1)*(N-1). Reduce this number (increase efficiency) without losing functionality. It is relatively simple to reduce this by roughly half. HINT: Change the inner loop bound from len to something smaller; after a pass, you need only sort one less than the previous pass.
7. Even after the above fix, the number of pairs compared can be reduced. Notice that bubble sort will repeatedly check the array even if the array is already sorted. Change the code so that it stops once the array is sorted.
8. Make a function to report the following values (with respect to the array): the largest and smallest (most negative) values; the average; and the median.
Here is the code given so far.....
#include
#include
int i=0;
int printArray(int a[i],int i)
{
i=0;
printf("How many characters long you want your array?");
scanf("%i",&i);
printf(" %i",a[i]);
}
int fillArray(int b[i])
{
}
int swapElements(int c[])
{
}
int main(void)
{
int num;
/*
The code generates a random number from 0 to 1000 inclusive.
A seed is first set based on the current time.
*/
srand((signed)time(NULL));
num = rand()%1001;
int a[100] = {2,4,1,3,5};
printArray(a,5);
BubbleSort(a[i], 5);
return 0;
printArray(a,5);
}
void BubbleSort(int sortMe[], int len)
{
int numSorted; // number of elements sorted
int posToCheck; // position of current element
int totalSwaps = 0; // number of swaps made by the algorithm
int pairsCompared = 0; // number pairs compared by algorithm
//keep sorting until the entire array is sorted
numSorted = 0;
while(numSorted < len)
{
posToCheck = 1;
//move the largest element to the end of the array
while(posToCheck < len)
{
// Is the value at the current position less than
// that at the position immediately prior.
// If so, swap the two values.
pairsCompared = pairsCompared + 1;
if(sortMe[posToCheck-1] > sortMe[posToCheck])
sortMe[posToCheck] = sortMe[posToCheck-1];
sortMe[posToCheck-1] = sortMe[posToCheck];
totalSwaps = totalSwaps + 1;
}
//move to the next position
posToCheck = posToCheck + 1;
}
//increase the number of sorted elements
numSorted = numSorted + 1;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started