# Data Structures & Algorithms Kathleen Christie Teaching Fellow, Department of Computing Science, University of Aberdeen.

## Presentation on theme: "Data Structures & Algorithms Kathleen Christie Teaching Fellow, Department of Computing Science, University of Aberdeen."— Presentation transcript:

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 4 5 or 10 3 ie number power 1set answer to 1 2Loop 1 to power times 2.1answer becomes answer * number 3report answer Notice the layout and numbering

Different Algorithms for Same Problem Devise an algorithm to find the solution to a number raised to a positive power eg 4 5 or 10 3 ie number power 4 5 = 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 3Report answer answernumvalue 125125 242242 2161 320 answernumvalue 134134 192192 1811 810

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 typestypes 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 items place in the array return place; }

Reading Watt and Brown: Chapter 1

Similar presentations