SlideShare a Scribd company logo
1 of 40
Selection Sort, Insertion Sort,
Bubble, & Shellsort
CPS212, Gordon College
Outline
 Importance of Sorting
 Selection Sort
 Explanation & Runtime
 Walk through example
 Insertion Sort
 Explanation & Runtime
 Advantage and Disadvantage
 Walk through example
 Bubble Sort
 Shell Sort
 History
 Explanation &Runtime
 Advantage and Disadvantage
 Walk through example
Why we do sorting?
 One of the most common programming tasks
in computing.
 Examples of sorting:
 List containing exam scores sorted from Lowest to
Highest or from Highest to Lowest
 List point pairs of a geometric shape.
 List of student records and sorted by student
number or alphabetically by first or last name.
Why we do sorting?
 Searching for an element in an array will be
more efficient. (example: looking for a
particular phone number).
 It’s always nice to see data in a sorted
display. (example: spreadsheet or database
application).
 Computers sort things fast - therefore it takes
the burden off of the user to search a list.
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.
History of Sorting
 It's still important for presentation of
data extracted from databases: most
people prefer to get reports sorted into
some relevant order before flipping
through pages of data!
Selection Sort
 Repeatedly searches for the largest value
in a section of the data
 Moves that value into its correct position in a
sorted section of the list
 Uses the Find Largest algorithm
Selection Sort
1. Get values for n and the n list items
2. Set the marker for the unsorted section at the end of the list
3. While the unsorted section of the list is not empty, do steps 4 through 6
4. Select the largest number in the unsorted section of the list
5. Exchange this number with the last number in the unsorted
section of the list
6. Move the marker for the unsorted section left one position
7. Stop
Selection Sort
 Count comparisons of largest so far against
other values
 Find Largest, given m values, does m-1
comparisons
 Selection sort calls Find Largest n times,
 Each time with a smaller list of values
 Cost = n-1 + (n-2) + … + 2 + 1 = n(n-1)/2
Selection Sort
 Time efficiency
 Comparisons: n(n-1)/2
 Exchanges: n (swapping largest into place)
 Overall: (n2), best and worst cases
 Space efficiency
 Space for the input sequence, plus a constant
number of local variables
Selection Sort
Sort: 34 8 64 51 32 21
 34 8 64 51 32 21
 Set marker at 21 and search for largest
 34 8 21 51 32 64
 34-32 is considered unsorted list
 34 8 21 32 51 64
 Repeat this process until unsorted list is empty
 32 8 21 34 51 64
 21 8 32 34 51 64
 8 21 32 34 51 64
^
^
^
Insertion Sort
 Insertion sort keeps making the left side of the array sorted until
the whole array is sorted. (Sorting cards in your hand)
 Basic Algorithm:
 Repeat the following steps until value in proper position
 Step 1. Pull out current value into a temp variable
 Step 2. Check index’s value - if less than temp then
move it left.
 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.
Insertion Sort runtimes
 Best case: O(n) It occurs when the data is
in sorted order. After making one pass
through the data and making no
insertions, insertion sort exits.
 Average case: (n^2) since there is a
wide variation with the running time.
 Worst case: O(n^2) if the numbers were
sorted in reverse order.
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
 The insertion sort shouldn't be used for
sorting lists larger than a couple
thousand items or repetitive sorting of
lists larger than a couple hundred items.
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.
Insertion Sort Overview
 Advantage of Insertion Sort is that it is
relatively simple and easy to implement.
 Disadvantage of Insertion Sort is that it
is not efficient to operate with a large list
or input size.
Insertion Sort Example
Sort: 34 8 64 51 32 21
 34 8 64 51 32 21
 Pull out 8 into Temp
 34 8 64 51 32 21
 Compare 34 and 8 - move 34 up a spot
 34 34 64 51 32 21
 Spot is found for 8 - place it where it belongs
 8 34 64 51 32 21
Insertion Sort Example
Sort: 34 8 64 51 32 21
 8 34 64 51 32 21
 Pull out 64 into Temp
 8 34 64 51 32 21
 Compare 64 and 34 - place 64 back into slot 2
 8 34 64 51 32 21
Insertion Sort Example
Sort: 34 8 64 51 32 21
 8 34 64 51 32 21
 Pull out 51 into Temp
 8 34 64 51 32 21
 Compare 51 and 64 - move 64 to the right
 8 34 64 64 32 21
 Compare 51 and 34 - place 51 into slot 2
 8 34 51 64 32 21
Insertion Sort Example
Sort: 34 8 64 51 32 21
 8 34 51 64 32 21
 Pull out 32 into Temp
 8 34 51 64 32 21
 Compare 32 and 64 - move 64 to the right
 8 34 51 64 64 21
 Compare 32 and 51 - move 51 to the right
 8 34 51 51 64 21
 Compare 32 and 34 - move 34 to the right
 8 34 34 51 64 21
 Compare 32 and 8 - place 32 in slot 1
 8 32 34 51 64 21
What comes next?
Bubble sort
 Works by repeatedly stepping through the list to be sorted, comparing
two items at a time and swapping them if they are in the wrong order.
The pass through the list is repeated until no swaps are needed, which
indicates that the list is sorted.
Sort: 34 8 64 51 32 21
Pass 1
34 8 64 51 32 21
8 34 64 51 32 21
8 34 51 64 32 21
8 34 51 32 64 21
8 34 51 32 21 64
Pass 2
8 34 51 32 21 64
8 34 51 32 21 64
8 34 51 32 21 64
8 34 32 51 21 64
8 34 32 21 51 64
8 34 32 21 51 64
Worst and average - O(n^2)
Not practical for list with large n -
except when list is very close to
sorted
Repeat until no swaps are made.
Shellsort
 Invented by Donald Shell in 1959.
 1st algorithm to break the quadratic
time barrier but few years later, a sub
quadratic time bound was proven
 Shellsort works by comparing
elements that are distant rather than
adjacent elements in an array.
Shellsort
 Shellsort uses a sequence h1, h2, …, ht
called the increment sequence. Any
increment sequence is fine as long as
h1 = 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.
Shellsort
 Shellsort improves on the efficiency of
insertion sort by quickly shifting values
to their destination.
Shellsort
 Shellsort is also known as diminishing
increment sort.
 The distance between comparisons
decreases as the sorting algorithm runs
until the last phase in which adjacent
elements are compared
Shellsort
 After each phase and some increment
hk, for every i, we have a[ i ] ≤ a [ i + hk ]
all elements spaced hk apart are sorted.
 The file is said to be hk – sorted.
Empirical Analysis of Shellsort
Source: http://linux.wku.edu/~lamonml/algor/sort/shell.html
Empirical Analysis of Shellsort
(Advantage)
 Advantage of Shellsort is that its only
efficient for medium size lists. For
bigger lists, the algorithm is not the best
choice. Fastest of all O(N^2) sorting
algorithms.
 5 times faster than the bubble sort and
a little over twice as fast as the insertion
sort, its closest competitor.
Empirical Analysis of Shellsort
(Disadvantage)
 Disadvantage of Shellsort is that it is a
complex algorithm and its not nearly as
efficient as the merge, heap, and quick sorts.
 The shell sort is still significantly slower than
the merge, heap, and quick sorts, but its
relatively simple algorithm makes it a good
choice for sorting lists of less than 5000 items
unless speed important. It's also an excellent
choice for repetitive sorting of smaller lists.
Shellsort Best Case
 Best Case: The best case in the shell
sort is when the array is already sorted
in the right order. The number of
comparisons is less.
Shellsort Worst Case
 The running time of Shellsort depends
on the choice of increment sequence.
 The problem with Shell’s increments is
that pairs of increments are not
necessarily relatively prime and smaller
increments can have little effect.
Shellsort Examples
Sort: 18 32 12 5 38 33 16 2
8 Numbers to be sorted, Shell’s increment will be floor(n/2)
* floor(8/2)  floor(4) = 4
increment 4: 1 2 3 4
18 32 12 5 38 33 16 2
(visualize underlining)
Step 1) Only look at 18 and 38 and sort in order ;
18 and 38 stays at its current position because they are in order.
Step 2) Only look at 32 and 33 and sort in order ;
32 and 33 stays at its current position because they are in order.
Shellsort Examples
Sort: 18 32 12 5 38 33 16 2
8 Numbers to be sorted, Shell’s increment will be floor(n/2)
* floor(8/2)  floor(4) = 4
increment 4: 1 2 3 4
18 32 12 5 38 33 16 2
(visualize underlining)
Step 3) Only look at 12 and 16 and sort in order ;
12 and 16 stays at its current position because they are in order.
Step 4) Only look at 5 and 2 and sort in order ;
2 and 5 need to be switched to be in order.
Shellsort Examples (con’t)
Sort: 18 32 12 5 38 33 16 2
Resulting numbers after increment 4 pass:
18 32 12 2 38 33 16 5
* floor(4/2)  floor(2) = 2
increment 2: 1 2
18 32 12 2 38 33 16 5
Step 1) Look at 18, 12, 38, 16 and sort them in their appropriate location:
12 38 16 2 18 33 38 5
Step 2) Look at 32, 2, 33, 5 and sort them in their appropriate location:
12 2 16 5 18 32 38 33
Shellsort Examples (con’t)
Sort: 18 32 12 5 38 33 16 2
* floor(2/2)  floor(1) = 1
increment 1: 1
12 2 16 5 18 32 38 33
2 5 12 16 18 32 33 38
The last increment or phase of Shellsort is basically an Insertion
Sort algorithm.
Additional Online References
 Spark Notes (From Barnes & Noble):
 http://www.sparknotes.com/cs/

More Related Content

Similar to sorting_part1.ppt

Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Muhammad Hammad Waseem
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptxssuserd602fd
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applicationsyazad dumasia
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 
Algorithim lec1.pptx
Algorithim lec1.pptxAlgorithim lec1.pptx
Algorithim lec1.pptxrediet43
 
Selection sort
Selection sortSelection sort
Selection sortasra khan
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure Eman magdy
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharpMicheal Ogundero
 
one main advantage of bubble sort as compared to others
one main advantage of bubble sort as compared to othersone main advantage of bubble sort as compared to others
one main advantage of bubble sort as compared to othersAjay Chimmani
 

Similar to sorting_part1.ppt (20)

sorting.pptx
sorting.pptxsorting.pptx
sorting.pptx
 
Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]Data Structures - Lecture 8 [Sorting Algorithms]
Data Structures - Lecture 8 [Sorting Algorithms]
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
Cis435 week06
Cis435 week06Cis435 week06
Cis435 week06
 
Chapter-2.pptx
Chapter-2.pptxChapter-2.pptx
Chapter-2.pptx
 
Sorting
SortingSorting
Sorting
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Algorithim lec1.pptx
Algorithim lec1.pptxAlgorithim lec1.pptx
Algorithim lec1.pptx
 
Selection sort
Selection sortSelection sort
Selection sort
 
Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
 
Algo PPT.pdf
Algo PPT.pdfAlgo PPT.pdf
Algo PPT.pdf
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
SIMPLE SORTING MUKUND
SIMPLE SORTING MUKUNDSIMPLE SORTING MUKUND
SIMPLE SORTING MUKUND
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
one main advantage of bubble sort as compared to others
one main advantage of bubble sort as compared to othersone main advantage of bubble sort as compared to others
one main advantage of bubble sort as compared to others
 

Recently uploaded

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 

Recently uploaded (20)

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 

sorting_part1.ppt

  • 1. Selection Sort, Insertion Sort, Bubble, & Shellsort CPS212, Gordon College
  • 2. Outline  Importance of Sorting  Selection Sort  Explanation & Runtime  Walk through example  Insertion Sort  Explanation & Runtime  Advantage and Disadvantage  Walk through example  Bubble Sort  Shell Sort  History  Explanation &Runtime  Advantage and Disadvantage  Walk through example
  • 3. Why we do sorting?  One of the most common programming tasks in computing.  Examples of sorting:  List containing exam scores sorted from Lowest to Highest or from Highest to Lowest  List point pairs of a geometric shape.  List of student records and sorted by student number or alphabetically by first or last name.
  • 4. Why we do sorting?  Searching for an element in an array will be more efficient. (example: looking for a particular phone number).  It’s always nice to see data in a sorted display. (example: spreadsheet or database application).  Computers sort things fast - therefore it takes the burden off of the user to search a list.
  • 5. 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.
  • 6. History of Sorting  It's still important for presentation of data extracted from databases: most people prefer to get reports sorted into some relevant order before flipping through pages of data!
  • 7. Selection Sort  Repeatedly searches for the largest value in a section of the data  Moves that value into its correct position in a sorted section of the list  Uses the Find Largest algorithm
  • 8. Selection Sort 1. Get values for n and the n list items 2. Set the marker for the unsorted section at the end of the list 3. While the unsorted section of the list is not empty, do steps 4 through 6 4. Select the largest number in the unsorted section of the list 5. Exchange this number with the last number in the unsorted section of the list 6. Move the marker for the unsorted section left one position 7. Stop
  • 9. Selection Sort  Count comparisons of largest so far against other values  Find Largest, given m values, does m-1 comparisons  Selection sort calls Find Largest n times,  Each time with a smaller list of values  Cost = n-1 + (n-2) + … + 2 + 1 = n(n-1)/2
  • 10. Selection Sort  Time efficiency  Comparisons: n(n-1)/2  Exchanges: n (swapping largest into place)  Overall: (n2), best and worst cases  Space efficiency  Space for the input sequence, plus a constant number of local variables
  • 11. Selection Sort Sort: 34 8 64 51 32 21  34 8 64 51 32 21  Set marker at 21 and search for largest  34 8 21 51 32 64  34-32 is considered unsorted list  34 8 21 32 51 64  Repeat this process until unsorted list is empty  32 8 21 34 51 64  21 8 32 34 51 64  8 21 32 34 51 64 ^ ^ ^
  • 12. Insertion Sort  Insertion sort keeps making the left side of the array sorted until the whole array is sorted. (Sorting cards in your hand)  Basic Algorithm:  Repeat the following steps until value in proper position  Step 1. Pull out current value into a temp variable  Step 2. Check index’s value - if less than temp then move it left.  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.
  • 13. 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.
  • 14. Insertion Sort runtimes  Best case: O(n) It occurs when the data is in sorted order. After making one pass through the data and making no insertions, insertion sort exits.  Average case: (n^2) since there is a wide variation with the running time.  Worst case: O(n^2) if the numbers were sorted in reverse order.
  • 15. 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.
  • 16. Insertion Sort  The insertion sort is a good choice for sorting lists of a few thousand items or less.
  • 17. Insertion Sort  The insertion sort shouldn't be used for sorting lists larger than a couple thousand items or repetitive sorting of lists larger than a couple hundred items.
  • 18. 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.
  • 19. Insertion Sort Overview  Advantage of Insertion Sort is that it is relatively simple and easy to implement.  Disadvantage of Insertion Sort is that it is not efficient to operate with a large list or input size.
  • 20. Insertion Sort Example Sort: 34 8 64 51 32 21  34 8 64 51 32 21  Pull out 8 into Temp  34 8 64 51 32 21  Compare 34 and 8 - move 34 up a spot  34 34 64 51 32 21  Spot is found for 8 - place it where it belongs  8 34 64 51 32 21
  • 21. Insertion Sort Example Sort: 34 8 64 51 32 21  8 34 64 51 32 21  Pull out 64 into Temp  8 34 64 51 32 21  Compare 64 and 34 - place 64 back into slot 2  8 34 64 51 32 21
  • 22. Insertion Sort Example Sort: 34 8 64 51 32 21  8 34 64 51 32 21  Pull out 51 into Temp  8 34 64 51 32 21  Compare 51 and 64 - move 64 to the right  8 34 64 64 32 21  Compare 51 and 34 - place 51 into slot 2  8 34 51 64 32 21
  • 23. Insertion Sort Example Sort: 34 8 64 51 32 21  8 34 51 64 32 21  Pull out 32 into Temp  8 34 51 64 32 21  Compare 32 and 64 - move 64 to the right  8 34 51 64 64 21  Compare 32 and 51 - move 51 to the right  8 34 51 51 64 21  Compare 32 and 34 - move 34 to the right  8 34 34 51 64 21  Compare 32 and 8 - place 32 in slot 1  8 32 34 51 64 21 What comes next?
  • 24. Bubble sort  Works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. Sort: 34 8 64 51 32 21 Pass 1 34 8 64 51 32 21 8 34 64 51 32 21 8 34 51 64 32 21 8 34 51 32 64 21 8 34 51 32 21 64 Pass 2 8 34 51 32 21 64 8 34 51 32 21 64 8 34 51 32 21 64 8 34 32 51 21 64 8 34 32 21 51 64 8 34 32 21 51 64 Worst and average - O(n^2) Not practical for list with large n - except when list is very close to sorted Repeat until no swaps are made.
  • 25. Shellsort  Invented by Donald Shell in 1959.  1st algorithm to break the quadratic time barrier but few years later, a sub quadratic time bound was proven  Shellsort works by comparing elements that are distant rather than adjacent elements in an array.
  • 26. Shellsort  Shellsort uses a sequence h1, h2, …, ht called the increment sequence. Any increment sequence is fine as long as h1 = 1 and some other choices are better than others.
  • 27. Shellsort  Shellsort makes multiple passes through a list and sorts a number of equally sized sets using the insertion sort.
  • 28. Shellsort  Shellsort improves on the efficiency of insertion sort by quickly shifting values to their destination.
  • 29. Shellsort  Shellsort is also known as diminishing increment sort.  The distance between comparisons decreases as the sorting algorithm runs until the last phase in which adjacent elements are compared
  • 30. Shellsort  After each phase and some increment hk, for every i, we have a[ i ] ≤ a [ i + hk ] all elements spaced hk apart are sorted.  The file is said to be hk – sorted.
  • 31. Empirical Analysis of Shellsort Source: http://linux.wku.edu/~lamonml/algor/sort/shell.html
  • 32. Empirical Analysis of Shellsort (Advantage)  Advantage of Shellsort is that its only efficient for medium size lists. For bigger lists, the algorithm is not the best choice. Fastest of all O(N^2) sorting algorithms.  5 times faster than the bubble sort and a little over twice as fast as the insertion sort, its closest competitor.
  • 33. Empirical Analysis of Shellsort (Disadvantage)  Disadvantage of Shellsort is that it is a complex algorithm and its not nearly as efficient as the merge, heap, and quick sorts.  The shell sort is still significantly slower than the merge, heap, and quick sorts, but its relatively simple algorithm makes it a good choice for sorting lists of less than 5000 items unless speed important. It's also an excellent choice for repetitive sorting of smaller lists.
  • 34. Shellsort Best Case  Best Case: The best case in the shell sort is when the array is already sorted in the right order. The number of comparisons is less.
  • 35. Shellsort Worst Case  The running time of Shellsort depends on the choice of increment sequence.  The problem with Shell’s increments is that pairs of increments are not necessarily relatively prime and smaller increments can have little effect.
  • 36. Shellsort Examples Sort: 18 32 12 5 38 33 16 2 8 Numbers to be sorted, Shell’s increment will be floor(n/2) * floor(8/2)  floor(4) = 4 increment 4: 1 2 3 4 18 32 12 5 38 33 16 2 (visualize underlining) Step 1) Only look at 18 and 38 and sort in order ; 18 and 38 stays at its current position because they are in order. Step 2) Only look at 32 and 33 and sort in order ; 32 and 33 stays at its current position because they are in order.
  • 37. Shellsort Examples Sort: 18 32 12 5 38 33 16 2 8 Numbers to be sorted, Shell’s increment will be floor(n/2) * floor(8/2)  floor(4) = 4 increment 4: 1 2 3 4 18 32 12 5 38 33 16 2 (visualize underlining) Step 3) Only look at 12 and 16 and sort in order ; 12 and 16 stays at its current position because they are in order. Step 4) Only look at 5 and 2 and sort in order ; 2 and 5 need to be switched to be in order.
  • 38. Shellsort Examples (con’t) Sort: 18 32 12 5 38 33 16 2 Resulting numbers after increment 4 pass: 18 32 12 2 38 33 16 5 * floor(4/2)  floor(2) = 2 increment 2: 1 2 18 32 12 2 38 33 16 5 Step 1) Look at 18, 12, 38, 16 and sort them in their appropriate location: 12 38 16 2 18 33 38 5 Step 2) Look at 32, 2, 33, 5 and sort them in their appropriate location: 12 2 16 5 18 32 38 33
  • 39. Shellsort Examples (con’t) Sort: 18 32 12 5 38 33 16 2 * floor(2/2)  floor(1) = 1 increment 1: 1 12 2 16 5 18 32 38 33 2 5 12 16 18 32 33 38 The last increment or phase of Shellsort is basically an Insertion Sort algorithm.
  • 40. Additional Online References  Spark Notes (From Barnes & Noble):  http://www.sparknotes.com/cs/