Question: I have the following C code of a bubble sort algorithm that works well. I want to modify the program so that it automatically generates
I have the following C code of a bubble sort algorithm that works well. I want to modify the program so that it automatically generates a list of random integers and then sorts them, as opposed to the integers already being provided in the program. Thanks for the help
#include
void printvalues(); void sort(); void swap(int*, int*); int values[] = {7,3,9,4,6,1,2,8,5};
void printvalues() { int i = 0; for (i=0; values[i] != '\0'; i++) { printf("%d", values[i]); } printf(" "); }
void swap(int* x, int* y) { int var; var = *x; *x = *y; *y = var; }
void sort() { int i = 0; int j = 0; for (i=0; values[i] != '\0'; i++) { for(j=0; values[j+1] != '\0'; j++) { if (values[j] > values[j+1]) { swap(&values[j], &values[j+1]); } } } }
int main() { printf("Before "); printvalues(); sort(); printf("After "); printvalues(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
