A
arnuld
Here is the C implementation of Selection-Sort. Any advice for
improvement will be apppreciated:
/* A C program to sort an array of characters using bubble sort algorithm
*
* VERSION 0.0
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort_bubble(char* );
int main(void)
{
char arrc[] = "heathfield";
printf("array(unsorted): %s\n", arrc);
sort_bubble(arrc);
printf("array(sorted): %s\n", arrc);
return 0;
}
void sort_bubble(char* c)
{
size_t i, j;
size_t s = strlen(c);
for(i = 0; i < s; ++i)
{
for(j = s-1; j != i; --j)
{
if( c[j] < c[j-1] )
{
char temp = c[j];
c[j] = c[j - 1];
c[j - 1] = temp;
}
}
}
}
========================== OUTPUT ==============================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra selection-sort.c
[arnuld@dune programs]$ ./a.out
Original Array = arnuld
Sorted Array = adlnru
[arnuld@dune programs]$
improvement will be apppreciated:
/* A C program to sort an array of characters using bubble sort algorithm
*
* VERSION 0.0
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort_bubble(char* );
int main(void)
{
char arrc[] = "heathfield";
printf("array(unsorted): %s\n", arrc);
sort_bubble(arrc);
printf("array(sorted): %s\n", arrc);
return 0;
}
void sort_bubble(char* c)
{
size_t i, j;
size_t s = strlen(c);
for(i = 0; i < s; ++i)
{
for(j = s-1; j != i; --j)
{
if( c[j] < c[j-1] )
{
char temp = c[j];
c[j] = c[j - 1];
c[j - 1] = temp;
}
}
}
}
========================== OUTPUT ==============================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra selection-sort.c
[arnuld@dune programs]$ ./a.out
Original Array = arnuld
Sorted Array = adlnru
[arnuld@dune programs]$