CS0007: Introduction to Computer Programming
Array Algorithms
Review
Arrays are…
complex variables that can hold multiple values of the same data type.
The size declarator indicates…
the number of elements the array can hold.
Individual values inside of an array are called…
elements.
An element’s index (or subscript) is…
a uniquely identifying number that is used to pinpoint its position in the array.
The enhanced for loop is…
a special loop that iterates through the elements in a collection (in this case array) and
allows access to each element.
Comparing Arrays
Sometimes you want to know if two arrays contain all of the same elements.
What would be the output?
int[] firstArray = {1, 2, 3, 4, 5};
int[] secondArray = {1, 2, 3, 4, 5};
if(firstArray == secondArray)
System.out.print("The arrays are the same!");
else
System.out.print("The arrays are NOT the same!");
Answer: “The arrays are NOT the same”
Why?
 Because the == operator compares the references in the array variables and they point to two different objects
How do we fix it?
We iterate through each the arrays and compare them element by element
Example: ArrayComparison.java
Sum, Average, and Minimum and
Maximum
Four common algorithms used with arrays:
Sum of all the elements in the array (ArraySum.java)
Average of all the elements in the array (ArrayAverage.java)
Minimum element in the array (ArrayMin.java)
Max element in the array (ArrayMax.java)
Partially Filled Arrays
Sometimes you need to stores a series of items in an array, but you
don’t know the number of items there are.
Solutions?
One solution is to create and array with the size being equal to the maximum number
of possible items.
Problem?
If you do not fill the entire array, the array will only be partially filled.
If you go to process the items in the array, you need to only process valid items.
o Solution?
• You must keep track of the number of items you have currently in the array, as
well.
Example: PartialArray.java
The Sequential Search Algorithm
Arrays are known as a data structures.
A Data Structure is a particular way of storing and organizing data in a computer so
that it can be used efficiently.
There are many, many data structures and many are provided by the JavaAPI.
The array is one of the more simple ones.
Although arrays are very useful, they may not be the most efficient structure
depending on the situation you want to use them.
For instance, if you need to find an element with a specific value in an array, what can you do?
Simplest answer: look at every element in the array starting from the begining and see if it is the
one we are looking for.
This is called the Sequential SearchAlgorithm (SequentialSearch.java).
The Sequential Search Algorithm
If we are talking about speed in terms of number of comparisons, what is the worst
case scenario for the Sequential SearchAlgorithm?
The element we are looking for is the last element of the array.
If there are n elements, that means we have to check n elements in the worst case.
If there are n elements in the array what is the average case?
n+1/2 comparisons.
There are data structures that can theoretically perform better in the worst case
There are search algorithms that can perform better in the average case
We will revisit this in a bit.
The Selection Sort Algorithm
Another thing we often want do to with data, besides search it, is to sort it.
Names, grades, scores, etc.
Probably the most simple sorting algorithm is the Selection SortAlgorithm.
The Selection SortAlgorithm works as follows:
Search the array for the smallest element
Swap the first element with the smallest element
The first element is holding the correct value, so no longer consider it in the sort
Repeat previous steps for rest of array until the array is completely sorted.
Example: SelectionSort.java
There are many, many more sorting algorithms, most of which are more
efficient than Selection Sort.
Binary Search
Sequential Search does not scale well to arrays with a large number of elements.
As stated before, there are many, many searching algorithms. One of which is the Binary Search Algorithm.
The general idea of Binary Search assumes that the array is sorted in ascending order, and it uses this knowledge to
incrementally eliminate half of the elements from the array.
The algorithm is as follows:
Compare the target value to the element in the middle of the sorted array
1. If they are the same, the search ends
2. If the target value is larger than the element, then you know every element to the left of the middle is also not the element.
Eliminate all values to the left of the middle and repeat step 1 with the elements left in the array.
3. If the target value is smaller that the element, then you know every element to the right of the middle is also not the element.
Eliminate all values to the right of the middle and repeat step 1 with the elements left in the array.
4. If there is no more elements to eliminate in the array, the target value is not in the array.
Binary Search
Sequential Search does not scale well to arrays with a large number of elements.
As stated before, there are many, many searching algorithms. One of which is the Binary Search Algorithm.
The general idea of Binary Search assumes that the array is sorted in ascending order, and it uses this knowledge to
incrementally eliminate half of the elements from the array.
The algorithm is as follows:
Compare the target value to the element in the middle of the sorted array
1. If they are the same, the search ends
2. If the target value is larger than the element, then you know every element to the left of the middle is also not the element.
Eliminate all values to the left of the middle and repeat step 1 with the elements left in the array.
3. If the target value is smaller that the element, then you know every element to the right of the middle is also not the element.
Eliminate all values to the right of the middle and repeat step 1 with the elements left in the array.
4. If there is no more elements to eliminate in the array, the target value is not in the array.

More Related Content

PPTX
Searching,sorting
PPTX
Dsa – data structure and algorithms sorting
DOCX
C# Collection classes
PDF
Searching and Sorting Techniques in Data Structure
PDF
Pattern Discovery Using Apriori and Ch-Search Algorithm
PPT
1 1 5 Clases
 
PDF
Classification and Clustering
PPTX
Knowledge Discovery
Searching,sorting
Dsa – data structure and algorithms sorting
C# Collection classes
Searching and Sorting Techniques in Data Structure
Pattern Discovery Using Apriori and Ch-Search Algorithm
1 1 5 Clases
 
Classification and Clustering
Knowledge Discovery

What's hot (19)

PPTX
Presentation_Malware Analysis.pptx
DOCX
Classification vs clustering
PPTX
Java Comparable and Comparator
PPTX
Machine learning (ML) and natural language processing (NLP)
DOCX
Introduction to Data Structure and Algorithm
PPTX
Database Engine
PPTX
RapidMiner: Data Mining And Rapid Miner
PPTX
Upstate CSCI 200 Java Chapter 8 - Arrays
PPTX
Machine learning module 2
PDF
Associative Classification: Synopsis
PPT
Machine Learning - Supervised learning
PPTX
Meetup sthlm - introduction to Machine Learning with demo cases
PPTX
Machine Learning - Accuracy and Confusion Matrix
PPTX
Machine learning introduction
PDF
Oracle ML Cheat Sheet
PPTX
RapidMiner: Learning Schemes In Rapid Miner
PPTX
Machine learning and types
PDF
Entity Relationship diagrams - ER diagrams
PPT
Supervised and unsupervised learning
Presentation_Malware Analysis.pptx
Classification vs clustering
Java Comparable and Comparator
Machine learning (ML) and natural language processing (NLP)
Introduction to Data Structure and Algorithm
Database Engine
RapidMiner: Data Mining And Rapid Miner
Upstate CSCI 200 Java Chapter 8 - Arrays
Machine learning module 2
Associative Classification: Synopsis
Machine Learning - Supervised learning
Meetup sthlm - introduction to Machine Learning with demo cases
Machine Learning - Accuracy and Confusion Matrix
Machine learning introduction
Oracle ML Cheat Sheet
RapidMiner: Learning Schemes In Rapid Miner
Machine learning and types
Entity Relationship diagrams - ER diagrams
Supervised and unsupervised learning
Ad

Viewers also liked (8)

PPT
Strings Arrays
PPTX
Control flow statements in java
PPT
algorithm
PPTX
Nomenclature of Organic Compounds (IUPAC)
PPTX
Epics and User Stories
PPT
Iupac nomenclature
PPTX
Iupac nomenclature class 11 CBSE-organic chemistry some basic principles and ...
PPTX
ORGANIC CHEMISTRY FOR CLASS XI CBSE
Strings Arrays
Control flow statements in java
algorithm
Nomenclature of Organic Compounds (IUPAC)
Epics and User Stories
Iupac nomenclature
Iupac nomenclature class 11 CBSE-organic chemistry some basic principles and ...
ORGANIC CHEMISTRY FOR CLASS XI CBSE
Ad

Similar to 4.1 sequentioal search (20)

DOCX
MODULE 5-Searching and-sorting
PPTX
Searching, Sorting and Hashing Techniques
PPTX
DS - Unit 2 FINAL (2).pptx
PPTX
Data Structures_ Sorting & Searching
PPT
Searching
PPT
Searching Sorting-SELECTION ,BUBBBLE.ppt
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
ODP
Data operatons & searching and sorting algorithms
PPTX
Algorithm & data structures lec4&5
PPT
ds 2-Arrays and its types and operations
PDF
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
PPT
ds 2Arrays.ppt
PPTX
Data Structures Unit 2 FINAL presentation.pptx
PPT
Data Structure and Algorithms Arrays
PPTX
Searching searching in in arrays arrays.pptx
PPT
Array 2
PPTX
SORT AND SEARCH ARRAY WITH WITH C++.pptx
PPTX
Searching linear & binary search
PPT
(Data Structure) Chapter11 searching & sorting
PPT
Algorithms the fundamentals, For computer Science.ppt
MODULE 5-Searching and-sorting
Searching, Sorting and Hashing Techniques
DS - Unit 2 FINAL (2).pptx
Data Structures_ Sorting & Searching
Searching
Searching Sorting-SELECTION ,BUBBBLE.ppt
Chapter 3 - Data Structure and Algorithms.pptx
Data operatons & searching and sorting algorithms
Algorithm & data structures lec4&5
ds 2-Arrays and its types and operations
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
ds 2Arrays.ppt
Data Structures Unit 2 FINAL presentation.pptx
Data Structure and Algorithms Arrays
Searching searching in in arrays arrays.pptx
Array 2
SORT AND SEARCH ARRAY WITH WITH C++.pptx
Searching linear & binary search
(Data Structure) Chapter11 searching & sorting
Algorithms the fundamentals, For computer Science.ppt

More from Krish_ver2 (20)

PPT
5.5 back tracking
PPT
5.5 back track
PPT
5.5 back tracking 02
PPT
5.4 randomized datastructures
PPT
5.4 randomized datastructures
PPT
5.4 randamized algorithm
PPT
5.3 dynamic programming 03
PPT
5.3 dynamic programming
PPT
5.3 dyn algo-i
PPT
5.2 divede and conquer 03
PPT
5.2 divide and conquer
PPT
5.2 divede and conquer 03
PPT
5.1 greedyyy 02
PPT
5.1 greedy
PPT
5.1 greedy 03
PPT
4.4 hashing02
PPT
4.4 hashing
PPT
4.4 hashing ext
PPT
4.4 external hashing
PPT
4.2 bst
5.5 back tracking
5.5 back track
5.5 back tracking 02
5.4 randomized datastructures
5.4 randomized datastructures
5.4 randamized algorithm
5.3 dynamic programming 03
5.3 dynamic programming
5.3 dyn algo-i
5.2 divede and conquer 03
5.2 divide and conquer
5.2 divede and conquer 03
5.1 greedyyy 02
5.1 greedy
5.1 greedy 03
4.4 hashing02
4.4 hashing
4.4 hashing ext
4.4 external hashing
4.2 bst

Recently uploaded (20)

PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PDF
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
PPTX
PLASMA AND ITS CONSTITUENTS 123.pptx
PDF
Hospital Case Study .architecture design
PDF
Journal of Dental Science - UDMY (2021).pdf
PPTX
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
PDF
anganwadi services for the b.sc nursing and GNM
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PPTX
Diploma pharmaceutics notes..helps diploma students
PPTX
ACFE CERTIFICATION TRAINING ON LAW.pptx
PPTX
Case Study on mbsa education to learn ok
PDF
Everyday Spelling and Grammar by Kathi Wyldeck
PDF
Chevening Scholarship Application and Interview Preparation Guide
PDF
0520_Scheme_of_Work_(for_examination_from_2021).pdf
PDF
Farming Based Livelihood Systems English Notes
PPTX
principlesofmanagementsem1slides-131211060335-phpapp01 (1).ppt
PDF
Compact First Student's Book Cambridge Official
PDF
Journal of Dental Science - UDMY (2020).pdf
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
PLASMA AND ITS CONSTITUENTS 123.pptx
Hospital Case Study .architecture design
Journal of Dental Science - UDMY (2021).pdf
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
anganwadi services for the b.sc nursing and GNM
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
Diploma pharmaceutics notes..helps diploma students
ACFE CERTIFICATION TRAINING ON LAW.pptx
Case Study on mbsa education to learn ok
Everyday Spelling and Grammar by Kathi Wyldeck
Chevening Scholarship Application and Interview Preparation Guide
0520_Scheme_of_Work_(for_examination_from_2021).pdf
Farming Based Livelihood Systems English Notes
principlesofmanagementsem1slides-131211060335-phpapp01 (1).ppt
Compact First Student's Book Cambridge Official
Journal of Dental Science - UDMY (2020).pdf

4.1 sequentioal search

  • 1. CS0007: Introduction to Computer Programming Array Algorithms
  • 2. Review Arrays are… complex variables that can hold multiple values of the same data type. The size declarator indicates… the number of elements the array can hold. Individual values inside of an array are called… elements. An element’s index (or subscript) is… a uniquely identifying number that is used to pinpoint its position in the array. The enhanced for loop is… a special loop that iterates through the elements in a collection (in this case array) and allows access to each element.
  • 3. Comparing Arrays Sometimes you want to know if two arrays contain all of the same elements. What would be the output? int[] firstArray = {1, 2, 3, 4, 5}; int[] secondArray = {1, 2, 3, 4, 5}; if(firstArray == secondArray) System.out.print("The arrays are the same!"); else System.out.print("The arrays are NOT the same!"); Answer: “The arrays are NOT the same” Why?  Because the == operator compares the references in the array variables and they point to two different objects How do we fix it? We iterate through each the arrays and compare them element by element Example: ArrayComparison.java
  • 4. Sum, Average, and Minimum and Maximum Four common algorithms used with arrays: Sum of all the elements in the array (ArraySum.java) Average of all the elements in the array (ArrayAverage.java) Minimum element in the array (ArrayMin.java) Max element in the array (ArrayMax.java)
  • 5. Partially Filled Arrays Sometimes you need to stores a series of items in an array, but you don’t know the number of items there are. Solutions? One solution is to create and array with the size being equal to the maximum number of possible items. Problem? If you do not fill the entire array, the array will only be partially filled. If you go to process the items in the array, you need to only process valid items. o Solution? • You must keep track of the number of items you have currently in the array, as well. Example: PartialArray.java
  • 6. The Sequential Search Algorithm Arrays are known as a data structures. A Data Structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. There are many, many data structures and many are provided by the JavaAPI. The array is one of the more simple ones. Although arrays are very useful, they may not be the most efficient structure depending on the situation you want to use them. For instance, if you need to find an element with a specific value in an array, what can you do? Simplest answer: look at every element in the array starting from the begining and see if it is the one we are looking for. This is called the Sequential SearchAlgorithm (SequentialSearch.java).
  • 7. The Sequential Search Algorithm If we are talking about speed in terms of number of comparisons, what is the worst case scenario for the Sequential SearchAlgorithm? The element we are looking for is the last element of the array. If there are n elements, that means we have to check n elements in the worst case. If there are n elements in the array what is the average case? n+1/2 comparisons. There are data structures that can theoretically perform better in the worst case There are search algorithms that can perform better in the average case We will revisit this in a bit.
  • 8. The Selection Sort Algorithm Another thing we often want do to with data, besides search it, is to sort it. Names, grades, scores, etc. Probably the most simple sorting algorithm is the Selection SortAlgorithm. The Selection SortAlgorithm works as follows: Search the array for the smallest element Swap the first element with the smallest element The first element is holding the correct value, so no longer consider it in the sort Repeat previous steps for rest of array until the array is completely sorted. Example: SelectionSort.java There are many, many more sorting algorithms, most of which are more efficient than Selection Sort.
  • 9. Binary Search Sequential Search does not scale well to arrays with a large number of elements. As stated before, there are many, many searching algorithms. One of which is the Binary Search Algorithm. The general idea of Binary Search assumes that the array is sorted in ascending order, and it uses this knowledge to incrementally eliminate half of the elements from the array. The algorithm is as follows: Compare the target value to the element in the middle of the sorted array 1. If they are the same, the search ends 2. If the target value is larger than the element, then you know every element to the left of the middle is also not the element. Eliminate all values to the left of the middle and repeat step 1 with the elements left in the array. 3. If the target value is smaller that the element, then you know every element to the right of the middle is also not the element. Eliminate all values to the right of the middle and repeat step 1 with the elements left in the array. 4. If there is no more elements to eliminate in the array, the target value is not in the array.
  • 10. Binary Search Sequential Search does not scale well to arrays with a large number of elements. As stated before, there are many, many searching algorithms. One of which is the Binary Search Algorithm. The general idea of Binary Search assumes that the array is sorted in ascending order, and it uses this knowledge to incrementally eliminate half of the elements from the array. The algorithm is as follows: Compare the target value to the element in the middle of the sorted array 1. If they are the same, the search ends 2. If the target value is larger than the element, then you know every element to the left of the middle is also not the element. Eliminate all values to the left of the middle and repeat step 1 with the elements left in the array. 3. If the target value is smaller that the element, then you know every element to the right of the middle is also not the element. Eliminate all values to the right of the middle and repeat step 1 with the elements left in the array. 4. If there is no more elements to eliminate in the array, the target value is not in the array.

Editor's Notes

  • #2: <number>
  • #3: <number>