You are on page 1of 36

IMAM ABDULRAHMAN BIN FAISAL

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)

An Introduction to Programming with C++, 8th Edition 3


Sorting Arrays (Bubble Sort)
• Common problem: sort a list of values, starting from lowest to
highest.
– List of exam scores
– Words of dictionary in alphabetical order
– Students names listed alphabetically
– Student records sorted by ID#
• Generally, we are given a list of records that have keys. These
keys are used to define an ordering of the items in the list.

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

n “Bubble” the largest value to the end using pair-wise


comparisons and swapping
An Introduction to Programming with C++, 8th Edition 6
Bubble Sort Code
int size=10; // You can change array size for any number
int i, j, data[size], temp; // temp variable should be same type as the array type
if (size < 2)
cout<<“ nothing to sort!!”;

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

• We are given a list of records.

• Each record has an associated key.

• Give efficient algorithm for searching for a record containing


a particular key.
Search

[0] [1] [2] [3] [4] [ 700 ]

Number 701466868
Number 281942902 Number 233667136 Number 580625685 Number 506643548

… Number 155778322

Each record in list has an associated key.


In this example, the keys for example: ID numbers.
Number 580625685
Given a particular key, how can we efficiently
retrieve the record from the list?
Searching Arrays
• Types of Search:
– Sequential Search: also called linear/serial search is a method
for finding an element within a list.
• It sequentially checks each element of the list until a match is
found or the whole list has been searched.
• Doesn’t require that all items are ordered.
– Binary Search: an enhanced (faster) form of search.
•Requires that all values are ordered.
•Works by locating the middle value in an array then looking
where your key(item) searching for is located: in the middle,
before or after. Then repeating until key is found or the
whole set has been searched
11
Searching Arrays
• Types of Search:
– Both types works on searching the array until:
• Item is found and location (index) is returned
• If the whole array has been searched and the key was not found
you shall return a value that indicates that value was not found.
– Linear search can be applied to both sorted and unsorted
arrays.

12
Sequential Search vs Binary Search

Sequential Search Binary Search


• Array items can be in any • Array items should be
arbitrary order. ordered (ascending or
• Worst in performance descending).
(longer time) • Optimized in performance

What are the questions we need to ask before we search?


Are data/items ordered or not?
Ordered èsequential or binary search
Unordered è sequential

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)

// Search for a desired item in the n array elements


// starting at a[first].
// Returns index to desired record if found.
// Otherwise, return (negative value) We add break
int first=0; /*assuming we are starting from the first element. to optimize
You can start from any index*/ the code
cin>>key; assuming that
for(i = first; i < n; ++i ) the value we
if(a[i]==key){ are looking
cout<<“Item found in “<<i+1; for is a key.
break; If there are
} duplicate
values we can
remove break
Pseudocode Searching Arrays
(Sequential Search)

// Search for a desired item in the n array elements


// starting at a[first].
// Returns index to desired record if found.
// Otherwise, return (negative value) What if the
int first=0; /*assuming we are starting from the first element.
item
You can start from any index*/
cin>>key; wasn’t
for(i = first; i < n; ++i ) found how
if(a[i]==key){ can we
cout<<“Item found in “<<i+1; determine
break;
}
that?
Pseudocode Searching Arrays
(Sequential Search)
// Search for a desired item in the n array elements
// starting at a[first].
// Returns index to desired record if found.
// Otherwise, return (negative value)
bool found=false;
int first=0; /*assuming we are starting from the first element. What if the
You can start from any index*/ item
cin>>key;
for(i = first; i < n; ++i )
wasn’t
if(a[i]==key){ found how
cout<<“Item found in “<<i+1; can we
found=true; determine
break; that?
}
Using Flag
if (!found)
cout<<“Item was not found!”;
Pseudocode Searching Arrays
(Sequential Search)
// Search for a desired item in the n array elements
// starting at a[first].
// Returns index to desired record if found.
// Otherwise, return (negative value)
int index= -1;
int first=0; /*assuming we are starting from the first element.
You can start from any index*/
What if the
cin>>key; item
for(i = first; i < n; ++i ) wasn’t
if(a[i]==key){ found how
cout<<“Item found in “<<i+1; can we
index=i;
break;
determine
} that?
if (index<0)
cout<<“Item was not found!”;
Searching Arrays
(Sequential Search)
• What are the worst and average case running times
for serial search?
– We must determine the O-notation for the number of
operations required in search.
– Number of operations depends on n, the number of
entries in the list.
– The whole array has been searched èworst case
– Found from first item è best case

Big O notation is used in Computer Science to describe the performance


or complexity of an algorithm. Big O specifically describes the worst-case
scenario, and can be used to describe the execution time required or the
space used (e.g. in memory or on disk) by an algorithm.
Searching Arrays
(Sequential Search) – Worst Case

• For an array of n elements, the worst case time


for serial search requires n array accesses: O(n).
• Consider cases where we must loop over all n
records:
– desired record appears in the last position of the
array
– desired record does not appear in the array at all
Searching Arrays
(Sequential Search) – Average Case
Assumptions:
1. All keys are equally likely in a search
2. We always search for a key that is in the array
Example:
• We have an array of 10 records.
• If search for the first record, then it requires 1
array access; if the second, then 2 array accesses.
etc.
The average of all these searches is:
(1+2+3+4+5+6+7+8+9+10)/10 = 5.5
Searching Arrays
(Sequential Search) – Average Case
Generalize for array size n.

Expression for average-case running time:

(1+2+…+n)/n = n(n+1)/2n = (n+1)/2

Therefore, average case time complexity for serial


search is O(n).
Searching Arrays
(Sequential Search)
Quiz:
Suppose you are doing a sequential search of the list [15,
18, 2, 19, 18, 0, 8, 14, 19, 14]. How many comparisons
would you need to do in order to find the key 18? Why?
– 5
– 10 ‫ نوقف عند اول مره نالقي‬sequential ‫في ال‬
‫فيها العدد ما يحتاج نكمل‬
– 4
– 2

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

• Binary search uses a recursive method to search an


array to find a specified value
• The array must be a sorted array:
a[0]≤a[1]≤a[2]≤. . . ≤ a[finalIndex]
• If the value is found, its index is returned
• If the value is not found, -1 is returned
• Note: Each execution of the recursive method
reduces the search space by about a half

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]

Locate the middle


3 6 7 11 32 33 53 number

Target > num Goto Right


3 6 7 11 32 33 53 Target < num Goto Left
Target = num cout Target

3 6 7 11 32 33 53

Locate the middle


number
Searching Arrays
(Binary Search)
Target > num Goto Right
3 6 7 11 32 33 53 Target < num Goto Left
Target = num cout Target

3 6] 7] [ 11
2] [ 32
3] [ 33
4] [ 53
5] [6]
[0 [1

Locate the middle


number
Target > num Goto Right
3 6 7 11 32 33 53 Target < num Goto Left
Target = num cout Target

Find approximate midpoint


Searching Arrays
(Binary Search) Implementation
int first=0, middle, size=10, target, location;
int a[size ];
bool found;
if(size == 0)
found = false;
else {
middle = (first + end)/2;
if(target == a[middle]){
We will
location = middle;
found = true; implement
} this using
else if (target < a[middle]) functions
// target is less than middle, so search subarray before middle next week
search(a, first, size/2, target, found, location);
else
// target is greater than middle, so search subarray after middle
search(a, middle+1, (size-1)/2, target, found, location);
Efficiency of Binary Search

• The binary search algorithm is extremely fast compared


to an algorithm that tries all array elements in order
– About half the array is eliminated from consideration
right at the start
– Then a quarter of the array, then an eighth of the array,
and so forth

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?

Sort data using sorting algorithms then apply binary search

36

You might also like