An Algorithmic Lucidity

a blog

July 2013

Quotations I

"As far as anyone knows, there's never been an animal population that was stable in the absence of predation, famine, or disease."

"Don't get discouraged," Carla said, reaching over and putting a hand on his shoulder. "That's just the history of life for the past few eons. It's not as if it's a law of physics."

The Eternal Flame by Greg Egan

Cherryl did not answer, then said suddenly, desperately, "Look ... what I don't want is charity."

"Jim must have told you—and it's true—that I never engage in charity."

"Yes, he did ... But what I mean is—"

"I know what you mean."

"But there's no reason why you should have to feel concern for me ... I didn't come here to complain and ... and load another burden on your shoulders. ... That I happen to suffer, doesn't give me a claim on you."

"No, it doesn't. But that you value all the things I value, does."

"You mean ... if you want to talk to me, it's not alms? Not just because you feel sorry for me?"

"I feel terribly sorry for you, Cherryl, and I'd like to help you—not because you suffer, but because you haven't deserved to suffer."

"You mean, you wouldn't be kind to anything weak or whining or rotten aobut me? Only to whatever you see in me that's good?"

"Of course."

Atlas Shrugged by Ayn Rand

The wizard gets the delight of working in a specialized area—magic—and gets a good look at the foundations of the Universe, the way things really work. It should be stated that there are people who consider the latter more of a curse than a blessing.

So You Want to Be a Wizard by Diane Duane

But I don't need to know the answer. I just recite to myself, over and over, until I can choose sleep:

It all adds up to normality.

Quarantine by Greg Egan

Quicksort in FIM++

Dear reader, I have got to tell you, fandom is intense. One day last October Equestria Daily (internet clearinghouse for fans of the animated series My Little Pony: Friendship Is Magic) posts a joke proposal for a programming language (FIM++) based on the show, and within the week there's a working interpreter for it. What does it mean to model a programming language after a cartoon, you ask? Well, in the show, episodes typically end with our heroine Twilight Sparkle (or after Season Two, Episode Three "Lesson Zero", one of her friends) writing a letter about what she's learned about the magic of friendship to her mentor (and God-Empress of the sun) Princess Celestia. So, then, why not have an esoteric programming langauge where the source code reads like a letter to Princess Celestia? Adorable, right?

So, this gift having been provided to us courtesy of Karol S. and the brony community, let's do something with it! More specifically, how about we implement quicksort?—that is a classic. What's quicksort? Well, we want to sort a list, right? So—bear with me—we define this partitioning procedure that, given indices into an array, partitions the subarray between those indices into a subsubarray of elements less-than-or-equal-to a given element dubbed the pivot, then the pivot itself, then a subsubarray of elements greater than the pivot. How do we do that? Well, let's designate the last element in our subarray as the pivot. Then we're going to scan through all the other elements, and if any of them are less-than-or-equal-to the pivot, we swap it into our first subsubarray and increment a variable keeping track of where the first subsubarray ends. Then, we swap the pivot into place and return its index. In Ruby—

def partition(array, p, r)
  i = p-1
  for j in p..(r-1) do
      if array[j] <= array[r]
        i += 1
        array[i], array[j] = array[j], array[i]
      end
  end
  array[i+1], array[r] = array[r], array[i+1]
  i+1
end

Then we can sort an entire array with a bunch of recursive calls to our partitioning procedure:

def quicksort(array, p, r)
  if p < r
    q = partition(array, p, r)
    quicksort(array, p, q-1)
    quicksort(array, q+1, r)
  end
end

# Let's try it!
my_array = [9, 5, 4, 11, 2, 10, 6, 3, 8, 12, 1, 7]
quicksort(my_array, 0, my_array.length-1)
print my_array # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

So that's quicksort. With a little more effort, we can do the same thing in FIM++:

Dear Princess Celestia: Letter about Quicksort:

I learned about exchange with Applejack, Rainbow Dash, and Rarity.

  On the page numbered by Rarity of Applejack I read about Sweetie
  Belle.

  On the page numbered by Rainbow Dash of Applejack I read about
  Scootaloo.

  On the page numbered by Rarity of Applejack I wrote what I knew
  about Scootaloo.

  On the page numbered by Rainbow Dash of Applejack I wrote what I
  knew about Sweetie Belle.

That's about exchange.


I learned about partitioning with Applejack, Rainbow Dash, and Rarity.

  On the page numbered by Rarity of Applejack I read about Apple
  Bloom.

  Sweetie Belle made the difference of Rainbow Dash and the number one.

  Did you know Scootaloo likes Rainbow Dash?

  I did this while Scootaloo had less than Rarity:

    On the page numbered by Scootaloo of Applejack I read about
    Diamond Tiara.

      When Diamond Tiara had not more than Apple Bloom:

        Sweetie Belle got one more.

        I did exchange of Applejack, Sweetie Belle, and Scootaloo.

      That's what I did.

    Scootaloo got one more.

  That's what I did.

  Sweetie Belle got one more.

  I also caused exchange of Applejack, Sweetie Belle, and Rarity.

That's about partitioning with Sweetie Belle.


I learned about quicksort with Applejack, Rainbow Dash, and Rarity:

  When Rainbow Dash had less than Rarity:

    Fluttershy did partitioning of Applejack, Rainbow Dash, and Rarity.

    Fluttershy got one less.

    I caused quicksort of Applejack, Rainbow Dash, and Fluttershy.

    Fluttershy got two more.

    I caused quicksort of Applejack, Fluttershy, and Rarity.

  That's what I did.

That's about quicksort.


Today I learned:

  Did you know Applejack likes 9, 5, 4, 11, 2, 10, 6, 3, 8, 12, 1, and 7?

  Applejack did dictionary of Applejack.

  I said: Applejack!

  I did quicksort of Applejack, one, and twelve.

  I said: Applejack! 

Your Faithful Student,
Twilight Sparkle

Bibliography Cormen et al., Introduction to Algorithms, third ed'n, §7.1

Compensation

"Maybe there should be an effort to cryopreserve specimens of endangered species. 'Hey, sorry we killed your entire species, but when we get more computing power later, we'll be sure to give you lots of happy runtime as compensation.'"

Guns

"Do you know, I've decided I like guns. Of course it would be preferable to wave a magic wand and have all sentient life live in peace and harmony in paradise forever. But if Reality puts you in a situation where you have to kill, at least we have tools to do it quickly: a well-aimed bang and there isn't a creature there to suffer for very long. That's actually a huge improvement over the state of nature, where animals kill with nothing but teeth and claws."