I am trying to implement bubble sort for a class assignment. They are trying to teach us how to use pointers. The only problem is it is an online course and I don't quite get the pointer thing. I'm having trouble with the swap function and getting it the right pointer level to be passed. Please help!
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 10
void bubbleSort(char * const array[], const int size);
int main()
{
char *a[SIZE] = {"Mahmuda", "Tim", "Andrew", "Jacob", "Vasant", "Kenneth", "Kendall", "John", "Vijayakumar", "Marion"};
int i;
printf("Data items in original order\n");
//loop through array a
for(i = 0; i < SIZE; i++)
{
printf("%s ", *(a + i));
}
printf("\nData items in ascending order\n");
bubbleSort(a, SIZE);
for(i = 0; i < SIZE; i++)
{
printf("%s ", *(a + i));
}
printf("\n");
return 0;
}
void bubbleSort(char * const array[], const int size)
{
void swap(char **ptrElement1, char **ptrElement2);
int stringGreaterThan(char string1[], char string2[]);
int pass;
int j;
for(pass = 0; pass < size - 1; pass++)
{
for(j = 0; j < size - 1; j++)
{
if(strcmp(*(array + j), *(array + j + 1)) > 0)
{
swap(*(array + j), *(array + j + 1));
}
}
}
}
void swap(char **ptrElement1, char **ptrElement2)
{
int hold = *ptrElement1;
*ptrElement1 = *ptrElement2;
*ptrElement2 = hold;
}