SlideShare une entreprise Scribd logo
1  sur  17
0 1 2     3 4 5
      23 78 45   8 32 56
i=0                        min=23   for (i=0;i<n;i++)
j=1                        min=23      minVal=arr[i]
j=2                        min=23      for (j=i+1;j<n;j++)
j=3                        min=8          if minVal >arr[j]
j=4                        min=8              minVal=arr[j]
j=5                        min=8
      8 78 45 23 32 56
i=1
      8 23 45 78 32 56
Loop 1                                                 Loop 2
pass 1   23   14   78   45    8   32   56   Original   j=1      swap      23   14
         14   23   78   45    8   32   56              j=2      no swap
         14   23   78   45    8   32   56              j=3      swap      78   45
         14   23   45   78    8   32   56              j=4      swap      78    8
         14   23   45    8   78   32   56              j=5      swap      78   32
         14   23   45    8   32   78   56              j=6      swap      78   56

         14 23 45        8 32 56 78




                                            SORTED
23      78 45   8 32 56   Original



Insert 78                      23 78      45   8 32 56   Pass 1



Insert 45 after 78 before 23   23 45 78        8 32 56   Pass 2




Insert 8 before 23              8 23 45 78       32 56   Pass 3



insert 32 after 45 before 23    8 23 32 45 78       56   Pass 4



Insert 56 after 78              8 23 32 45 56 78         Pass 5

                                                         SORTED
Wall


 1    j      k         n


place k element in appropriate place
by walking back from j
Array to be Sorted   72 62 14       9 30 21 80 25 70 55


Arr Index             0 1      2    3    4  5 6         7    8    9
1st Incre k=5        Walker               Current
                     72 62    14    9   30 21 80       25   70   55   SWAP
                     21 62    14    9   30 77 80       25   70   55
                     21 62    14    9   30 77 80       25   70   55
                     21 62    14    9   30 77 80       25   70   55                       Why Insertion Sort ??
                     21 62    14    9   30 77 80       25   70   55

                                                                                            9
Arr Index             0 1      2  3 4        5     6    7    8    9                       arr[1]
1st Incre k=3        Walker     Current
                     21 62    14 9 30        77   80   25   70   55   SWAP
                      9 62    14 21 30       77   80   25   70   55   SWAP
                      9 30    14 21 62       77   80   25   70   55                         9
                      9 30    14 21 62       77   80   25   70   55                       arr[1]
                      9 30    14 21 62       77   80   25   70   55   SWAP     FALLBACK            25 is placed in th
                      9 25    14 21 30       77   80   62   70   55   SWAP                         consider only ou
                      9 25    14 21 30       70   80   62   77   55   SWAP
                      9 25    14 21 30       70   55   62   77   80


Arr Index             0 1 2         3    4   5     6    7    8    9
1st Incre k=1        Walker
                        Current
                      9 25 14      21   30   70   55   62   77   80
                      9 25 14      21   30   70   55   62   77   80   SWAP
                      9 14 25      21   30   70   55   62   77   80   SWAP
                      9 14 21      25   30   70   55   62   77   80
                      9 14 21      25   30   70   55   62   77   80
                      9 14 21      25   30   70   55   62   77   80   SWAP
                      9 14 21      25   30   55   70   62   77   80   SWAP
                      9 14 21      25   30   55   62   70   77   80
                      9 14 21      25   30   55   62   70   77   80   SORTED
Why Insertion Sort ??
                                              Wall

           15            22        36                25          6
          arr[1+k]      arr[1+2k] arr[1+3k]          arr[1+4k] arr[1+5k]

                                                           Wall

            15          22          25         36                6
           arr[1+k] arr[1+2k] arr[1+3k]              arr[1+4k] arr[1+5k]
       25 is placed in the inserted when we
       consider only our segment
78   21   14   97   87   62   74   85   76   45   84   22
                             62   21   14   97   87   78   74   85   76   45   84   22
                             22   21   14   97   87   78   74   85   76   45   84   62
Determine Pivot              22   21   14   97   87   62   74   85   76   45   84   78

array Index                   0 1 2 3 4 5 6 7 8 9 10 11
                             62 21 14 97 87 22 74 85 76 45 84 78
Sort Left:1                                                                              Looking for number > than 62 (pivot)
Sort Left:2
Sort Left:3
Sort Right :11                                                                           Looking for number < than 62 (pivot)
Sort Right :10
Sort Right :9
                             62 21 14 45 87 22 74 85 76 97 84 78 SWAP
                                        …
Sort Left :4
Sort Right moves to 5
                             62 21 14 45 22 87 74 85 76 97 84 78 SWAP
0,11
                        62 22 21 14 45 62 87 74 85 76 97 84 78 Move Pivot Back
                             < 62                 >=62
0,3
                        22
5,11                         14 21 22 45 62 84 74 85 76 78 87 97
                        87
0,1
                        14
3,3                          14 21 22 45 62 76 74 78 84 85
                        45
1,1
                        21
5,9
84
5,7
        76 14 21 22 45 62 74 76 78 84 85
5,5
        74
7,7
        78
11,11
        85
or number > than 62 (pivot)


or number < than 62 (pivot)
Merge                                         i                     j                                        i
                                                  14 20 36              10 12 30 40 44                           14 20

aux                                               10 12 14



Merge Sort
                                                  12 30 21      8        6   9   1   7               12 30 21        8

                                                  12 30     8 21         6   9   1   7               12 30 21        8

                                                   8 12 21 30            1   6   7   9

                                                   1   6    7   8        9 12 21 30


Recursive                                         12 30 21      8        6   9   1   7
Break into 2 array                                12 30 21      8                6   9   1   7
Break into 4 arrays                               12 30                 21   8       6   9       1   7

Break into 8 arrays                               12       30           21       8       6       9       1           7

Now each single elemnet array is sorted !!!
SO Merge                                          12 30         8 21             6   9       1   7

Merge Again                                        8 12 21 30                    1   6   7   9

Merge Again                                        1   6    7   8        9 12 21 30
m m+1          j
 36 10 12 30 40 44




 6   9     1    7

 6   9     1    7




         MergeSort(arr,start,end)
            //if only 1 elemnet
            if (start==end) return;
            //divide into two equal parts
            m=(i+j)/2
            mergeSort(arr,start,middle)
            mergeSort(arr,middle+1,end)
            merge(arr,start,middle,end)
MergeSort(arr,start,end)
          //if only 1 element
          if (start==end) return;
          //divide into two equal parts
          m=(i+j)/2
          mergeSort(arr,start,middle)
          mergeSort(arr,middle+1,end)
          merge(arr,start,middle,end)

          Let us analyse Recursion in Merge Sort

          Let us say an array has 8 elements, how many times will MergeSort get called
                                                          Start       End
                                                                    0         7
                                                                    0         3
                                                                    0         1
                                                                    0         0
                                                                    1         1
                                                                    2         3
                                                                    2         2
                                                                    3         3
                                                                    4         7
                                                                    4         5
                                                                    4         4
                                                                    5         5
                                                                    6         7
                                                                    6         6
                                                                    7         7
          If 100 the call would be ?                              199
Create Heap by using reheapUp                    Original

After Heap            78 32 56      8 23 45


reheapDown            56 32 45      8 23       78 Pass 1


                      45 32 23      8      56 78 Pass 2


                      32   8 23         45 56 78 Pass 3


                      23   8       32 45 56 78 Pass 4


                       8        23 32 45 56 78 Pass 5


Sorted                     8 23 32 45 56 78 Pass 6
111 112 113 114 115 211 311 411 511 611 121 131 141 151 161


Unit Digit
          0
          1     111 211 311 411 511 611 121 131 141 151 161
          2     112
          3     113
          4     114
          5     115
          6
….

Ten Digit                                                                     Ten Digit
            0                                                                             0
            1   111 211 311 411 511 611 112 113 114 115                                   1   111 112 113
            2   121                                                                       2   211
            3   131                                                                       3   311
            4   141                                                                       4   411
            5   151                                                                       5   511
            6   161                                                                       6   611
….                                                                            ….
                                                                              SORTED          111 112 113
114 115 121 131 141 151 161




114 115 121 131 141 151 161 211 311 411 511 611
Created by Jibrael Jos to explain Sort to M.Sc/MCA Students
     Jul-09
Christ College

Contenu connexe

En vedette

Data Structures, Graphs
Data Structures, GraphsData Structures, Graphs
Data Structures, GraphsJibrael Jos
 
Grant Research 101
Grant Research 101Grant Research 101
Grant Research 101dereklink
 
Efidondenadietelleva Jd Ppt
Efidondenadietelleva Jd PptEfidondenadietelleva Jd Ppt
Efidondenadietelleva Jd PptKelly Automotive
 
Systems and Chaos Theory
Systems and Chaos TheorySystems and Chaos Theory
Systems and Chaos Theoryguest2a16eb
 
Steven Covey 7 Habits of Highly Effective People
Steven Covey 7 Habits of Highly Effective PeopleSteven Covey 7 Habits of Highly Effective People
Steven Covey 7 Habits of Highly Effective PeopleJibrael Jos
 
Data Structures : AVL Trees
Data Structures : AVL TreesData Structures : AVL Trees
Data Structures : AVL TreesJibrael Jos
 
Followership, A Leadership Workshop
Followership, A Leadership WorkshopFollowership, A Leadership Workshop
Followership, A Leadership WorkshopJibrael Jos
 
BTree, Data Structures
BTree, Data StructuresBTree, Data Structures
BTree, Data StructuresJibrael Jos
 

En vedette (10)

Alex Hultgren Ppt
Alex Hultgren PptAlex Hultgren Ppt
Alex Hultgren Ppt
 
2007 Irt Attendee List
2007 Irt Attendee List2007 Irt Attendee List
2007 Irt Attendee List
 
Data Structures, Graphs
Data Structures, GraphsData Structures, Graphs
Data Structures, Graphs
 
Grant Research 101
Grant Research 101Grant Research 101
Grant Research 101
 
Efidondenadietelleva Jd Ppt
Efidondenadietelleva Jd PptEfidondenadietelleva Jd Ppt
Efidondenadietelleva Jd Ppt
 
Systems and Chaos Theory
Systems and Chaos TheorySystems and Chaos Theory
Systems and Chaos Theory
 
Steven Covey 7 Habits of Highly Effective People
Steven Covey 7 Habits of Highly Effective PeopleSteven Covey 7 Habits of Highly Effective People
Steven Covey 7 Habits of Highly Effective People
 
Data Structures : AVL Trees
Data Structures : AVL TreesData Structures : AVL Trees
Data Structures : AVL Trees
 
Followership, A Leadership Workshop
Followership, A Leadership WorkshopFollowership, A Leadership Workshop
Followership, A Leadership Workshop
 
BTree, Data Structures
BTree, Data StructuresBTree, Data Structures
BTree, Data Structures
 

Similaire à Data Structures : Sort Explained

Velocity is NOT the Goal - PNSQC
Velocity is NOT the Goal - PNSQCVelocity is NOT the Goal - PNSQC
Velocity is NOT the Goal - PNSQCDoc Norton
 
Triangulation Sample
Triangulation  SampleTriangulation  Sample
Triangulation SampleChattichai
 
Math practice Paper 7 Answer Key
Math practice Paper 7 Answer KeyMath practice Paper 7 Answer Key
Math practice Paper 7 Answer Keylizsaid
 
8 elementary sorts-shell
8 elementary sorts-shell8 elementary sorts-shell
8 elementary sorts-shellirdginfo
 
Nilai Kelas Iii Sd Merak
Nilai Kelas Iii Sd MerakNilai Kelas Iii Sd Merak
Nilai Kelas Iii Sd Merakguestd1077c
 
Nilai Kelas Iii Sd Merak
Nilai Kelas Iii Sd MerakNilai Kelas Iii Sd Merak
Nilai Kelas Iii Sd Merakguestd1077c
 
Analysis of the Drivers of Landcover and Landuse Change in Western Kenya over...
Analysis of the Drivers of Landcover and Landuse Change in Western Kenya over...Analysis of the Drivers of Landcover and Landuse Change in Western Kenya over...
Analysis of the Drivers of Landcover and Landuse Change in Western Kenya over...World Agroforestry (ICRAF)
 

Similaire à Data Structures : Sort Explained (8)

Velocity is NOT the Goal - PNSQC
Velocity is NOT the Goal - PNSQCVelocity is NOT the Goal - PNSQC
Velocity is NOT the Goal - PNSQC
 
Triangulation Sample
Triangulation  SampleTriangulation  Sample
Triangulation Sample
 
Trabajojunta.aux
Trabajojunta.auxTrabajojunta.aux
Trabajojunta.aux
 
Math practice Paper 7 Answer Key
Math practice Paper 7 Answer KeyMath practice Paper 7 Answer Key
Math practice Paper 7 Answer Key
 
8 elementary sorts-shell
8 elementary sorts-shell8 elementary sorts-shell
8 elementary sorts-shell
 
Nilai Kelas Iii Sd Merak
Nilai Kelas Iii Sd MerakNilai Kelas Iii Sd Merak
Nilai Kelas Iii Sd Merak
 
Nilai Kelas Iii Sd Merak
Nilai Kelas Iii Sd MerakNilai Kelas Iii Sd Merak
Nilai Kelas Iii Sd Merak
 
Analysis of the Drivers of Landcover and Landuse Change in Western Kenya over...
Analysis of the Drivers of Landcover and Landuse Change in Western Kenya over...Analysis of the Drivers of Landcover and Landuse Change in Western Kenya over...
Analysis of the Drivers of Landcover and Landuse Change in Western Kenya over...
 

Dernier

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 

Dernier (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

Data Structures : Sort Explained

  • 1. 0 1 2 3 4 5 23 78 45 8 32 56 i=0 min=23 for (i=0;i<n;i++) j=1 min=23 minVal=arr[i] j=2 min=23 for (j=i+1;j<n;j++) j=3 min=8 if minVal >arr[j] j=4 min=8 minVal=arr[j] j=5 min=8 8 78 45 23 32 56 i=1 8 23 45 78 32 56
  • 2. Loop 1 Loop 2 pass 1 23 14 78 45 8 32 56 Original j=1 swap 23 14 14 23 78 45 8 32 56 j=2 no swap 14 23 78 45 8 32 56 j=3 swap 78 45 14 23 45 78 8 32 56 j=4 swap 78 8 14 23 45 8 78 32 56 j=5 swap 78 32 14 23 45 8 32 78 56 j=6 swap 78 56 14 23 45 8 32 56 78 SORTED
  • 3. 23 78 45 8 32 56 Original Insert 78 23 78 45 8 32 56 Pass 1 Insert 45 after 78 before 23 23 45 78 8 32 56 Pass 2 Insert 8 before 23 8 23 45 78 32 56 Pass 3 insert 32 after 45 before 23 8 23 32 45 78 56 Pass 4 Insert 56 after 78 8 23 32 45 56 78 Pass 5 SORTED
  • 4. Wall 1 j k n place k element in appropriate place by walking back from j
  • 5. Array to be Sorted 72 62 14 9 30 21 80 25 70 55 Arr Index 0 1 2 3 4 5 6 7 8 9 1st Incre k=5 Walker Current 72 62 14 9 30 21 80 25 70 55 SWAP 21 62 14 9 30 77 80 25 70 55 21 62 14 9 30 77 80 25 70 55 21 62 14 9 30 77 80 25 70 55 Why Insertion Sort ?? 21 62 14 9 30 77 80 25 70 55 9 Arr Index 0 1 2 3 4 5 6 7 8 9 arr[1] 1st Incre k=3 Walker Current 21 62 14 9 30 77 80 25 70 55 SWAP 9 62 14 21 30 77 80 25 70 55 SWAP 9 30 14 21 62 77 80 25 70 55 9 9 30 14 21 62 77 80 25 70 55 arr[1] 9 30 14 21 62 77 80 25 70 55 SWAP FALLBACK 25 is placed in th 9 25 14 21 30 77 80 62 70 55 SWAP consider only ou 9 25 14 21 30 70 80 62 77 55 SWAP 9 25 14 21 30 70 55 62 77 80 Arr Index 0 1 2 3 4 5 6 7 8 9 1st Incre k=1 Walker Current 9 25 14 21 30 70 55 62 77 80 9 25 14 21 30 70 55 62 77 80 SWAP 9 14 25 21 30 70 55 62 77 80 SWAP 9 14 21 25 30 70 55 62 77 80 9 14 21 25 30 70 55 62 77 80 9 14 21 25 30 70 55 62 77 80 SWAP 9 14 21 25 30 55 70 62 77 80 SWAP 9 14 21 25 30 55 62 70 77 80 9 14 21 25 30 55 62 70 77 80 SORTED
  • 6. Why Insertion Sort ?? Wall 15 22 36 25 6 arr[1+k] arr[1+2k] arr[1+3k] arr[1+4k] arr[1+5k] Wall 15 22 25 36 6 arr[1+k] arr[1+2k] arr[1+3k] arr[1+4k] arr[1+5k] 25 is placed in the inserted when we consider only our segment
  • 7. 78 21 14 97 87 62 74 85 76 45 84 22 62 21 14 97 87 78 74 85 76 45 84 22 22 21 14 97 87 78 74 85 76 45 84 62 Determine Pivot 22 21 14 97 87 62 74 85 76 45 84 78 array Index 0 1 2 3 4 5 6 7 8 9 10 11 62 21 14 97 87 22 74 85 76 45 84 78 Sort Left:1 Looking for number > than 62 (pivot) Sort Left:2 Sort Left:3 Sort Right :11 Looking for number < than 62 (pivot) Sort Right :10 Sort Right :9 62 21 14 45 87 22 74 85 76 97 84 78 SWAP … Sort Left :4 Sort Right moves to 5 62 21 14 45 22 87 74 85 76 97 84 78 SWAP 0,11 62 22 21 14 45 62 87 74 85 76 97 84 78 Move Pivot Back < 62 >=62 0,3 22 5,11 14 21 22 45 62 84 74 85 76 78 87 97 87 0,1 14 3,3 14 21 22 45 62 76 74 78 84 85 45 1,1 21 5,9
  • 8. 84 5,7 76 14 21 22 45 62 74 76 78 84 85 5,5 74 7,7 78 11,11 85
  • 9. or number > than 62 (pivot) or number < than 62 (pivot)
  • 10.
  • 11. Merge i j i 14 20 36 10 12 30 40 44 14 20 aux 10 12 14 Merge Sort 12 30 21 8 6 9 1 7 12 30 21 8 12 30 8 21 6 9 1 7 12 30 21 8 8 12 21 30 1 6 7 9 1 6 7 8 9 12 21 30 Recursive 12 30 21 8 6 9 1 7 Break into 2 array 12 30 21 8 6 9 1 7 Break into 4 arrays 12 30 21 8 6 9 1 7 Break into 8 arrays 12 30 21 8 6 9 1 7 Now each single elemnet array is sorted !!! SO Merge 12 30 8 21 6 9 1 7 Merge Again 8 12 21 30 1 6 7 9 Merge Again 1 6 7 8 9 12 21 30
  • 12. m m+1 j 36 10 12 30 40 44 6 9 1 7 6 9 1 7 MergeSort(arr,start,end) //if only 1 elemnet if (start==end) return; //divide into two equal parts m=(i+j)/2 mergeSort(arr,start,middle) mergeSort(arr,middle+1,end) merge(arr,start,middle,end)
  • 13. MergeSort(arr,start,end) //if only 1 element if (start==end) return; //divide into two equal parts m=(i+j)/2 mergeSort(arr,start,middle) mergeSort(arr,middle+1,end) merge(arr,start,middle,end) Let us analyse Recursion in Merge Sort Let us say an array has 8 elements, how many times will MergeSort get called Start End 0 7 0 3 0 1 0 0 1 1 2 3 2 2 3 3 4 7 4 5 4 4 5 5 6 7 6 6 7 7 If 100 the call would be ? 199
  • 14. Create Heap by using reheapUp Original After Heap 78 32 56 8 23 45 reheapDown 56 32 45 8 23 78 Pass 1 45 32 23 8 56 78 Pass 2 32 8 23 45 56 78 Pass 3 23 8 32 45 56 78 Pass 4 8 23 32 45 56 78 Pass 5 Sorted 8 23 32 45 56 78 Pass 6
  • 15. 111 112 113 114 115 211 311 411 511 611 121 131 141 151 161 Unit Digit 0 1 111 211 311 411 511 611 121 131 141 151 161 2 112 3 113 4 114 5 115 6 …. Ten Digit Ten Digit 0 0 1 111 211 311 411 511 611 112 113 114 115 1 111 112 113 2 121 2 211 3 131 3 311 4 141 4 411 5 151 5 511 6 161 6 611 …. …. SORTED 111 112 113
  • 16. 114 115 121 131 141 151 161 114 115 121 131 141 151 161 211 311 411 511 611
  • 17. Created by Jibrael Jos to explain Sort to M.Sc/MCA Students Jul-09 Christ College