Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Successfully reported this slideshow.

Like this presentation? Why not share!

2,055 views

Published on

insertion sorting

Published in:
Technology

No Downloads

Total views

2,055

On SlideShare

0

From Embeds

0

Number of Embeds

4

Shares

0

Downloads

134

Comments

0

Likes

3

No embeds

No notes for slide

- 1. The Insertion Sort Mr. Dave ClausenLa Cañada High School
- 2. Insertion Sort DescriptionThe insertion sort uses a vectors partial ordering. Onthe kth pass, the kth item should be inserted into itsplace among the first k items in the vector.After the kth pass (k starting at 1), the first k items ofthe vector should be in sorted order.This is like the way that people pick up playing cardsand order them in their hands. When holding the first(k - 1) cards in order, a person will pick up the kthcard and compare it with cards already held until itssorted spot is found. Mr. Dave Clausen 2
- 3. Insertion Sort AlgorithmFor each k from 1 to n - 1 (k is the index of vector element to insert) Set item_to_insert to v[k] Set j to k - 1 (j starts at k - 1 and is decremented until insertion position is found) While (insertion position not found) and (not beginning of vector) If item_to_insert < v[j] Move v[j] to index position j + 1 Decrement j by 1 Else The insertion position has been found Mr. Dave Clausen 3 item_to_insert should be positioned at index
- 4. C + + Code For Insertion Sort void Insertion_Sort(apvector<int> &v) { int item_to_insert, j; // On the kth pass, insert item k into its correct bool still_looking; // position among the first k entries in vector. for (int k = 1; k < v.length(); ++k) { // Walk backwards through list, looking for slot to insert v[k] item_to_insert = v[k]; j = k - 1; still_looking = true; while ((j >= 0) && still_looking ) if (item_to_insert < v[j]) { v[j + 1] = v[j]; --j; } else Dave Clausen Mr. 4 still_looking = false; // Upon
- 5. Insertion Sort Example The Unsorted Vector: 80 40For each pass, the index j begins at 32the (k - 1)st item and moves that 84item to position j + 1 until we findthe insertion point for what was 61originally the kth item.We start with k = 1and set j = k-1 or 0 (zero) Mr. Dave Clausen 5
- 6. The First Pass K=280 Insert 40, 80 Insert 40 4040 compare 80 80 & move32 32 3284 84 8461 61 61 item_to_insert 40 Mr. Dave Clausen 6
- 7. The Second Pass K=340 40 Compare 40 Insert 32 32 & move80 Insert 32, 80 40 4032 compare 80 80 80 & move84 84 84 8461 61 61 61 item_to_insert 32 Mr. Dave Clausen 7
- 8. The Third PassK=4 324080 Insert 84?84 compare & stop61 item_to_insert 84 Mr. Dave Clausen 8
- 9. The Fourth Pass K=532 32 32 3240 Compare 40 40 40 & stop80 80 Compare 80 Insert 61 61 & move84 Insert 61, 84 80 8061 compare 84 84 84 & move item_to_insert 61 Mr. Dave Clausen 9
- 10. What “Moving” Meansitem_to_insert 80 40 40 32Place the second element 84into the variable 61item_to_insert. Mr. Dave Clausen 10
- 11. What “Moving” Meansitem_to_insert 80 80 40 32Replace the second element 84with the value of the first 61element. Mr. Dave Clausen 11
- 12. What “Moving” Meansitem_to_insert 40 80 40 32Replace the first element 84(in this example) with the 61variable item_to_insert. Mr. Dave Clausen 12
- 13. C + + Examples of The Insertion SortOn the Net:http://compsci.exeter.edu/Winter99/CS320/Resources/sortDemo.htmlhttp://www.aist.go.jp/ETL/~suzaki/AlgorithmAnimation/index.html Mr. Dave Clausen 13
- 14. Big - O NotationBig - O notation is used to describe the efficiencyof a search or sort. The actual time necessary tocomplete the sort varies according to the speed ofyour system. Big - O notation is an approximatemathematical formula to determine how manyoperations are necessary to perform the search orsort. The Big - O notation for the Insertion Sort isO(n2), because it takes approximately n2 passes tosort the “n” elements. Mr. Dave Clausen 14

No public clipboards found for this slide

Be the first to comment