Presentation is loading. Please wait.

Presentation is loading. Please wait.

Insertion Sort Suppose that you have a hand of cards that you want to sort – in this case, assume that you have all thirteen cards of one suit. One way.

Similar presentations


Presentation on theme: "Insertion Sort Suppose that you have a hand of cards that you want to sort – in this case, assume that you have all thirteen cards of one suit. One way."— Presentation transcript:

1 Insertion Sort Suppose that you have a hand of cards that you want to sort – in this case, assume that you have all thirteen cards of one suit. One way to do this is to put the cards in your hand one at a time, in order. So, you start with one card in your hand, say the 4. Then you take the next card and, starting from the right, slide the cards that are bigger towards the right until you come to the right spot.

2 Insertion Sort (cont'd) That is, you keep sliding over cards which are bigger until you find a card that is smaller, or you run out of cards. Then you insert the new card in that spot. You repeat this until all the cards have been placed into the hand.

3 Insertion Sort Example

4 Running Time In the worst case, we have to shift over all the cards each time, so that's 1 card, then 2 cards, then 3 cards,... for a total for n*(n-1)/2. That's the same as Selection Sort and Bubble Sort. In the best case, we don't have to shift over any cards, so the time is proportional to the number of cards. That's called linear time.

5 When to Use Each Sort Insertion sort is fast when the data is in order, or nearly in order, that is, either a few items are out of place, or many are out of place, but near to where they should be. Selection sort is used when it is expensive to swap items, since each item is swapped just once. That means when we are copying large items. Bubble sort is good for tests – it's only four lines.

6 Bubble Sort Revisited We can make Bubble Sort a little more efficient (in the best and and average case) by checking if we actually do any swaps. If no swaps are done, then the list is in order, and we can stop sorting.

7 Random Algorithms An interesting class of algorithms are random algorithms, or stochastic algorithms. We use (pseudo-) random numbers to help us in solving a problem. For example, we can estimate the value of Pi (the ratio of the circumference of a circle to it s diameter) by doing the following:

8 Estimating Pi Draw a square, say two feet per side on the ground. Inside the square, draw a circle of diameter two feet. From a distance, throw pennies onto the square and count the number of pennies that land in the circle.

9 Estimating Pi (cont'd) Since the fraction of pennies that land in the circle is proportional to the size of the circle vs. the square, we can estimate Pi. Pi = 4 * number of pennies inside the circle/number of pennies thrown.


Download ppt "Insertion Sort Suppose that you have a hand of cards that you want to sort – in this case, assume that you have all thirteen cards of one suit. One way."

Similar presentations


Ads by Google