SlideShare a Scribd company logo
1 of 21
CMSC 104, Version 8/06 1
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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
L24Searching&Sorting.ppt
Bubble Sort - Let’s Do One!
C
P
G
A
T
O
B
CMSC 104, Version 8/06 13
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 14
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 15
L24Searching&Sorting.ppt
Arranging Your Hand
7
5 7
CMSC 104, Version 8/06 16
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 17
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 18
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 19
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 20
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 21
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 ABCD.ppt

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
 
introduction into IR
introduction into IRintroduction into IR
introduction into IRssusere3b1a2
 
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
 
Building a CRM on top of ElasticSearch
Building a CRM on top of ElasticSearchBuilding a CRM on top of ElasticSearch
Building a CRM on top of ElasticSearchMark Greene
 
The Data Analysis Workflow
The Data Analysis WorkflowThe Data Analysis Workflow
The Data Analysis WorkflowJonathanEarley3
 
Hippo get together presentation solr integration
Hippo get together presentation   solr integrationHippo get together presentation   solr integration
Hippo get together presentation solr integrationHippo
 
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
 
Tutorial(release)
Tutorial(release)Tutorial(release)
Tutorial(release)Oshin Hung
 
Cynthia Lee ITEM 2018
Cynthia Lee ITEM 2018Cynthia Lee ITEM 2018
Cynthia Lee ITEM 2018ITEM
 
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
 
Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...
Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...
Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...Sebastian Wild
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part VHari Christian
 
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
 
An Introduction To Python - Working With Data
An Introduction To Python - Working With DataAn Introduction To Python - Working With Data
An Introduction To Python - Working With DataBlue Elephant Consulting
 
Sean Kandel - Data profiling: Assessing the overall content and quality of a ...
Sean Kandel - Data profiling: Assessing the overall content and quality of a ...Sean Kandel - Data profiling: Assessing the overall content and quality of a ...
Sean Kandel - Data profiling: Assessing the overall content and quality of a ...huguk
 

Similar to ABCD.ppt (20)

Basics in algorithms and data structure
Basics in algorithms and data structure Basics in algorithms and data structure
Basics in algorithms and data structure
 
introduction into IR
introduction into IRintroduction into IR
introduction into IR
 
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...
 
Building a CRM on top of ElasticSearch
Building a CRM on top of ElasticSearchBuilding a CRM on top of ElasticSearch
Building a CRM on top of ElasticSearch
 
The Data Analysis Workflow
The Data Analysis WorkflowThe Data Analysis Workflow
The Data Analysis Workflow
 
day 13.pptx
day 13.pptxday 13.pptx
day 13.pptx
 
Hippo get together presentation solr integration
Hippo get together presentation   solr integrationHippo get together presentation   solr integration
Hippo get together presentation solr integration
 
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
 
Tutorial(release)
Tutorial(release)Tutorial(release)
Tutorial(release)
 
Cynthia Lee ITEM 2018
Cynthia Lee ITEM 2018Cynthia Lee ITEM 2018
Cynthia Lee ITEM 2018
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Data strucer
Data strucerData strucer
Data strucer
 
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
 
Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...
Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...
Dual-Pivot Quicksort and Beyond: Analysis of Multiway Partitioning and Its Pr...
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part V
 
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...
 
An Introduction To Python - Working With Data
An Introduction To Python - Working With DataAn Introduction To Python - Working With Data
An Introduction To Python - Working With Data
 
Algorithms
Algorithms Algorithms
Algorithms
 
Sean Kandel - Data profiling: Assessing the overall content and quality of a ...
Sean Kandel - Data profiling: Assessing the overall content and quality of a ...Sean Kandel - Data profiling: Assessing the overall content and quality of a ...
Sean Kandel - Data profiling: Assessing the overall content and quality of a ...
 

More from WrushabhShirsat3

unit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDppt
unit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDpptunit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDppt
unit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDpptWrushabhShirsat3
 
01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt
01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt
01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFptWrushabhShirsat3
 
chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...
chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...
chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...WrushabhShirsat3
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufWrushabhShirsat3
 
m150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiue
m150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiuem150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiue
m150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiueWrushabhShirsat3
 
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytxjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytWrushabhShirsat3
 
jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb
jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybbjhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb
jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybbWrushabhShirsat3
 
dcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvh
dcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvhdcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvh
dcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvhWrushabhShirsat3
 
asdabuydvduyawdyuadauysdasuydyudayudayudaw
asdabuydvduyawdyuadauysdasuydyudayudayudawasdabuydvduyawdyuadauysdasuydyudayudayudaw
asdabuydvduyawdyuadauysdasuydyudayudayudawWrushabhShirsat3
 
Ch01_Introduction_to_SPM.pdf
Ch01_Introduction_to_SPM.pdfCh01_Introduction_to_SPM.pdf
Ch01_Introduction_to_SPM.pdfWrushabhShirsat3
 
chapter1-conventionalsoftwaremanagement1 (1).ppt
chapter1-conventionalsoftwaremanagement1 (1).pptchapter1-conventionalsoftwaremanagement1 (1).ppt
chapter1-conventionalsoftwaremanagement1 (1).pptWrushabhShirsat3
 

More from WrushabhShirsat3 (19)

unit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDppt
unit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDpptunit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDppt
unit-iipart-1.WDQWDQWDQWDQWDQWDQWDQWDQWDQWDppt
 
01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt
01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt
01_intro.pfwekfmwEKFHswfuhSWFYUGsduyfgSWY6FEWUFpt
 
chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...
chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...
chapter1-convehisudhiusdiudiudsiusdiuddsdshdibsdiubdsjxkjxjntionalsoftwareman...
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
 
m150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiue
m150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiuem150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiue
m150c1.pptfefgefuygfiuffwefnefjufbwefbweiufwiuefiuefhiuefhwiuefhwuiefhuwiefhwiue
 
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfytxjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
 
jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb
jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybbjhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb
jhbuhbhujnhyubhbuybuybuybbuhyybuybuybuybybyubyubybybb
 
dcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvh
dcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvhdcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvh
dcvdhusdbsduvb0sdyvbsdyvbsdvysdvysdbvsydvdbvbyubdvbdvhvhvhvh
 
asdabuydvduyawdyuadauysdasuydyudayudayudaw
asdabuydvduyawdyuadauysdasuydyudayudayudawasdabuydvduyawdyuadauysdasuydyudayudayudaw
asdabuydvduyawdyuadauysdasuydyudayudayudaw
 
IntroT.ppt
IntroT.pptIntroT.ppt
IntroT.ppt
 
scan.pptx
scan.pptxscan.pptx
scan.pptx
 
2.pptx
2.pptx2.pptx
2.pptx
 
papp01.pptx
papp01.pptxpapp01.pptx
papp01.pptx
 
80410172053.pdf
80410172053.pdf80410172053.pdf
80410172053.pdf
 
asabydqwfdytwdv.ppt
asabydqwfdytwdv.pptasabydqwfdytwdv.ppt
asabydqwfdytwdv.ppt
 
Ch01_Introduction_to_SPM.pdf
Ch01_Introduction_to_SPM.pdfCh01_Introduction_to_SPM.pdf
Ch01_Introduction_to_SPM.pdf
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
chapter1-conventionalsoftwaremanagement1 (1).ppt
chapter1-conventionalsoftwaremanagement1 (1).pptchapter1-conventionalsoftwaremanagement1 (1).ppt
chapter1-conventionalsoftwaremanagement1 (1).ppt
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 

Recently uploaded

Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...
Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...
Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...World Wide Tickets And Hospitality
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service 🦺
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service  🦺CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service  🦺
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service 🦺anilsa9823
 
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改atducpo
 
TAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdf
TAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdfTAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdf
TAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdfSocial Samosa
 
08448380779 Call Girls In Lajpat Nagar Women Seeking Men
08448380779 Call Girls In Lajpat Nagar Women Seeking Men08448380779 Call Girls In Lajpat Nagar Women Seeking Men
08448380779 Call Girls In Lajpat Nagar Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Karol Bagh Women Seeking Men
08448380779 Call Girls In Karol Bagh Women Seeking Men08448380779 Call Girls In Karol Bagh Women Seeking Men
08448380779 Call Girls In Karol Bagh Women Seeking MenDelhi Call girls
 
Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...
Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...
Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...gurkirankumar98700
 
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual serviceanilsa9823
 
Top Call Girls In Jankipuram ( Lucknow ) 🔝 8923113531 🔝 Cash Payment
Top Call Girls In Jankipuram ( Lucknow  ) 🔝 8923113531 🔝  Cash PaymentTop Call Girls In Jankipuram ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment
Top Call Girls In Jankipuram ( Lucknow ) 🔝 8923113531 🔝 Cash Paymentanilsa9823
 
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...baharayali
 
08448380779 Call Girls In International Airport Women Seeking Men
08448380779 Call Girls In International Airport Women Seeking Men08448380779 Call Girls In International Airport Women Seeking Men
08448380779 Call Girls In International Airport Women Seeking MenDelhi Call girls
 
ALL NFL NETWORK CONTACTS- April 29, 2024
ALL NFL NETWORK CONTACTS- April 29, 2024ALL NFL NETWORK CONTACTS- April 29, 2024
ALL NFL NETWORK CONTACTS- April 29, 2024Brian Slack
 
Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...
Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...
Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...Eticketing.co
 
JORNADA 5 LIGA MURO 2024INSUGURACION.pdf
JORNADA 5 LIGA MURO 2024INSUGURACION.pdfJORNADA 5 LIGA MURO 2024INSUGURACION.pdf
JORNADA 5 LIGA MURO 2024INSUGURACION.pdfArturo Pacheco Alvarez
 
9990611130 Find & Book Russian Call Girls In Ghazipur
9990611130 Find & Book Russian Call Girls In Ghazipur9990611130 Find & Book Russian Call Girls In Ghazipur
9990611130 Find & Book Russian Call Girls In GhazipurGenuineGirls
 
08448380779 Call Girls In IIT Women Seeking Men
08448380779 Call Girls In IIT Women Seeking Men08448380779 Call Girls In IIT Women Seeking Men
08448380779 Call Girls In IIT Women Seeking MenDelhi Call girls
 
Tableaux 9ème étape circuit fédéral 2024
Tableaux 9ème étape circuit fédéral 2024Tableaux 9ème étape circuit fédéral 2024
Tableaux 9ème étape circuit fédéral 2024HechemLaameri
 
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...Eticketing.co
 

Recently uploaded (20)

Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...
Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...
Spain Vs Italy 20 players confirmed for Spain's Euro 2024 squad, and three po...
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service 🦺
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service  🦺CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service  🦺
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best Female service 🦺
 
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
大学学位办理《原版美国USD学位证书》圣地亚哥大学毕业证制作成绩单修改
 
TAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdf
TAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdfTAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdf
TAM Sports_IPL 17 Till Match 37_Celebrity Endorsement _Report.pdf
 
08448380779 Call Girls In Lajpat Nagar Women Seeking Men
08448380779 Call Girls In Lajpat Nagar Women Seeking Men08448380779 Call Girls In Lajpat Nagar Women Seeking Men
08448380779 Call Girls In Lajpat Nagar Women Seeking Men
 
08448380779 Call Girls In Karol Bagh Women Seeking Men
08448380779 Call Girls In Karol Bagh Women Seeking Men08448380779 Call Girls In Karol Bagh Women Seeking Men
08448380779 Call Girls In Karol Bagh Women Seeking Men
 
Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...
Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...
Jankipuram / Call Girls Lucknow | Whatsapp No 🫗 8923113531 🎳 VIP Escorts Serv...
 
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Chinhat Lucknow best sexual service
 
Top Call Girls In Jankipuram ( Lucknow ) 🔝 8923113531 🔝 Cash Payment
Top Call Girls In Jankipuram ( Lucknow  ) 🔝 8923113531 🔝  Cash PaymentTop Call Girls In Jankipuram ( Lucknow  ) 🔝 8923113531 🔝  Cash Payment
Top Call Girls In Jankipuram ( Lucknow ) 🔝 8923113531 🔝 Cash Payment
 
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
Asli Kala jadu, Black magic specialist in Pakistan Or Kala jadu expert in Egy...
 
08448380779 Call Girls In International Airport Women Seeking Men
08448380779 Call Girls In International Airport Women Seeking Men08448380779 Call Girls In International Airport Women Seeking Men
08448380779 Call Girls In International Airport Women Seeking Men
 
ALL NFL NETWORK CONTACTS- April 29, 2024
ALL NFL NETWORK CONTACTS- April 29, 2024ALL NFL NETWORK CONTACTS- April 29, 2024
ALL NFL NETWORK CONTACTS- April 29, 2024
 
Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...
Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...
Italy vs Albania Tickets: Italy's Quest for Euro Cup Germany History, Defendi...
 
JORNADA 5 LIGA MURO 2024INSUGURACION.pdf
JORNADA 5 LIGA MURO 2024INSUGURACION.pdfJORNADA 5 LIGA MURO 2024INSUGURACION.pdf
JORNADA 5 LIGA MURO 2024INSUGURACION.pdf
 
9990611130 Find & Book Russian Call Girls In Ghazipur
9990611130 Find & Book Russian Call Girls In Ghazipur9990611130 Find & Book Russian Call Girls In Ghazipur
9990611130 Find & Book Russian Call Girls In Ghazipur
 
Call Girls In Vasundhara 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In Vasundhara 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICECall Girls In Vasundhara 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
Call Girls In Vasundhara 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SERVICE
 
08448380779 Call Girls In IIT Women Seeking Men
08448380779 Call Girls In IIT Women Seeking Men08448380779 Call Girls In IIT Women Seeking Men
08448380779 Call Girls In IIT Women Seeking Men
 
Call Girls 🫤 Paharganj ➡️ 9999965857 ➡️ Delhi 🫦 Russian Escorts FULL ENJOY
Call Girls 🫤 Paharganj ➡️ 9999965857  ➡️ Delhi 🫦  Russian Escorts FULL ENJOYCall Girls 🫤 Paharganj ➡️ 9999965857  ➡️ Delhi 🫦  Russian Escorts FULL ENJOY
Call Girls 🫤 Paharganj ➡️ 9999965857 ➡️ Delhi 🫦 Russian Escorts FULL ENJOY
 
Tableaux 9ème étape circuit fédéral 2024
Tableaux 9ème étape circuit fédéral 2024Tableaux 9ème étape circuit fédéral 2024
Tableaux 9ème étape circuit fédéral 2024
 
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
Croatia vs Italy Euro Cup 2024 Three pitfalls for Spalletti’s Italy in Group ...
 

ABCD.ppt

  • 1. CMSC 104, Version 8/06 1 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?
  • 2. CMSC 104, Version 8/06 2 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?
  • 3. CMSC 104, Version 8/06 3 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.
  • 4. CMSC 104, Version 8/06 4 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 ?
  • 5. CMSC 104, Version 8/06 5 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
  • 6. CMSC 104, Version 8/06 6 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
  • 7. CMSC 104, Version 8/06 7 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
  • 8. CMSC 104, Version 8/06 8 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!
  • 9. CMSC 104, Version 8/06 9 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.
  • 10. CMSC 104, Version 8/06 10 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.
  • 11. CMSC 104, Version 8/06 11 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
  • 12. CMSC 104, Version 8/06 12 L24Searching&Sorting.ppt Bubble Sort - Let’s Do One! C P G A T O B
  • 13. CMSC 104, Version 8/06 13 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; } } } }
  • 14. CMSC 104, Version 8/06 14 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.
  • 15. CMSC 104, Version 8/06 15 L24Searching&Sorting.ppt Arranging Your Hand 7 5 7
  • 16. CMSC 104, Version 8/06 16 L24Searching&Sorting.ppt Arranging Your Hand 5 6 7 5 7 5 6 7 K 5 6 7 8 K
  • 17. CMSC 104, Version 8/06 17 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
  • 18. CMSC 104, Version 8/06 18 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
  • 19. CMSC 104, Version 8/06 19 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
  • 20. CMSC 104, Version 8/06 20 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
  • 21. CMSC 104, Version 8/06 21 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