# String swap algorithms

When I went for interview at Imperial College, my interviewer (Dr. P. Kelly) asked me quite a nice question, which (for some unearthly reason) I decided to follow up on afterwards.

The question was a simple one: given a string (e.g.) "examplesgnome" consisting of two substrings (in this case, "examples" and "gnome"), how can we swap the two substrings in-place? In the interview, we covered the third method in more detail, but touched upon the other two. Afterwards, I calculated their computational efficiency, and determined while they all have the same upper bound, one has a lower average running time.

### Method one: reversal

This is one Dr. Kelly suggested, which is really rather neat. It consists of two steps: reversing the entire string, then reversing both substrings. For example:

1. examplesgnome
2. emongselpmaxe
3. gnomeexamples

If we take

n

to equal the total number of characters,

a

to equal the number of characters in the smaller substring, and

b

to equal the number of characters in the larger substring, this has:

\left\lfloor \frac{n}{2} \right\rfloor + \left\lfloor \frac{a}{2} \right\rfloor + \left\lfloor \frac{b}{2} \right\rfloor

swaps, so we may as well say it's of linear complexity

O(n)

.

### Method two: cycles

This one was also suggested by Dr. Kelly, and involves taking the first character of the larger substring out to a temporary location, then cyclically moving characters

b

places along into the generated space. Once the strings have been swapped (

n

swaps have taken place), we put the stored character back in the final space. For example:

1. examplesgnome
2. _xamplesgnome
3. gxamples_nome
4. gxa_plesmnome
5. gxamplesmno_e
6. gxampl_smnoee
7. g_amplxsmnoee
8. gnamplxsm_oee
9. gnam_lxsmpoee
10. gnamelxsmpoe_
11. gnamelx_mpoes
12. gn_melxampoes
13. gnomelxamp_es
14. gnome_xamples
15. gnomeexamples

Since each character moves to its final position in one move, the algorithm is intuitively of complexity

O(n)

— but we can go further and say it's of absolute complexity

\sim (n + 2)

, taking the steps to remove the first character and fill the last space into account.

### Method three: reduction

This is the method I came up with during the interview and, with some help, made work. It basically involves swapping the first smaller substring into its final position, then applying the same algorithm to the resulting subsubstrings of the second substring. For example:

1. examplesgnome
2. gnomelesexamp
3. gnomeampexles
4. gnomeexpamles
5. gnomeexmaples
6. gnomeexamples

This is quite a neat one to implement, and will perform

O(n)

swaps in the worst case (when the smaller substring is only one character long, as one character moves to its final position with each swap).

However, what if we consider the case "foobar", where the substrings are "foo" and "bar"? In this case, method three only takes 3 swaps. Obviously, this method is limited in the upper bound to

O(n)

swaps, but it seems it can do the operation in far fewer — as few as

\Omega (a)

.

I haven't got a concrete proof for this, but I believe this is actually of complexity:

\sim (a \left\lfloor \frac{b}{a} \right\rfloor + (b \bmod{a}) \left\lfloor \frac{a}{b \bmod{a}} \right\rfloor)

as the lengths of the substring for each iteration

i

depend on the remainder of

\frac{b}{a}

for the

i - 1

th iteration. This complexity can be manipulated to prove that it's also of complexity

O(n)

:

All-in-all, I'm quite pleased with the interview: even if I'm not successful, it's given me an interesting problem to consider. Corrections and further thoughts on the problem are welcomed.

# Almanah 0.5.0

It seems to be release week for me. Here's Almanah Diary 0.5.0. Of note with this release is the support for text formatting (bold, italic and underline — nothing particularly fancy), persistent window dimensions, the ability to set the encryption key, and better recovery from database corruption/errors (although it's still not perfect).

Packagers should note that the GTK+ dependency has been bumped from 2.12 to 2.14, and the GConf dependency is no longer optional — though the GtkSpell dependency now is (as spell checking support is now optional).

I've now also completed the application name migration to "Almanah Diary", and as such, the source is now at http://svn.gnome.org/svn/almanah/, and localisation statistics are at http://l10n.gnome.org/module/almanah/.

Plans for future versions include:

• Proper accessibility support
• Automatic link types (yes, this is a stupid name), so that (for example) past Evolution appointments would be shown for each day
• Documentation
• Export to blog, text file, HTML, etc.
• Undo/Redo support

The first three are what I'd like to definitely get done for 0.6.0, but before I can do that I need to sort out the naming for "links" and "automatic links". Obviously these are rubbish names, especially considering one can currently add a "note link". I had considered "attachments", but that's not much better. Does anyone have any ideas?

I went with a friend to see Motörhead in Leicester last night, and it was an awesome gig. Danko Jones and Saxon were supporting, and while I'll give Danko Jones a miss in future, Saxon were great, even if their intro was sickeningly patriotic.

Motörhead themselves were absolutely amazing, playing a variety of stuff: new, old and classic. Mikkey did a fairly hefty drum solo (easily the loudest part of a concert from which my right ear still hasn't stopped ringing), and they managed to fit an acoustic number in, with Lemmy playing the harmonica. I'm told by a friend (whose uncle works for the venue) that Motörhead reached 118dB before the encore, which is not bad at all.

# MCUS version 0.2.1

Here's an announcement I've wanted to make for a while: I'm releasing my microcontroller simulator, MCUS. It's a fairly simple simulator designed to aid in teaching the OCR 2008 A-level electronics syllabus to UK students. As far as I know, it's the only simulator available which follows the new syllabus (for which OCR have deigned to write their own simple assembly language, obsoleting any previous simulators used in schools).

It's already had three releases (this is the fourth), but I didn't want to make the 0.1.x releases too public, as they were mainly for testing the program at school. The 0.2.0 release was a brown paper bag release, due to a bad GTK+ version dependency, and some files necessary for the Windows zip package not being included.

It's available on the MCUS project page, with 0.2.1 being the latest release. A Windows installer is available too, built with NSIS; as well as a simple zip package of the installed directory tree on Windows, since the program's relocatable.

My school is already using it, and I'm releasing it in the hope of other schools being able to make use of it. As always, it's open source, released under the GPLv3+.

Future plans include a graph to visualise the ADC waveform, and the addition of more simulated input and output hardware.