#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 i, j;
int swapped = 1;
for(i = 0; swapped && i < size - 1; i++)
{
swapped = 0;
for(j = 0; j < size - 1 - i; j++)
{
if( array[j] > array[j + 1] )
{
Swap(&array[j], &array[j + 1]);
swapped = 1;
}
}
}
}
// 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]);
}
BubbleSort(array, arraySize);
printf("\nAfter sorting : \n");
for(index = 0; index <= arraySize - 1; index++)
{
printf("%c ", array[index]);
}
printf("\n");
}
Sunday, 14 December 2014
Iterative BubbleSort for Array of Characters
Labels:
Sorting
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment