#include<stdio.h>
/*
* Implemented by Arjun Sunel.
*/
// Method to swap two characters 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)
{
if(swapped == 0 || size == 0)
{
return;
}
int i;
for(i = 0; i < size - 1; i++)
{
swapped = 0;
if( array[i] > array[i + 1] )
{
Swap(&array[i], &array[i + 1]);
swapped = 1;
}
}
BubbleSort(array, size-1, swapped);
}
// Entry point of the program
int main()
{
char array[] = {'d', 'a', 'c', 'b'};
int arraySize = 4;
int index;
printf("Before sorting : \n");
for(index = 0; index <= arraySize - 1; index++)
{
printf("%c ", array[index]);
}
int swapped = 1;
BubbleSort(array, arraySize, swapped);
printf("\nAfter sorting : \n");
for(index = 0; index <= arraySize - 1; index++)
{
printf("%c ", array[index]);
}
printf("\n");
}
Sunday, 14 December 2014
Recursive BubbleSort for Array of Characters
Labels:
Sorting
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment