Download presentation

Presentation is loading. Please wait.

Published byJack Wilkinson Modified over 2 years ago

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

2
CS5539 Introduction to Course What is an algorithm What is a data structure Different algorithms Reading Lecture 1 : Introduction

3
Algorithm: Solution to a Problem Recipe Flat pack furniture Music score Knitting pattern What are the common features of these?

4
Algorithm Systematic instructions on a task Step-wise form Makes use of sequence, selection, iteration, recursion Language and details agent specific

5
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

6
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

7
Find the result of number power 1Set answer to 1 2Loop for 1 to 5 times 2.1answer becomes answer * 2 3Report answer AnswerLoop ? Set answer to 1 2Loop for 1 to 4 times 2.1 answer becomes answer * 3 3Report answer AnswerLoop ?

8
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

9
Find the result of number power 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 answernumvalue

10
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

11
Different Representations Data:types typestypes t y p e s

12
Searching algorithms Linear search: unsorted array Linear search: sorted array Binary search: sorted array

13
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)

14
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; }

15
Reading Watt and Brown: Chapter 1

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google