2 - Search SortArrays
2 - Search SortArrays
UNIVERSITY
College of Computer Science & IT
Department of CS
Welcome to
CS 221:Fundamentals of Programming
Weeks (3): Searching and Sorting Arrays
Objectives
• Arrays :
– Sorting Arrays (Bubble Sort)
– Sequential Search
– Binary Search
2
Click to edit Master title style
SORTING ARRAYS
(BUBBLE SORT)
4
The Bubble Sort Algorithm
• The Bubble Sort algorithm looks at pairs of entries in the
array and swaps their order if needed.
Traverse a collection of elements
n Move from the front to the end
for(i = 0; i < size -1; ++i) //nested loop .. Why do we need to reach size-1?
{
for(j = 0; j < size -1;++j)
if(data[j] > data[j+1]) // if out of order, swap!
{
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
}
Click to edit Master title style
SEARCHING ARRAYS
8
Problem: Search
Number 701466868
Number 281942902 Number 233667136 Number 580625685 Number 506643548
… Number 155778322
12
Sequential Search vs Binary Search
13
What are the questions we need to
ask before we search?
• Are data values are unique values (keys) or not?
– unique values are called keys because you can use them
to reach a certain item where no other item has same
value for sure. In such case when you reach this item you
can return its location and exit the search
– If data included replicas/duplicate values then you need
to set when you will exit the search?
• Search all items up to the end?
• Search from the beginning or end ? Return first occurrence
or last occurrence ?
• All occurrences?
14
Click to edit Master title style
SEARCHING ARRAYS
(SEQUENTIAL SEARCH)
An Introduction to Programming with C++, 8th Edition 15
Searching Arrays
(Sequential Search)
• Step through array of records, one at a time.
• Look for record with matching key.
• Search stops when
– record with matching key is found
– or when search has examined all records without success.
Searching Arrays
(Sequential Search)
• Given a list L of n elements with values L0 .... Ln−1, and
target value T, the following algorithm uses linear search
to find the index of the target T in L.
1. Set i to 0.
2. If Li = T, the search terminates successfully; return i.
3. Increase i by 1.
4. If i < n, go to step 2. Otherwise, the search terminates
unsuccessfully.
17
Pseudocode Searching Arrays
(Sequential Search)
As it will the return the index of the item in its first occurrence
26
Click to edit Master title style
SEARCHING ARRAYS
(BINARY SEARCH)
An Introduction to Programming with C++, 8th Edition 27
Searching Arrays
(Binary Search)
• Perhaps we can do better than O(n) in the average
case?
• Assume that we are give an array of records that is
sorted. For instance:
– an array of records with integer keys sorted from
smallest to largest (e.g., ID numbers), or
– an array of records with string keys sorted in alphabetical
order (e.g., names).
Binary Search
29
Binary Search
• If the value looked for is larger than the value in the middle
of the array or array segment
– Then the first half of the array or array segment can be ignored
– This strategy is then applied to the second half of the array or array
segment
• If the value looked for is at the middle of the array or array
segment, then it has been found
• If the entire array (or array segment) has been searched in
this way without finding the value, then it is not in the array
30
Searching Arrays
(Binary Search)Pseudocode
…
if(size == 0)
found = false;
else {
middle = index of approximate midpoint of array segment;
if(target == a[middle])
target has been found!
else if(target < a[middle])
search for target in area before midpoint;
else
search for target in area after midpoint;
}
…
Searching Arrays
(Binary Search)
Example: Searching for Target=7.
[0] [1] [2] [3] [4] [5] [6]
3 6 7 11 32 33 53
3 6] 7] [ 11
2] [ 32
3] [ 33
4] [ 53
5] [6]
[0 [1
35
Searching Arrays
(Sequential Search)
Quiz:
If you had a set of data that is not ordered and you were
asked to apply a binary search, what shall you do?
36