Insertion Sort - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Some concept of Data Structures are Abstract, Balance Factor, Complete Binary Tree, Dynamically, Storage, Implementation, Sequential Search, Advanced Data Structures, Graph Coloring Two, Insertion Sort. Main points of this lecture are: Insertion Sort, Shellsort, Importance of Sorting, Explanation, Runtime, Advantage and Disadvantage, History, Explanation, Advantage, Containing

Typology: Slides

2012/2013

Uploaded on 04/30/2013

dinpal
dinpal 🇮🇳

3.6

(12)

73 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Insertion Sort & Shellsort
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Insertion Sort - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Insertion Sort & Shellsort

Outline

  • Importance of Sorting
  • Insertion Sort
    • Explanation
    • Runtime
    • Advantage and Disadvantage
    • Walk through example
  • Shell Sort
    • History
    • Explanation
    • Runtime
    • Advantage and Disadvantage
    • Walk through example

Why we do sorting?

  • Searching for an element in an array will be more efficient. (example: looking up for information like phone number).
  • It’s always nice to see data in a sorted display. (example: spreadsheet or database application).
  • Computers sort things much faster.

History of Sorting

  • Sorting is one of the most important operations performed by computers. In the days of magnetic tape storage before modern databases, database updating was done by sorting transactions and merging them with a master file.

Insertion Sort

  • Insertion sort keeps making the left side of the array sorted until the whole array is sorted. It sorts the values seen far away and repeatedly inserts unseen values in the array into the left sorted array.
  • It is the simplest of all sorting algorithms.
  • Although it has the same complexity as Bubble Sort, the insertion sort is a little over twice as efficient as the bubble sort.

Insertion Sort

  • Real life example:
    • An example of an insertion sort occurs in everyday life while playing cards. To sort the cards in your hand you extract a card, shift the remaining cards, and then insert the extracted card in the correct place. This process is repeated until all the cards are in the correct sequence.

Empirical Analysis of Insertion Sort

Source: http://linux.wku.edu/~lamonml/algor/sort/insertion.html

The graph demonstrates the n^ 2 complexity of the insertion sort.

Insertion Sort

  • The insertion sort is a good choice for sorting lists of a few thousand items or less.

Insertion Sort

  • This algorithm is much simpler than the shell sort, with only a small trade-off in efficiency. At the same time, the insertion sort is over twice as fast as the bubble sort.

Advantage of Insertion Sort

  • The advantage of Insertion Sort is that it is relatively simple and easy to implement.

Insertion Sort Example

  • Sort: 34 8 64 51 32 21
  • 34 8 64 51 32 21
  • The algorithm sees that 8 is smaller than 34 so it swaps.
  • 8 34 64 51 32 21
  • 51 is smaller than 64, so they swap.
  • 8 34 51 64 32 21

Insertion Sort Example

  • Sort: 34 8 64 51 32 21
  • 8 34 51 64 32 21 (from previous slide)
  • The algorithm sees 32 as another smaller number and moves it to its appropriate location between 8 and 34.
  • 8 32 34 51 64 21
  • The algorithm sees 21 as another smaller number and moves into between 8 and 32.
  • Final sorted numbers:
  • 8 21 32 34 51 64

Shellsort

  • Shellsort uses a sequence h 1 , h 2 , …, ht called the increment sequence. Any increment sequence is fine as long as h 1 = 1 and some other choices are better than others.

Shellsort

  • Shellsort makes multiple passes through a list and sorts a number of equally sized sets using the insertion sort.