#include<stdio.h>
#include<string.h>
/*
* Implemented by Arjun Sunel.
*/
// Method to swap two strings by reference.
void Swap(char **first, char **second)
{
char *temp = *first;
*first = *second;
*second = temp;
}
// Bubble Sort Method
void BubbleSort(char *array[], int size, int swapped)
{
int i;
if(swapped == 0 || size == 0)
{
return;
}
for(i = 0; i < size - 1; i++)
{
swapped = 0;
if(strcmp(array[i], array[i+1]) > 0)
{
Swap(&array[i], &array[i + 1]);
swapped = 1;
}
}
BubbleSort(array, size-1, swapped);
}
// Entry point of the program
int main()
{
char* array[] = {"dog", "dose", "apple", "baby", "den", "deck"};
int arraySize = 6;
int index;
printf("Before Sorting : \n");
for(index = 0; index < arraySize; index++)
{
printf("%s ", array[index]);
}
printf("\n");
int swapped = 1;
BubbleSort(array, arraySize, swapped);
printf("After Sorting : \n");
for(index = 0; index < arraySize; index++)
{
printf("%s ", array[index]);
}
printf("\n");
return 0;
}
Sunday, 14 December 2014
Recursive BubbleSort for Array of Strings
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment