SlideShare a Scribd company logo
1 of 27
CMSC 104, Version 8/06 1
L24Searching&Sorting.ppt
Searching and Sorting
Topics
• Sequential Search on an Unordered File
• Sequential Search on an Ordered File
• Binary Search
• Bubble Sort
• Insertion Sort
Reading
• Sections 6.6 - 6.8
CMSC 104, Version 8/06 2
L24Searching&Sorting.ppt
Common Problems
• There are some very common problems that
we use computers to solve:
o Searching through a lot of records for a specific
record or set of records
o Placing records in order, which we call sorting
• There are numerous algorithms to perform
searches and sorts. We will briefly explore a
few common ones.
CMSC 104, Version 8/06 3
L24Searching&Sorting.ppt
Searching
• A question you should always ask when selecting a
search algorithm is “How fast does the search have
to be?” The reason is that, in general, the faster
the algorithm is, the more complex it is.
• Bottom line: you don’t always need to use or
should use the fastest algorithm.
• Let’s explore the following search algorithms,
keeping speed in mind.
o Sequential (linear) search
o Binary search
CMSC 104, Version 8/06 4
L24Searching&Sorting.ppt
Sequential Search on an Unordered File
• Basic algorithm:
Get the search criterion (key)
Get the first record from the file
While ( (record != key) and (still more records) )
Get the next record
End_while
• When do we know that there wasn’t a
record in the file that matched the key?
CMSC 104, Version 8/06 5
L24Searching&Sorting.ppt
Sequential Search on an Ordered File
• Basic algorithm:
Get the search criterion (key)
Get the first record from the file
While ( (record < key) and (still more records) )
Get the next record
End_while
If ( record = key )
Then success
Else there is no match in the file
End_else
• When do we know that there wasn’t a record in
the file that matched the key?
CMSC 104, Version 8/06 6
L24Searching&Sorting.ppt
Sequential Search of
Ordered vs.. Unordered List
• Let’s do a comparison.
• If the order was ascending alphabetical on
customer’s last names, how would the search for
John Adams on the ordered list compare with the
search on the unordered list?
o Unordered list
– if John Adams was in the list?
– if John Adams was not in the list?
o Ordered list
– if John Adams was in the list?
– if John Adams was not in the list?
CMSC 104, Version 8/06 7
L24Searching&Sorting.ppt
Ordered vs. Unordered (con’t)
• How about George Washington?
o Unordered
– if George Washington was in the list?
– If George Washington was not in the list?
o Ordered
– if George Washington was in the list?
– If George Washington was not in the list?
• How about James Madison?
CMSC 104, Version 8/06 8
L24Searching&Sorting.ppt
Ordered vs.. Unordered (con’t)
• Observation: the search is faster on an
ordered list only when the item being
searched for is not in the list.
• Also, keep in mind that the list has to first be
placed in order for the ordered search.
• Conclusion: the efficiency of these
algorithms is roughly the same.
• So, if we need a faster search, we need a
completely different algorithm.
• How else could we search an ordered file?
CMSC 104, Version 8/06 9
L24Searching&Sorting.ppt
Binary Search
• If we have an ordered list and we know
how many things are in the list (i.e., number
of records in a file), we can use a different
strategy.
• The binary search gets its name because
the algorithm continually divides the list into
two parts.
CMSC 104, Version 8/06 10
L24Searching&Sorting.ppt
How a Binary Search Works
Always look at the
center value. Each
time you get to discard
half of the remaining
list.
Is this fast ?
CMSC 104, Version 8/06 11
L24Searching&Sorting.ppt
How Fast is a Binary Search?
• Worst case: 11 items in the list took 4 tries
• How about the worst case for a list with 32
items ?
o 1st try - list has 16 items
o 2nd try - list has 8 items
o 3rd try - list has 4 items
o 4th try - list has 2 items
o 5th try - list has 1 item
CMSC 104, Version 8/06 12
L24Searching&Sorting.ppt
How Fast is a Binary Search? (con’t)
List has 250 items
1st try - 125 items
2nd try - 63 items
3rd try - 32 items
4th try - 16 items
5th try - 8 items
6th try - 4 items
7th try - 2 items
8th try - 1 item
List has 512 items
1st try - 256 items
2nd try - 128 items
3rd try - 64 items
4th try - 32 items
5th try - 16 items
6th try - 8 items
7th try - 4 items
8th try - 2 items
9th try - 1 item
CMSC 104, Version 8/06 13
L24Searching&Sorting.ppt
What’s the Pattern?
• List of 11 took 4 tries
• List of 32 took 5 tries
• List of 250 took 8 tries
• List of 512 took 9 tries
• 32 = 25 and 512 = 29
• 8 < 11 < 16 23 < 11 < 24
• 128 < 250 < 256 27 < 250 < 28
CMSC 104, Version 8/06 14
L24Searching&Sorting.ppt
A Very Fast Algorithm!
• How long (worst case) will it take to find an
item in a list 30,000 items long?
210 = 1024 213 = 8192
211 = 2048 214 = 16384
212 = 4096 215 = 32768
• So, it will take only 15 tries!
CMSC 104, Version 8/06 15
L24Searching&Sorting.ppt
Lg n Efficiency
• We say that the binary search algorithm
runs in log2 n time. (Also written as lg n)
• Lg n means the log to the base 2 of some
value of n.
• 8 = 23 lg 8 = 3 16 = 24 lg 16 = 4
• There are no algorithms that run faster than
lg n time.
CMSC 104, Version 8/06 16
L24Searching&Sorting.ppt
Sorting
• So, the binary search is a very fast
search algorithm.
• But, the list has to be sorted before we
can search it with binary search.
• To be really efficient, we also need a
fast sort algorithm.
CMSC 104, Version 8/06 17
L24Searching&Sorting.ppt
Common Sort Algorithms
Bubble Sort Heap Sort
Selection Sort Merge Sort
Insertion Sort Quick Sort
• There are many known sorting algorithms. Bubble
sort is the slowest, running in n2 time. Quick sort
is the fastest, running in n lg n time.
• As with searching, the faster the sorting algorithm,
the more complex it tends to be.
• We will examine two sorting algorithms:
o Bubble sort
o Insertion sort
CMSC 104, Version 8/06 18
L24Searching&Sorting.ppt
Bubble Sort - Let’s Do One!
C
P
G
A
T
O
B
CMSC 104, Version 8/06 19
L24Searching&Sorting.ppt
Bubble Sort Code
void bubbleSort (int a[ ] , int size)
{
int i, j, temp;
for ( i = 0; i < size; i++ ) /* controls passes through the list */
{
for ( j = 0; j < size - 1; j++ ) /* performs adjacent comparisons */
{
if ( a[ j ] > a[ j+1 ] ) /* determines if a swap should occur */
{
temp = a[ j ]; /* swap is performed */
a[ j ] = a[ j + 1 ];
a[ j+1 ] = temp;
}
}
}
}
CMSC 104, Version 8/06 20
L24Searching&Sorting.ppt
Insertion Sort
• Insertion sort is slower than quick sort, but
not as slow as bubble sort, and it is easy to
understand.
• Insertion sort works the same way as
arranging your hand when playing cards.
o Out of the pile of unsorted cards that were
dealt to you, you pick up a card and place it in
your hand in the correct position relative to the
cards you’re already holding.
CMSC 104, Version 8/06 21
L24Searching&Sorting.ppt
Arranging Your Hand
7
5 7
CMSC 104, Version 8/06 22
L24Searching&Sorting.ppt
Arranging Your Hand
5 6
7
5
7
5 6 7
K
5 6 7 8 K
CMSC 104, Version 8/06 23
L24Searching&Sorting.ppt
Insertion Sort
Unsorted - shaded
Look at 2nd item - 5.
Compare 5 to 7.
5 is smaller, so move 5
to temp, leaving
an empty slot in
position 2.
Move 7 into the empty
slot, leaving position 1
open.
Move 5 into the open
position.
7
7
5
7
5
7
K
5
7
v
>
<
1
2
3
CMSC 104, Version 8/06 24
L24Searching&Sorting.ppt
Insertion Sort (con’t)
Look at next item - 6.
Compare to 1st - 5.
6 is larger, so leave 5.
Compare to next - 7.
6 is smaller, so move
6 to temp, leaving an
empty slot.
Move 7 into the empty
slot, leaving position 2
open.
Move 6 to the open
2nd position.
7
7
5
7
5
K
5
7
v
>
<
1
2
3
6
7
6
5
6
5
CMSC 104, Version 8/06 25
L24Searching&Sorting.ppt
Insertion Sort (con’t)
Look at next item - King.
Compare to 1st - 5.
King is larger, so
leave 5 where it is.
Compare to next - 6.
King is larger, so
leave 6 where it is.
Compare to next - 7.
King is larger, so
leave 7 where it is.
7 K
5 6
CMSC 104, Version 8/06 26
L24Searching&Sorting.ppt
Insertion Sort (con’t)
7
7
5
7
5 K
5
7
v
>
<
1
2
3
6 7
8
5
6
5
6
6
6
8
K 8
K
K 8
K
CMSC 104, Version 8/06 27
L24Searching&Sorting.ppt
Courses at UMBC
• Data Structures - CMSC 341
o Some mathematical analysis of various
algorithms, including sorting and searching
• Design and Analysis of Algorithms - CMSC 441
o Detailed mathematical analysis of various
algorithms
• Cryptology - CMSC 443
o The study of making and breaking codes

More Related Content

Similar to L24Searching&Sorting.ppt

Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...
Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...
Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...Lucidworks
 
The Data Analysis Workflow
The Data Analysis WorkflowThe Data Analysis Workflow
The Data Analysis WorkflowJonathanEarley3
 
introduction into IR
introduction into IRintroduction into IR
introduction into IRssusere3b1a2
 
Is sort andy-le
Is sort andy-leIs sort andy-le
Is sort andy-leSumedha
 
Hippo get together presentation solr integration
Hippo get together presentation   solr integrationHippo get together presentation   solr integration
Hippo get together presentation solr integrationHippo
 
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...ORAU
 
LecccccccccccccProgrammingLecture-09.pdf
LecccccccccccccProgrammingLecture-09.pdfLecccccccccccccProgrammingLecture-09.pdf
LecccccccccccccProgrammingLecture-09.pdfAmirMohamedNabilSale
 
Approximate "Now" is Better Than Accurate "Later"
Approximate "Now" is Better Than Accurate "Later"Approximate "Now" is Better Than Accurate "Later"
Approximate "Now" is Better Than Accurate "Later"NUS-ISS
 
Handling and Analyzing Large Telemetry Data Sets
Handling and Analyzing Large Telemetry Data SetsHandling and Analyzing Large Telemetry Data Sets
Handling and Analyzing Large Telemetry Data SetsScintica Instrumentation
 
CS2: A Searchable Cryptographic Cloud Storage System
CS2: A Searchable Cryptographic Cloud Storage SystemCS2: A Searchable Cryptographic Cloud Storage System
CS2: A Searchable Cryptographic Cloud Storage SystemShurenBi1
 
MongoDB World 2019: Finding the Right MongoDB Atlas Cluster Size: Does This I...
MongoDB World 2019: Finding the Right MongoDB Atlas Cluster Size: Does This I...MongoDB World 2019: Finding the Right MongoDB Atlas Cluster Size: Does This I...
MongoDB World 2019: Finding the Right MongoDB Atlas Cluster Size: Does This I...MongoDB
 
Apache Solr - An Experience Report
Apache Solr - An Experience ReportApache Solr - An Experience Report
Apache Solr - An Experience ReportNetcetera
 
FIle Organization.pptx
FIle Organization.pptxFIle Organization.pptx
FIle Organization.pptxSreenivas R
 
Full Table Scan: friend or foe
Full Table Scan: friend or foeFull Table Scan: friend or foe
Full Table Scan: friend or foeMauro Pagano
 
How to interactively visualise and explore a billion objects (wit vaex)
How to interactively visualise and explore a billion objects (wit vaex)How to interactively visualise and explore a billion objects (wit vaex)
How to interactively visualise and explore a billion objects (wit vaex)Ali-ziane Myriam
 

Similar to L24Searching&Sorting.ppt (20)

Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...
Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...
Building a Large Scale SEO/SEM Application with Apache Solr: Presented by Rah...
 
The Data Analysis Workflow
The Data Analysis WorkflowThe Data Analysis Workflow
The Data Analysis Workflow
 
introduction into IR
introduction into IRintroduction into IR
introduction into IR
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Is sort andy-le
Is sort andy-leIs sort andy-le
Is sort andy-le
 
Hippo get together presentation solr integration
Hippo get together presentation   solr integrationHippo get together presentation   solr integration
Hippo get together presentation solr integration
 
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
Non equilibrium Molecular Simulations of Polymers under Flow Saving Energy th...
 
LecccccccccccccProgrammingLecture-09.pdf
LecccccccccccccProgrammingLecture-09.pdfLecccccccccccccProgrammingLecture-09.pdf
LecccccccccccccProgrammingLecture-09.pdf
 
Approximate "Now" is Better Than Accurate "Later"
Approximate "Now" is Better Than Accurate "Later"Approximate "Now" is Better Than Accurate "Later"
Approximate "Now" is Better Than Accurate "Later"
 
SearchAlgorithm.pdf
SearchAlgorithm.pdfSearchAlgorithm.pdf
SearchAlgorithm.pdf
 
Natural Language Processing using Java
Natural Language Processing using JavaNatural Language Processing using Java
Natural Language Processing using Java
 
Handling and Analyzing Large Telemetry Data Sets
Handling and Analyzing Large Telemetry Data SetsHandling and Analyzing Large Telemetry Data Sets
Handling and Analyzing Large Telemetry Data Sets
 
Shell sort
Shell sortShell sort
Shell sort
 
CS2: A Searchable Cryptographic Cloud Storage System
CS2: A Searchable Cryptographic Cloud Storage SystemCS2: A Searchable Cryptographic Cloud Storage System
CS2: A Searchable Cryptographic Cloud Storage System
 
MongoDB World 2019: Finding the Right MongoDB Atlas Cluster Size: Does This I...
MongoDB World 2019: Finding the Right MongoDB Atlas Cluster Size: Does This I...MongoDB World 2019: Finding the Right MongoDB Atlas Cluster Size: Does This I...
MongoDB World 2019: Finding the Right MongoDB Atlas Cluster Size: Does This I...
 
Apache Solr - An Experience Report
Apache Solr - An Experience ReportApache Solr - An Experience Report
Apache Solr - An Experience Report
 
FIle Organization.pptx
FIle Organization.pptxFIle Organization.pptx
FIle Organization.pptx
 
Full Table Scan: friend or foe
Full Table Scan: friend or foeFull Table Scan: friend or foe
Full Table Scan: friend or foe
 
How to interactively visualise and explore a billion objects (wit vaex)
How to interactively visualise and explore a billion objects (wit vaex)How to interactively visualise and explore a billion objects (wit vaex)
How to interactively visualise and explore a billion objects (wit vaex)
 
Vaex talk-pydata-paris
Vaex talk-pydata-parisVaex talk-pydata-paris
Vaex talk-pydata-paris
 

Recently uploaded

Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Delhi Call girls
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceanilsa9823
 
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130Suhani Kapoor
 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...SUHANI PANDEY
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Call Girls in Nagpur High Profile
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentationamedia6
 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...Call Girls in Nagpur High Profile
 
Stark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxStark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxjeswinjees
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)amitlee9823
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation decktbatkhuu1
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...home
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Call Girls in Nagpur High Profile
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...kumaririma588
 
Call Girls in Kalkaji Delhi 8264348440 call girls ❤️
Call Girls in Kalkaji Delhi 8264348440 call girls ❤️Call Girls in Kalkaji Delhi 8264348440 call girls ❤️
Call Girls in Kalkaji Delhi 8264348440 call girls ❤️soniya singh
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptxVanshNarang19
 
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai DouxDubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Douxkojalkojal131
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 

Recently uploaded (20)

Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
Best VIP Call Girls Noida Sector 44 Call Me: 8448380779
 
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
 
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
VIP Call Girls Service Bhagyanagar Hyderabad Call +91-8250192130
 
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
VIP Model Call Girls Kalyani Nagar ( Pune ) Call ON 8005736733 Starting From ...
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance  VVIP 🍎 SER...
Call Girls Service Mukherjee Nagar @9999965857 Delhi 🫦 No Advance VVIP 🍎 SER...
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentation
 
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...Booking open Available Pune Call Girls Kirkatwadi  6297143586 Call Hot Indian...
Booking open Available Pune Call Girls Kirkatwadi 6297143586 Call Hot Indian...
 
Stark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptxStark Industries Marketing Plan (1).pptx
Stark Industries Marketing Plan (1).pptx
 
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Basapura ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
Peaches App development presentation deck
Peaches App development presentation deckPeaches App development presentation deck
Peaches App development presentation deck
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
 
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...Verified Trusted Call Girls Adugodi💘 9352852248  Good Looking standard Profil...
Verified Trusted Call Girls Adugodi💘 9352852248 Good Looking standard Profil...
 
Call Girls in Kalkaji Delhi 8264348440 call girls ❤️
Call Girls in Kalkaji Delhi 8264348440 call girls ❤️Call Girls in Kalkaji Delhi 8264348440 call girls ❤️
Call Girls in Kalkaji Delhi 8264348440 call girls ❤️
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptx
 
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai DouxDubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
 
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 

L24Searching&Sorting.ppt

  • 1. CMSC 104, Version 8/06 1 L24Searching&Sorting.ppt Searching and Sorting Topics • Sequential Search on an Unordered File • Sequential Search on an Ordered File • Binary Search • Bubble Sort • Insertion Sort Reading • Sections 6.6 - 6.8
  • 2. CMSC 104, Version 8/06 2 L24Searching&Sorting.ppt Common Problems • There are some very common problems that we use computers to solve: o Searching through a lot of records for a specific record or set of records o Placing records in order, which we call sorting • There are numerous algorithms to perform searches and sorts. We will briefly explore a few common ones.
  • 3. CMSC 104, Version 8/06 3 L24Searching&Sorting.ppt Searching • A question you should always ask when selecting a search algorithm is “How fast does the search have to be?” The reason is that, in general, the faster the algorithm is, the more complex it is. • Bottom line: you don’t always need to use or should use the fastest algorithm. • Let’s explore the following search algorithms, keeping speed in mind. o Sequential (linear) search o Binary search
  • 4. CMSC 104, Version 8/06 4 L24Searching&Sorting.ppt Sequential Search on an Unordered File • Basic algorithm: Get the search criterion (key) Get the first record from the file While ( (record != key) and (still more records) ) Get the next record End_while • When do we know that there wasn’t a record in the file that matched the key?
  • 5. CMSC 104, Version 8/06 5 L24Searching&Sorting.ppt Sequential Search on an Ordered File • Basic algorithm: Get the search criterion (key) Get the first record from the file While ( (record < key) and (still more records) ) Get the next record End_while If ( record = key ) Then success Else there is no match in the file End_else • When do we know that there wasn’t a record in the file that matched the key?
  • 6. CMSC 104, Version 8/06 6 L24Searching&Sorting.ppt Sequential Search of Ordered vs.. Unordered List • Let’s do a comparison. • If the order was ascending alphabetical on customer’s last names, how would the search for John Adams on the ordered list compare with the search on the unordered list? o Unordered list – if John Adams was in the list? – if John Adams was not in the list? o Ordered list – if John Adams was in the list? – if John Adams was not in the list?
  • 7. CMSC 104, Version 8/06 7 L24Searching&Sorting.ppt Ordered vs. Unordered (con’t) • How about George Washington? o Unordered – if George Washington was in the list? – If George Washington was not in the list? o Ordered – if George Washington was in the list? – If George Washington was not in the list? • How about James Madison?
  • 8. CMSC 104, Version 8/06 8 L24Searching&Sorting.ppt Ordered vs.. Unordered (con’t) • Observation: the search is faster on an ordered list only when the item being searched for is not in the list. • Also, keep in mind that the list has to first be placed in order for the ordered search. • Conclusion: the efficiency of these algorithms is roughly the same. • So, if we need a faster search, we need a completely different algorithm. • How else could we search an ordered file?
  • 9. CMSC 104, Version 8/06 9 L24Searching&Sorting.ppt Binary Search • If we have an ordered list and we know how many things are in the list (i.e., number of records in a file), we can use a different strategy. • The binary search gets its name because the algorithm continually divides the list into two parts.
  • 10. CMSC 104, Version 8/06 10 L24Searching&Sorting.ppt How a Binary Search Works Always look at the center value. Each time you get to discard half of the remaining list. Is this fast ?
  • 11. CMSC 104, Version 8/06 11 L24Searching&Sorting.ppt How Fast is a Binary Search? • Worst case: 11 items in the list took 4 tries • How about the worst case for a list with 32 items ? o 1st try - list has 16 items o 2nd try - list has 8 items o 3rd try - list has 4 items o 4th try - list has 2 items o 5th try - list has 1 item
  • 12. CMSC 104, Version 8/06 12 L24Searching&Sorting.ppt How Fast is a Binary Search? (con’t) List has 250 items 1st try - 125 items 2nd try - 63 items 3rd try - 32 items 4th try - 16 items 5th try - 8 items 6th try - 4 items 7th try - 2 items 8th try - 1 item List has 512 items 1st try - 256 items 2nd try - 128 items 3rd try - 64 items 4th try - 32 items 5th try - 16 items 6th try - 8 items 7th try - 4 items 8th try - 2 items 9th try - 1 item
  • 13. CMSC 104, Version 8/06 13 L24Searching&Sorting.ppt What’s the Pattern? • List of 11 took 4 tries • List of 32 took 5 tries • List of 250 took 8 tries • List of 512 took 9 tries • 32 = 25 and 512 = 29 • 8 < 11 < 16 23 < 11 < 24 • 128 < 250 < 256 27 < 250 < 28
  • 14. CMSC 104, Version 8/06 14 L24Searching&Sorting.ppt A Very Fast Algorithm! • How long (worst case) will it take to find an item in a list 30,000 items long? 210 = 1024 213 = 8192 211 = 2048 214 = 16384 212 = 4096 215 = 32768 • So, it will take only 15 tries!
  • 15. CMSC 104, Version 8/06 15 L24Searching&Sorting.ppt Lg n Efficiency • We say that the binary search algorithm runs in log2 n time. (Also written as lg n) • Lg n means the log to the base 2 of some value of n. • 8 = 23 lg 8 = 3 16 = 24 lg 16 = 4 • There are no algorithms that run faster than lg n time.
  • 16. CMSC 104, Version 8/06 16 L24Searching&Sorting.ppt Sorting • So, the binary search is a very fast search algorithm. • But, the list has to be sorted before we can search it with binary search. • To be really efficient, we also need a fast sort algorithm.
  • 17. CMSC 104, Version 8/06 17 L24Searching&Sorting.ppt Common Sort Algorithms Bubble Sort Heap Sort Selection Sort Merge Sort Insertion Sort Quick Sort • There are many known sorting algorithms. Bubble sort is the slowest, running in n2 time. Quick sort is the fastest, running in n lg n time. • As with searching, the faster the sorting algorithm, the more complex it tends to be. • We will examine two sorting algorithms: o Bubble sort o Insertion sort
  • 18. CMSC 104, Version 8/06 18 L24Searching&Sorting.ppt Bubble Sort - Let’s Do One! C P G A T O B
  • 19. CMSC 104, Version 8/06 19 L24Searching&Sorting.ppt Bubble Sort Code void bubbleSort (int a[ ] , int size) { int i, j, temp; for ( i = 0; i < size; i++ ) /* controls passes through the list */ { for ( j = 0; j < size - 1; j++ ) /* performs adjacent comparisons */ { if ( a[ j ] > a[ j+1 ] ) /* determines if a swap should occur */ { temp = a[ j ]; /* swap is performed */ a[ j ] = a[ j + 1 ]; a[ j+1 ] = temp; } } } }
  • 20. CMSC 104, Version 8/06 20 L24Searching&Sorting.ppt Insertion Sort • Insertion sort is slower than quick sort, but not as slow as bubble sort, and it is easy to understand. • Insertion sort works the same way as arranging your hand when playing cards. o Out of the pile of unsorted cards that were dealt to you, you pick up a card and place it in your hand in the correct position relative to the cards you’re already holding.
  • 21. CMSC 104, Version 8/06 21 L24Searching&Sorting.ppt Arranging Your Hand 7 5 7
  • 22. CMSC 104, Version 8/06 22 L24Searching&Sorting.ppt Arranging Your Hand 5 6 7 5 7 5 6 7 K 5 6 7 8 K
  • 23. CMSC 104, Version 8/06 23 L24Searching&Sorting.ppt Insertion Sort Unsorted - shaded Look at 2nd item - 5. Compare 5 to 7. 5 is smaller, so move 5 to temp, leaving an empty slot in position 2. Move 7 into the empty slot, leaving position 1 open. Move 5 into the open position. 7 7 5 7 5 7 K 5 7 v > < 1 2 3
  • 24. CMSC 104, Version 8/06 24 L24Searching&Sorting.ppt Insertion Sort (con’t) Look at next item - 6. Compare to 1st - 5. 6 is larger, so leave 5. Compare to next - 7. 6 is smaller, so move 6 to temp, leaving an empty slot. Move 7 into the empty slot, leaving position 2 open. Move 6 to the open 2nd position. 7 7 5 7 5 K 5 7 v > < 1 2 3 6 7 6 5 6 5
  • 25. CMSC 104, Version 8/06 25 L24Searching&Sorting.ppt Insertion Sort (con’t) Look at next item - King. Compare to 1st - 5. King is larger, so leave 5 where it is. Compare to next - 6. King is larger, so leave 6 where it is. Compare to next - 7. King is larger, so leave 7 where it is. 7 K 5 6
  • 26. CMSC 104, Version 8/06 26 L24Searching&Sorting.ppt Insertion Sort (con’t) 7 7 5 7 5 K 5 7 v > < 1 2 3 6 7 8 5 6 5 6 6 6 8 K 8 K K 8 K
  • 27. CMSC 104, Version 8/06 27 L24Searching&Sorting.ppt Courses at UMBC • Data Structures - CMSC 341 o Some mathematical analysis of various algorithms, including sorting and searching • Design and Analysis of Algorithms - CMSC 441 o Detailed mathematical analysis of various algorithms • Cryptology - CMSC 443 o The study of making and breaking codes