You are on page 1of 26

ddobach

Array Search
o Know the array element we are seeking
(search target).
Linear Search
o Perform search by examining each array
element using a loop and by testing whether
the element matches the target
o Exit search loop when target is found

ddobach

Problem:
Find the index of a number in an
unsorted array of integers
linear_search.c

ddobach

1. Assume the target is not yet found


2. Start with initial array element
3. Repeat while the target is not found and there are more
array elements
4. if the current element matches the target
5. Set a flag to indicate that the target has been
found
else
6. Advance to the next array element
7. If the target was found
8. return the target index as search result
else
9. return -1 as search result
ddobach

function returns the index of target if present


in the array, -1 otherwise
local variable i: selects the array element that
is compared to the target value
variable found: used to represent the concept
of whether the target has been found yet and
is tested in the loop repetition condition;
initially set to 0 for false (target is initially not
found before we begin searching)

ddobach

int search_array(const int arr[], int target, int n)


{
int i = 0, found = 0, where;
while (!found && i < n)
{
if (arr[i] == target)
found = 1;
else
i++;
}
if (found)
where = i;
else
where = -1;
}
ddobach

ddobach

ddobach

Questions
1. For the search function given earlier:
a) what happens if the last ID stored matches the
target?
b) what happens if several IDs stored match the
target?

ddobach

Problem:
Find the minimum number in an array
of unsorted integers.
find_minimum.c

ddobach

1.
2.

3.

Get list of numbers (array elements)


Set first element as the smallest
Compare each remaining list element to the
current smallest. Save the smaller.

ddobach

int find_min(const int list[], int n)


{
int i, cur_smallest;
cur_smallest = list[0];
for(i = 1; i < n; i++)
{
if(list[i] < cur_smallest)
cur_smallest = list[i];
}
}

ddobach

Important note: the word const

In the formal parameter list, the reserved word


const indicates that the array variable declared
is strictly an input parameter and will not be
modified by the function.

ddobach

Write a main function for the function


Test your program

ddobach

Challenge Problem:
Find all occurrences of a number in an
array and replace it with a new value.
search_and_replace.c

ddobach

ddobach

In some programming instances, it is better


to work with data that is already sorted. This
is particularly true when you are dealing with
large amounts of data like in databases.
Sorting is the act of arranging the elements,
most commonly in arrays, in a specific order
as determined by the programmer.
Examples: Quick sort, Bubble Sort, Selection
Sort, Merge Sort, Insertion Sort, Bucket Sort,
etc.
ddobach

Bubble Sort Algorithm


for all elements of the array
compare adjacent elements
if element(i) > element(i+1)
exchange element(i) & element(i+1)
repeat the above until no more exchanges are made.

ddobach

In all sorting algorithms, the most common


or basic concept being applied is the
exchanging or swapping of two objects (most
commonly numbers).

Question:
How do you swap 2 numbers?
How many variables do you need?

ddobach

Problem:
Implement the Bubble Sort Algorithm in
an unsorted array of integers.
bubble_sort.c

ddobach

ddobach

A pointer is a variable that holds a memory


address.
Because it holds a memory address it is said
to be pointing to the value at that memory
location.
You can use a pointer in a certain way to get
the value at the address to which it points.

ddobach

To declare a pointer: put a * in front of its


name.

Syntax:
<datatype> *<name_of_pointer>;

Example:
int *x;

ddobach

You can put the address of an integer into a


pointer to an integer by using the & operator to
get the integers address.

int main()
{
int i;
int *p;
i = 5;
p = &i;
return (0);
}
ddobach

& - address-of operator


* - value-at operator; also called the
dereference operator.

Whats the difference?


&x - gets the address where the value of x is
stored.
*x - points to the actual value of x.

ddobach

ddobach

You might also like