In: Computer Science
This is my code for an array using the bubblesort in the function outside of main. My bubblesort is not working correctly , and I was told i'm missing a +j instead of +i in the function for void sorter.Can someone please help me with making my sorter work properly? everything else is fine and runs great. Thank you #include <stdio.h> #include <stdlib.h> #include <time.h> void printer(int *ptr, int length); void randomFill(int *ptr, int length); void sorter(int *ptr, int length); int main(void) { int size =30; int array[size]; int *myPointer; myPointer = array; int i; for(i =0; i<size; i++) { *myPointer = 0; myPointer++; } printf("\nArray before:\n\n"); printer(array, size); randomFill(array, size); printf("\nArray after insert:\n\n"); printer(array, size); sorter(array, size); printf("\nArray after sort:\n\n"); printer(array, size); return 0; } void printer(int *ptr, int length) { int i =0; for(i =0; i<length; ++i) printf("a[%d] = %d\n\n", i, *(ptr + i)); } void randomFill(int *ptr, int length) { srand(time(NULL)); int i; for(i =0; i<length; i++) { *ptr = (rand()% (205-55+1)) + 55; ptr++; } } void sorter(int *ptr, int length) { int i, j, temp; for(i =0; i<length; i++) { for(j = i+1; j<length; j++) { if(*(ptr +i) > *(ptr +j)) { temp = *(ptr + i); *(ptr + i) = *(ptr + j); *(ptr + j) = temp; } } } }
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//prototypes of functions
void printer(int *ptr, int length);
void randomFill(int *ptr, int length);
void sorter(int *ptr, int length);
//driver program
int main(void)
{
srand(time(NULL));
int size =30; //set the valeu of size to 30
int array[size];
int *myPointer;
myPointer = array; //assign the base address of array to
myPointer
int i;
//set the values of all 30 elements to 0
for(i =0; i<size; i++)
{
*myPointer = 0;
myPointer++;
}
//print the array elements
printf("\nArray before:\n\n");
printer(array, size);
//Randomly fill 30 elements into array
randomFill(array, size);
//display the elements
printf("\nArray after insert:\n\n");
printer(array, size);
//sort the 30 elements
sorter(array, size);
//display the elements after sort
printf("\nArray after sort:\n\n");
printer(array, size);
return 0;
}
//printer() method will print the elements of array through
pointer
void printer(int *ptr, int length)
{
int i =0;
for(i =0; i<length; ++i)
printf("a[%d] = %d\n\n", i, *(ptr + i)); //print the elements
}
//rgis method will generate a random integer and assign it to
array
void randomFill(int *ptr, int length)
{
int i;
for(i =0; i<length; i++)
{
*ptr = (rand()% (205-55+1)) + 55;
ptr++;
}
}
//this method implements bubble sort to sort the array
elements
void sorter(int *ptr, int length)
{
int i, j, temp;
//logic for bubble sort
for(i =0; i<length; i++)
{
for(j = 0; j<length-i; j++) // in this step it will skip one
element from last after each iteration
{
if(*(ptr +j) > *(ptr +(j+1))) //compare the element with its
next element
{ //perform swap operation
temp = *(ptr + j);
*(ptr + j) = *(ptr +(j+1));
*(ptr +(j+1)) = temp;
}
}
}
}
OUTPUT