SlideShare a Scribd company logo
Algorithms & Data Structures CS112
                          Spring 2012
                            Lecture 4


                Syed Muhammad Raza
Searching Algorithms
   Searching is the process of determining whether or not a given value
    exists in a data structure or a storage media.

   We will study two searching algorithms

       Linear Search

       Binary Search
Linear Search: O(n)
   The linear (or sequential) search algorithm on an array is:

       Start from beginning of an array/list and continues until the item is
        found or the entire array/list has been searched.

       Sequentially scan the array, comparing each array item with the
        searched value.

       If a match is found; return the index of the matched element;
        otherwise return –1.

   Note: linear search can be applied to both sorted and unsorted
    arrays.
Linear Search
     bool LinSearch(double x[ ], int n, double item)
     {
             for(int i=0;i<n;i++)
             {
                 if(x[i]==item)
                 {
                        return true;
                 }
                 else
                 {
                         return false;
                 }
             }
             return false;
         }
Linear Search Tradeoffs
   Benefits
       Easy to understand
       Array can be in any order
   Disadvantages
       Inefficient for array of N elements
        Examines N/2 elements on average for value in array, N
        elements for value not in array
Binary Search: O(log2 n)
   Binary search looks for an item in an array/list using
    divide and conquer strategy
Binary Search
    Binary search algorithm assumes that the items in the array being
     searched is sorted

    The algorithm begins at the middle of the array in a binary
     search

    If the item for which we are searching is less than the item in the
     middle, we know that the item won’t be in the second half of the
     array

    Once again we examine the “middle” element

    The process continues with each comparison cutting in half the
     portion of the array where the item might be
Binary Search
   Binary search uses a recursive method to search an array to find a
    specified value

   The array must be a sorted array:

    a[0]≤a[1]≤a[2]≤. . . ≤ a[finalIndex]

   If the value is found, its index is returned

   If the value is not found, -1 is returned

   Note: Each execution of the recursive method reduces the search
    space by about a half
Pseudocode for Binary Search
Execution of Binary Search
Execution of Binary Search
Key Points in Binary Search
1.       There is no infinite recursion
     •      On each recursive call, the value of first is increased, or the
            value of last is decreased
     •      If the chain of recursive calls does not end in some other way,
            then eventually the method will be called with first larger than
            last

2.       Each stopping case performs the correct action for that case
     •      If first > last, there are no array elements between a[first] and
            a[last], so key is not in this segment of the array, and result is
            correctly set to -1
     •      If key == a[mid], result is correctly set to mid
Key Points in Binary Search
3.       For each of the cases that involve recursion, if all recursive calls
         perform their actions correctly, then the entire case performs
         correctly

     •      If key < a[mid], then key must be one of the elements a[first]
            through a[mid-1], or it is not in the array

     •      The method should then search only those elements, which it
            does

     •      The recursive call is correct, therefore the entire action is
            correct
Key Points in Binary Search
    •    If key > a[mid], then key must be one of the elements
         a[mid+1] through a[last], or it is not in the array

    •    The method should then search only those elements, which it
         does

    •    The recursive call is correct, therefore the entire action is
         correct

The method search passes all three tests:

Therefore, it is a good recursive method definition
Efficiency of Binary Search
   The binary search algorithm is extremely fast compared to an
    algorithm that tries all array elements in order

       About half the array is eliminated from consideration right at the
        start

       Then a quarter of the array, then an eighth of the array, and so
        forth
Efficiency of Binary Search
   Given an array with 1,000 elements, the binary search will only need
    to compare about 10 array elements to the key value, as compared
    to an average of 500 for a serial search algorithm

   The binary search algorithm has a worst-case running time that is
    logarithmic:   O(log n)

       A serial search algorithm is linear: O(n)

   If desired, the recursive version of the method search can be
    converted to an iterative version that will run more efficiently
Binary Search
Algorithms & Data Structures CSC-112
                              Fall 2011
                              Lecture 5



                Syed Muhammad Raza
Sorting Algorithms
   Sorting is the process of rearranging your data elements/Item in
    ascending or descending order
       Unsorted Data

                  4     3         2   7   1   6   5   8    9


       Sorted Data (Ascending)

                  1     2         3   4   5   6   7   8    9


       Sorted Data (Descending)


                  9     8         7   6   5   4   3   2    1
Sorting Algorithms
   They are many
       Bubble Sort
       Selection Sort
       Insertion Sort
       Shell sort
       Comb Sort
       Merge Sort
       Heap Sort
       Quick Sort
       Counting Sort
       Bucket Sort
       Radix Sort
       Distribution Sort
       Time Sort
                            Source: Wikipedia
Bubble Sort
   Compares Adjacent Items and Exchanges Them if They are Out of
    Order

   When You Order Successive Pairs of Elements, the Largest Element
    Bubbles to the Top(end) of the Array

   Bubble Sort (Usually) Requires Several Passes Through the Array
Bubble Sort

          Pass 1                       Pass 2

29   10    14      37   13   10   14     29     13   37

10   29    14      37   13   10   14     29     13   37

10   14    29      37   13   10   14     29     13   37

10   14    29      37   13   10   14     13     29   37

10   14    29      13   37
Selection Sort
   To Sort an Array into Ascending Order, First Search for the Largest Element

   Because You Want the Largest Element to be in the Last Position of the
    Array, You Swap the Last Item With the Largest Item to be in the Last Position of
    the Array, You Swap the Last Item with the Largest Item, Even if These Items
    Appear to be Identical

   Now, Ignoring the Last (Largest) Item of the Array, search Rest of the Array For
    Its Largest Item and Swap it With Its Last Item, Which is the Next-to-Last Item in
    the original Array

   You Continue Until You Have Selected and Swapped N-1 of the N Items in the
    Array

   The Remaining Item, Which is Now in the First Position of the Array, is in its
    Proper Order
Selection Sort



       Initial Array   29   10   14   37   13

      After1st Swap    29   10   14   13   37

      After2nd Swap    13   10   14   29   37

      After3rd Swap    13   10   14   29   37

      After4th Swap    10   13   14   29   37
Insertion Sort
   Divide Array Into Sorted Section At Front (Initially Empty), Unsorted
    Section At End

   Step Iteratively Through Array, Moving Each Element To Proper
    Place In Sorted Section

                     Sorted                           Unsorted


                  …..                                    …..

           0                           i                          N-1
                                After i Iterations
Insertion Sort

Initial Array   29   10   14   37   13      Copy 10

                29   29   14   37   13      Shift 29

                10   29   14   37   13   Insert 10, Copy 14


                10   29   29   37   13      Shift 29

                                         Insert 14; Copy 37
                10   14   29   37   13
                                         Insert 37 on Itself

                10   14   29   37   13      Copy 13

                10   14   14   29   37   Shift 14, 29, 37


Sorted Array    10   13   14   29   37       Insert 13

More Related Content

PPTX
Binary search
Raghu nath
 
PPT
Data Structures - Searching & sorting
Kaushal Shah
 
PPTX
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
PPT
Sorting Algorithms
multimedia9
 
PDF
Linear search algorithm
NeoClassical
 
PDF
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
PPT
L10 sorting-searching
mondalakash2012
 
DOC
Insertion sort
Dorina Isaj
 
Binary search
Raghu nath
 
Data Structures - Searching & sorting
Kaushal Shah
 
Sorting and searching arrays binary search algorithm
David Burks-Courses
 
Sorting Algorithms
multimedia9
 
Linear search algorithm
NeoClassical
 
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
L10 sorting-searching
mondalakash2012
 
Insertion sort
Dorina Isaj
 

What's hot (20)

PPTX
Searching & Sorting Algorithms
Rahul Jamwal
 
PPSX
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Dharmendra Prasad
 
PPT
Sorting Seminar Presentation by Ashin Guha Majumder
Ashin Guha Majumder
 
PDF
Sorting Algorithms
Mohammed Hussein
 
PPT
Sorting
Ghaffar Khan
 
PPT
Lect11 Sorting
ryokollll
 
PPTX
Sorting algorithms
Eleonora Ciceri
 
PPT
Searching Sorting
guest2cb109
 
PPTX
sorting algorithm graphical method
Shantanu Mishra
 
PPTX
Sorting algorithms
Maher Alshammari
 
PPT
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Tiểu Hổ
 
PPTX
Sorting algorithms
Trupti Agrawal
 
PPTX
Rahat &amp; juhith
Rj Juhith
 
PPTX
Coin Changing, Binary Search , Linear Search - Algorithm
Md Sadequl Islam
 
PPTX
Binary search
AparnaKumari31
 
PPT
Sorting & Linked Lists
J.T.A.JONES
 
PDF
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
PPT
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
 
PPTX
Insertion Sort, Quick Sort And Their complexity
Motaleb Hossen Manik
 
PPTX
Dsa – data structure and algorithms searching
sajinis3
 
Searching & Sorting Algorithms
Rahul Jamwal
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Dharmendra Prasad
 
Sorting Seminar Presentation by Ashin Guha Majumder
Ashin Guha Majumder
 
Sorting Algorithms
Mohammed Hussein
 
Sorting
Ghaffar Khan
 
Lect11 Sorting
ryokollll
 
Sorting algorithms
Eleonora Ciceri
 
Searching Sorting
guest2cb109
 
sorting algorithm graphical method
Shantanu Mishra
 
Sorting algorithms
Maher Alshammari
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Tiểu Hổ
 
Sorting algorithms
Trupti Agrawal
 
Rahat &amp; juhith
Rj Juhith
 
Coin Changing, Binary Search , Linear Search - Algorithm
Md Sadequl Islam
 
Binary search
AparnaKumari31
 
Sorting & Linked Lists
J.T.A.JONES
 
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
(Data Structure) Chapter11 searching & sorting
Fadhil Ismail
 
Insertion Sort, Quick Sort And Their complexity
Motaleb Hossen Manik
 
Dsa – data structure and algorithms searching
sajinis3
 
Ad

Similar to Algorithm & data structures lec4&5 (20)

PDF
advanced searching and sorting.pdf
haramaya university
 
PPTX
Unit vii sorting
Tribhuvan University
 
PPTX
Searching searching in in arrays arrays.pptx
Sahar160629
 
PPTX
Unit 7 sorting
Dabbal Singh Mahara
 
PPTX
Data Structures_ Sorting & Searching
ThenmozhiK5
 
PPTX
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
PPTX
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
PPTX
Analysis of Algorithm - Binary Search.pptx
Maulana Abul Kalam Azad University of Technology
 
PPTX
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali702240
 
PDF
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
mylinhbangus
 
PPT
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
PPTX
Searching and Sorting Algorithms in Data Structures
poongothai11
 
DOCX
Sorting
BHARATH KUMAR
 
PPT
Searching Sorting-SELECTION ,BUBBBLE.ppt
kunalpatil5661
 
PPTX
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
 
PPTX
Sorting-Algorithms-A-Comprehensive-Guide.pptx
ReemEmad26
 
DOCX
MODULE 5-Searching and-sorting
nikshaikh786
 
PDF
Unit 6 dsa SEARCHING AND SORTING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PPTX
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
tahliildhoore54
 
PPT
Sorting algorithms
CHANDAN KUMAR
 
advanced searching and sorting.pdf
haramaya university
 
Unit vii sorting
Tribhuvan University
 
Searching searching in in arrays arrays.pptx
Sahar160629
 
Unit 7 sorting
Dabbal Singh Mahara
 
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
Analysis of Algorithm - Binary Search.pptx
Maulana Abul Kalam Azad University of Technology
 
Algorithm 8th lecture linear & binary search(2).pptx
Aftabali702240
 
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
mylinhbangus
 
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Searching and Sorting Algorithms in Data Structures
poongothai11
 
Sorting
BHARATH KUMAR
 
Searching Sorting-SELECTION ,BUBBBLE.ppt
kunalpatil5661
 
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
 
Sorting-Algorithms-A-Comprehensive-Guide.pptx
ReemEmad26
 
MODULE 5-Searching and-sorting
nikshaikh786
 
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
tahliildhoore54
 
Sorting algorithms
CHANDAN KUMAR
 
Ad

More from Abdul Khan (9)

PPT
Lec 04 intro assembly
Abdul Khan
 
PPTX
Algorithm & data structure lec2
Abdul Khan
 
PPTX
Algorithm & data structures lec1
Abdul Khan
 
PPT
Lec 03 ia32 architecture
Abdul Khan
 
PPT
Lec 02 data representation part 2
Abdul Khan
 
PPT
Lec 02 data representation part 1
Abdul Khan
 
PPT
Lec 01 basic concepts
Abdul Khan
 
PPTX
war on terror
Abdul Khan
 
PPT
Xhtml
Abdul Khan
 
Lec 04 intro assembly
Abdul Khan
 
Algorithm & data structure lec2
Abdul Khan
 
Algorithm & data structures lec1
Abdul Khan
 
Lec 03 ia32 architecture
Abdul Khan
 
Lec 02 data representation part 2
Abdul Khan
 
Lec 02 data representation part 1
Abdul Khan
 
Lec 01 basic concepts
Abdul Khan
 
war on terror
Abdul Khan
 
Xhtml
Abdul Khan
 

Recently uploaded (20)

PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PPT
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Coupa-Kickoff-Meeting-Template presentai
annapureddyn
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
This slide provides an overview Technology
mineshkharadi333
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 

Algorithm & data structures lec4&5

  • 1. Algorithms & Data Structures CS112 Spring 2012 Lecture 4 Syed Muhammad Raza
  • 2. Searching Algorithms  Searching is the process of determining whether or not a given value exists in a data structure or a storage media.  We will study two searching algorithms  Linear Search  Binary Search
  • 3. Linear Search: O(n)  The linear (or sequential) search algorithm on an array is:  Start from beginning of an array/list and continues until the item is found or the entire array/list has been searched.  Sequentially scan the array, comparing each array item with the searched value.  If a match is found; return the index of the matched element; otherwise return –1.  Note: linear search can be applied to both sorted and unsorted arrays.
  • 4. Linear Search bool LinSearch(double x[ ], int n, double item) { for(int i=0;i<n;i++) { if(x[i]==item) { return true; } else { return false; } } return false; }
  • 5. Linear Search Tradeoffs  Benefits  Easy to understand  Array can be in any order  Disadvantages  Inefficient for array of N elements Examines N/2 elements on average for value in array, N elements for value not in array
  • 6. Binary Search: O(log2 n)  Binary search looks for an item in an array/list using divide and conquer strategy
  • 7. Binary Search  Binary search algorithm assumes that the items in the array being searched is sorted  The algorithm begins at the middle of the array in a binary search  If the item for which we are searching is less than the item in the middle, we know that the item won’t be in the second half of the array  Once again we examine the “middle” element  The process continues with each comparison cutting in half the portion of the array where the item might be
  • 8. Binary Search  Binary search uses a recursive method to search an array to find a specified value  The array must be a sorted array: a[0]≤a[1]≤a[2]≤. . . ≤ a[finalIndex]  If the value is found, its index is returned  If the value is not found, -1 is returned  Note: Each execution of the recursive method reduces the search space by about a half
  • 12. Key Points in Binary Search 1. There is no infinite recursion • On each recursive call, the value of first is increased, or the value of last is decreased • If the chain of recursive calls does not end in some other way, then eventually the method will be called with first larger than last 2. Each stopping case performs the correct action for that case • If first > last, there are no array elements between a[first] and a[last], so key is not in this segment of the array, and result is correctly set to -1 • If key == a[mid], result is correctly set to mid
  • 13. Key Points in Binary Search 3. For each of the cases that involve recursion, if all recursive calls perform their actions correctly, then the entire case performs correctly • If key < a[mid], then key must be one of the elements a[first] through a[mid-1], or it is not in the array • The method should then search only those elements, which it does • The recursive call is correct, therefore the entire action is correct
  • 14. Key Points in Binary Search • If key > a[mid], then key must be one of the elements a[mid+1] through a[last], or it is not in the array • The method should then search only those elements, which it does • The recursive call is correct, therefore the entire action is correct The method search passes all three tests: Therefore, it is a good recursive method definition
  • 15. Efficiency of Binary Search  The binary search algorithm is extremely fast compared to an algorithm that tries all array elements in order  About half the array is eliminated from consideration right at the start  Then a quarter of the array, then an eighth of the array, and so forth
  • 16. Efficiency of Binary Search  Given an array with 1,000 elements, the binary search will only need to compare about 10 array elements to the key value, as compared to an average of 500 for a serial search algorithm  The binary search algorithm has a worst-case running time that is logarithmic: O(log n)  A serial search algorithm is linear: O(n)  If desired, the recursive version of the method search can be converted to an iterative version that will run more efficiently
  • 18. Algorithms & Data Structures CSC-112 Fall 2011 Lecture 5 Syed Muhammad Raza
  • 19. Sorting Algorithms  Sorting is the process of rearranging your data elements/Item in ascending or descending order  Unsorted Data 4 3 2 7 1 6 5 8 9  Sorted Data (Ascending) 1 2 3 4 5 6 7 8 9  Sorted Data (Descending) 9 8 7 6 5 4 3 2 1
  • 20. Sorting Algorithms  They are many  Bubble Sort  Selection Sort  Insertion Sort  Shell sort  Comb Sort  Merge Sort  Heap Sort  Quick Sort  Counting Sort  Bucket Sort  Radix Sort  Distribution Sort  Time Sort Source: Wikipedia
  • 21. Bubble Sort  Compares Adjacent Items and Exchanges Them if They are Out of Order  When You Order Successive Pairs of Elements, the Largest Element Bubbles to the Top(end) of the Array  Bubble Sort (Usually) Requires Several Passes Through the Array
  • 22. Bubble Sort Pass 1 Pass 2 29 10 14 37 13 10 14 29 13 37 10 29 14 37 13 10 14 29 13 37 10 14 29 37 13 10 14 29 13 37 10 14 29 37 13 10 14 13 29 37 10 14 29 13 37
  • 23. Selection Sort  To Sort an Array into Ascending Order, First Search for the Largest Element  Because You Want the Largest Element to be in the Last Position of the Array, You Swap the Last Item With the Largest Item to be in the Last Position of the Array, You Swap the Last Item with the Largest Item, Even if These Items Appear to be Identical  Now, Ignoring the Last (Largest) Item of the Array, search Rest of the Array For Its Largest Item and Swap it With Its Last Item, Which is the Next-to-Last Item in the original Array  You Continue Until You Have Selected and Swapped N-1 of the N Items in the Array  The Remaining Item, Which is Now in the First Position of the Array, is in its Proper Order
  • 24. Selection Sort Initial Array 29 10 14 37 13 After1st Swap 29 10 14 13 37 After2nd Swap 13 10 14 29 37 After3rd Swap 13 10 14 29 37 After4th Swap 10 13 14 29 37
  • 25. Insertion Sort  Divide Array Into Sorted Section At Front (Initially Empty), Unsorted Section At End  Step Iteratively Through Array, Moving Each Element To Proper Place In Sorted Section Sorted Unsorted ….. ….. 0 i N-1 After i Iterations
  • 26. Insertion Sort Initial Array 29 10 14 37 13 Copy 10 29 29 14 37 13 Shift 29 10 29 14 37 13 Insert 10, Copy 14 10 29 29 37 13 Shift 29 Insert 14; Copy 37 10 14 29 37 13 Insert 37 on Itself 10 14 29 37 13 Copy 13 10 14 14 29 37 Shift 14, 29, 37 Sorted Array 10 13 14 29 37 Insert 13