#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;
}
// Insertion Sort Method
void InsertionSort(char *array[], int size)
{
int i, j;
char *curElement;
for(i = 1; i < size; i++)
{
curElement = array[i];
j = i - 1;
while(j >=0 && strcmp(array[j], curElement) > 0)
{
Swap(&array[j+1], &array[j]);
j--;
}
array[j+1] = curElement;
}
}
// 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");
InsertionSort(array, arraySize);
printf("After Sorting : \n");
for(index = 0; index < arraySize; index++)
{
printf("%s ", array[index]);
}
printf("\n");
return 0;
}
Sunday, 14 December 2014
Iterative InsertionSort for Array of Strings
Labels:
Sorting
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment