int
, long
(Integer numbers)float
, double
(Decimal/Floating point numbers))char
(Single characters) boolean
(Boolean ) String
int
[], String
[] )Scanner
Random
new
keywordThere are numbers behind the doors. You are asked to find if the number 11 is behind any of the doors
There are numbers behind the doors. You are asked to find if the number 11 is behind any of the doors
There are numbers behind the doors. You are asked to find if the number 11 is behind any of the doors
There are numbers behind the doors. You are asked to find if the number 11 is behind any of the doors
There are numbers behind the doors. You are asked to find if the number 11 is behind any of the doors
There are numbers behind the doors.
You are asked to find if the number 11 is behind any of the doors
We needed 5 steps to find the number
There are numbers behind the doors. You are asked to find if the number 25 is behind any of the doors
There are numbers behind the doors. You are asked to find if the number 25 is behind any of the doors
There are numbers behind the doors. You are asked to find if the number 25 is behind any of the doors
There are numbers behind the doors. You are asked to find if the number 25 is behind any of the doors
We had to go through ALL the elments of the array
Given an array ofN
items, and atarget
to search For every positioni
in the array { if the item as positioni
is our target{ Finish! We found our target! } }
There are numbers behind the doors. You are asked to find if the number 11 is behind any of the doors
Let's start by checking the middle of the array
There are numbers behind the doors. You are asked to find if the number 25 is behind any of the doors
11 is greater than 7. If it is in the array, it must be to the right of 7.
There are numbers behind the doors. You are asked to find if the number 25 is behind any of the doors
11 is smaller than 13. If it is in the array, it must be to the left of 13.
There are numbers behind the doors. You are asked to find if the number 25 is behind any of the doors
We found 11!.
There are numbers behind the doors. You are asked to find if the number 25 is behind any of the doors
Let's start by checking the middle of the array
There are numbers behind the doors. You are asked to find if the number 25 is behind any of the doors
25 is greater than 7. If it is in the array, it must be to the right of 7.
There are numbers behind the doors. You are asked to find if the number 25 is behind any of the doors
25 is greater than 13. If it is in the array, it must be to the right of 13.
There are numbers behind the doors. You are asked to find if the number 25 is behind any of the doors
25 is greater than 17. If it is in the array, it must be to the right of 17.
There are numbers behind the doors. You are asked to find if the number 25 is behind any of the doors
25 is greater than 19. We cannot continue subdividing the array, so 25 was not found.
For 8 doors, we had to d 4 comparisons in the worst case
For 8 doors, we had to d 4 comparisons in the worst case
This procedure is know as binary search
Given an arrayA
ofN
items, and atarget
to search Letstart
= 0,end
=N
-1 whiletarget
not found{ Setmid
= (start
+end
)/2 ifA[mid]
is our target{ Finish! We found our target! } else{ ifA[mid]
< target{ setstart
=mid
+ 1 } else ifA[mid]
> target{ setend
=mid
- 1 } } }
Given an array of N
items, and a target
that is not in the array
For Linear Search:
Given an array of N
items, and a target
that is not in the array
For Binary Search:
In this case, the operations we measure are the number of comparisons
The items need to be comparable
Start with an unsorted arrayA
withN
items while the array is unsorted{ For everyposition
between 1 andN
{ ifA[i]
andA[i-1]
are not in the correct order{ SwapA[i]
andA[i-1]
} } }
Start with an unsorted arrayA
withN
items For everypos
between 1 andN
{ Letj
=pos_a
LetITEM
=A[pos]
Whilej
> 0 andITEM
is greater thanA[j -1]
{ SetA[j]
=A[j-1]
Decreasej
by 1 } SetA[j]
=ITEM
}
Why is insertion sort faster?
/