Upload
jack-wilkinson
View
214
Download
0
Embed Size (px)
Citation preview
Data Structures & Algorithms
Kathleen Christie
Teaching Fellow, Department of Computing Science,
University of Aberdeen
CS5539
• Introduction to Course
• What is an algorithm
• What is a data structure
• Different algorithms
• Reading
Lecture 1 : Introduction
Algorithm: Solution to a Problem
• Recipe
• Flat pack furniture
• Music score
• Knitting pattern
What are the common features of these?
Algorithm
• Systematic instructions on a task
• Step-wise form
• Makes use of sequence, selection, iteration, recursion
• Language and details agent specific
What is an Algorithm?
• Step-by-step procedure used to solve a problem
• These steps should be capable of being performed by a machine
• Must eventually stop and so produce an answer
Different Algorithms for Same Problem
Devise an algorithm to find the solution to a number raised to a positive power
eg 45 or 10 3 ie number power
1 set answer to 1
2 Loop 1 to power times
2.1 answer becomes answer * number
3 report answer
Notice the layout and numbering
Find the result of number power
1 Set answer to 1
2 Loop for 1 to 5 times
2.1 answer becomes answer * 2
3 Report answer
AnswerLoop
1 1
2 2
4 3
8 4
16 5
32 ?
2 5 3 4
1 Set answer to 1
2 Loop for 1 to 4 times
2.1 answer becomes answer * 3
3 Report answer
Answer Loop
1 1
3 2
9 3
27 4
81 ?
Different Algorithms for Same Problem
Devise an algorithm to find the solution to a number raised to a positive power
eg 45 or 10 3 ie number power
45 = 4 2 * 4 2 * 4 1
= 16 * 16 * 4
= 1024
Find the result of number power
2 5 3 4
1 Set answer to 1, num to number, value to power
2 Loop while value is positive
2.1 if value is odd then multiply answer by num
2.2 divide value by 2 ignore remainder, multiply num by itself
3 Report answer
answer num value
1 2 5
2 4 2
2 16 1
32 0
answer num value
1 3 4
1 9 2
1 81 1
81 0
Data Structure
• A way of organizing a collection of Data• (Ref BlueJ Book page 243 exercise 8.14)
– Array
– ArrayList
– HashMap
– HashSet Ch 10 BlueJ
• The same collection of data may be represented by several data structures so there is a choice
Different Representations
• Data:types
t y p e s
t y p e s
t
y p
e s
Searching algorithms
• Linear search: unsorted array
• Linear search: sorted array
• Binary search: sorted array
Algorithm for Linear Search (Unsorted Array)
Go through the array comparing the item required with the item in the array at each position
If item is not found state this
If item is found state position of item
Number of comparisons:
Average for successful search = number of elements/2 (n/2)
Unsuccessful search = number of elements (n)
Code for Linear Search (Unsorted Array)public static int search(int array[], int item) {
//local variable to store position of required item set initially to –1
int place = -1; int index = 0;
//Go through array till whole array searched or item found
while ( index < array.length && place == -1) {
//check if item is found, and if so set place accordingly
if ( array[index] == item) { place = index;
} else {
index++;
}
//Return the item’s place in the array
return place;
}
Reading
• Watt and Brown: Chapter 1