# Write a recurrence for the running time of insertion sort python

Algorithm[ edit ] A graphical example of insertion sort. The partial sorted list black initially contains only the first element in the list. With each iteration one element red is removed from the input data and inserted in-place into the sorted list. So today's lecture is on sorting. We'll be talking about specific sorting algorithms today. I want to start by motivating why we're interested in sorting, which should be fairly easy.

Then I want to discuss a particular sorting algorithm that's called insertion sort. That's probably the simplest sorting algorithm you can write, it's five lines of code. It's not the best sorting algorithm that's out there and so we'll try and improve it.

• The Insertion Sort — Problem Solving with Algorithms and Data Structures
• Analysis of selection sort (article) | Khan Academy

We'll also talk about merge sort, which is a divide and conquer algorithm and that's going to motivate the last thing that I want to spend time on, which is recurrences and how you solve recurrences.

Typically the recurrences that we'll be looking at in double o six are going to come from divide and conquer problems like merge sort but you'll see this over and over. So let's talk about why we're interested in sorting.

There's some fairly obvious applications like if you want to maintain a phone book, you've got a bunch of names and numbers corresponding to a telephone directory and you want to keep them in sorted order so it's easy to search, mp3 organizers, spreadsheets, et cetera.

[BINGSNIPMIX-3

So there's lots of obvious applications. There's also some interesting problems that become easy once items are sorted.

One example of that is finding a median. So let's say that you have a bunch of items in an array a zero through n and a zero through n contains n numbers and they're not sorted. When you sort, you turn this into b 0 through n, where if it's just numbers, then you may sort them in increasing order or decreasing order. Let's just call it increasing order for now. Or if they're records, and they're not numbers, then you have to provide a comparison function to determine which record is smaller than another record.

And that's another input that you have to have in order to do the sorting. So it doesn't really matter what the items are as long as you have the comparison function.

Think of it as less than or equal to. And if you have that and it's straightforward, obviously, to check that 3 is less than 4, et cetera. But it may be a little more complicated for more sophisticated sorting applications.

But the bottom line is that if you have your algorithm that takes a comparison function as an input, you're going to be able to, after a certain amount of time, get B 0 n.

Now if you wanted to find the median of the set of numbers that were originally in the array A, what would you do once you have the sorted array B? Isn't there a more efficient algorithm for median? But this is sort of a side effect of having a sorted list. If you happen to have a sorted list, there's many ways that you could imagine building up a sorted list.

One way is you have something that's completely unsorted and you run insertion sort or merge sort. Another way would be to maintain a sorted list as you're getting items put into the list.

So if you happened to have a sorted list and you need to have this sorted list for some reason, the point I'm making here is that finding the median is easy.

And it's easy because all you have to do is look at-- depending on whether n is odd or even-- look at B of n over 2.

That would give you the median because you'd have a bunch of numbers that are less than that and the equal set of numbers that are greater than that, which is the definition of median. So this is not necessarily the best way, as you pointed out, of finding the median.

But it's constant time if you have a sorted list. That's the point I wanted to make.So, if every element is greater than or equal to every element to its left, the running time of insertion sort is Θ (n) \Theta(n) Θ (n).

This situation occurs if the array starts out already sorted, and so an already-sorted array is the best case for insertion sort. The Insertion Sort¶.