All examples By author By category About

chabb

String-Word Inversion

This one is a classic : reverse a string, and reverse the word of the strings, in place, and in linear time.

Cost comparison is caracter exchange. The heart of the algorithm is the reverseWord function, which reverses a word.

The first passe uses N/2 characters exchange, where N is the number of character

The second passe uses W*(w/2) characters exchange, where w is the average length of a word, and W the number of word. This falls back around N/2

Overall Cost is N exchange