Question

I know there is a sort function:

>>> a = 'bags'
>>> ''.join(sorted(a))
'abgs'

However, I need to write mine from scratch. I think I would like to use mergesort but I'm not sure how that would work for a string in Python. E.g., can I compare characters? Can I find the middle of a string somehow?

I'm using Python 3.4.

Was it helpful?

Solution

Yes you could compare characters.

b > a evaluates to True in Python, and so forth.

You could first convert the string to a list, and get it's middle via its length, do sequential comparison and then join the sorted list to get back a sorted string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top