Presentation is loading. Please wait.

Presentation is loading. Please wait.

Faculty of Sciences and Social Sciences HOPE Data Structures 2 Stewart Blakeway

Similar presentations


Presentation on theme: "Faculty of Sciences and Social Sciences HOPE Data Structures 2 Stewart Blakeway"— Presentation transcript:

1 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Data Structures 2 Stewart Blakeway blakews@hope.ac.uk

2 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Aims of the Presentation To demonstrate Arrays in Java – Defining the Array object – Determining the Range – Assigning a value to an element – Sorting the Array – Searching the Array

3 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Quick question – What is Shopping[1] and Shopping[4] ? Shopping[1] is Crisps Shopping[4] does not exist 3 0Wine 1Crisps 2Nuts 3Bread Shopping

4 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE int[] cars = new int[7]; 4 Another question

5 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE int[][] iceCreamSales = new int[7][15]; 5 Final question

6 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we have done before in data structures A record is a collection of different data types An array is a fixed size collection of same data types – Single-dimension – Multi-dimension A sequence is a variable size collection of same data types – Stack – Queue 6

7 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Operations on arrays: assignment Scores[4] = 29; 7

8 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Operations on arrays: retrieval What would be the output of: System.out.println(Scores[3]+Scores[10]); 8 62

9 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Example of retrieval Write a fragment of Java that would display the third name in the array. System.out.println(PlayOffTeams[3]); 9

10 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Example of retrieval Write a fragment of Java that would display all the names in the array. 10

11 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Example of retrieval int i; for (i = 1; i<=4; i++) { System.out.println(PlayOffTeams[i]); } 11

12 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Initialisation When arrays are created they should be initialised, otherwise they will possibly contain random numbers or NULL values 12 The array Scores would want to start out life like this – each element contains 0

13 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Initialisation We can initialise Scores in Java like this: int j; for (j=1; j<=10; j++) { Scores[j] = 0; } 13

14 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Searching and sorting arrays 14

15 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Searching an array Example: – How does a “hole in the wall” find your account from your credit card details ? Need to search through the array of bank records 15

16 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Different kinds of search available Linear Search Binary search 16

17 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Linear Search Start at the beginning and examine each in turn until a match found or end of records reached – Fastest find in - 1 comparison – Slowest find in - 1,000 comparisons – Average find in - 500 comparisons 17 for simplicity, we will assume that each search takes 1ms

18 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Search for a national insurance number Is a linear search appropriate ? No ! A better solution in this case is a binary search 18 Linear Search Approximately 59 million people in the UK = Average Search Time of: 48.77 weeks Maximum Search Time of: 1y 43 weeks

19 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Think of a number between 1 and 1000 19 Figure 12 Binary Search

20 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Think of a number between 1 and 1000 I can get that number in 10 yes/no questions – is it less than 500?No – is it less than 750?No – is it less than 825?Yes –... keep going 20 Figure 12 Binary Search

21 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Search for Evans in the array of names which have been sorted into alphabetical order 21 Figure 12 Binary Search

22 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Ball 2 Davies 3 Evans 4 Galt 5 Hurst 6 Martin 7 Mason 8 Moore 9 Perkins 10 Stephens 22 See pages 37, 38 in booklet Searching for Evans

23 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Ball 2 Davies 3 Evans 4 Galt 5 Hurst 6 Martin 7 Mason 8 Moore 9 Perkins 10 Stephens 23 See pages 37, 38 in booklet Searching for Evans

24 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Ball 2 Davies 3 Evans 4 Galt 5 Hurst 6 Martin 7 Mason 8 Moore 9 Perkins 10 Stephens 24 See pages 37, 38 in booklet Searching for Evans

25 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Ball 2 Davies 3 Evans 4 Galt 5 Hurst 6 Martin 7 Mason 8 Moore 9 Perkins 10 Stephens 25 See pages 37, 38 in booklet Searching for Evans

26 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Ball 2 Davies 3 Evans 4 Galt 5 Hurst 6 Martin 7 Mason 8 Moore 9 Perkins 10 Stephens 26 See pages 37, 38 in booklet Searching for Evans

27 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Ball 2 Davies 3 Evans 4 Galt 5 Hurst 6 Martin 7 Mason 8 Moore 9 Perkins 10 Stephens 27 See pages 37, 38 in booklet Searching for Evans

28 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Ball 2 Davies 3 Evans 4 Galt 5 Hurst 6 Martin 7 Mason 8 Moore 9 Perkins 10 Stephens 28 Found Evans ! Searching for Evans

29 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sorting We need to sort to – be able to use the fast binary search which depends on the array being in ascending or descending order – produce reports in required order 29

30 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Sorting Many different ways of carrying out a sort on an array. We shall examine EXCHANGE SORT (Also known as the BUBBLE SORT) We shall look at others 30

31 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Exchange Sort Simple Slow 31

32 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Davies 2 Martin 3 Perkins 4 Evans 5 Mason 6 Ball 7 Stephens 8 Moore 9 Hurst 10 Galt 32 See pages 42, 43 in booklet No swap

33 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Davies 2 Martin 3 Perkins 4 Evans 5 Mason 6 Ball 7 Stephens 8 Moore 9 Hurst 10 Galt 33 See pages 42, 43 in booklet No swap

34 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Davies 2 Martin 3 Perkins 4 Evans 5 Mason 6 Ball 7 Stephens 8 Moore 9 Hurst 10 Galt 34 See pages 42, 43 in booklet Swap

35 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Davies 2 Martin 3 Evans 4 Perkins 5 Mason 6 Ball 7 Stephens 8 Moore 9 Hurst 10 Galt 35 See pages 42, 43 in booklet Swap

36 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Davies 2 Martin 3 Evans 4 Mason 5 Perkins 6 Ball 7 Stephens 8 Moore 9 Hurst 10 Galt 36 See pages 42, 43 in booklet Swap

37 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Davies 2 Martin 3 Evans 4 Mason 5 Ball 6 Perkins 7 Stephens 8 Moore 9 Hurst 10 Galt 37 See pages 42, 43 in booklet No Swap

38 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Davies 2 Martin 3 Evans 4 Mason 5 Ball 6 Perkins 7 Stephens 8 Moore 9 Hurst 10 Galt 38 See pages 42, 43 in booklet Swap

39 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Davies 2 Martin 3 Evans 4 Mason 5 Ball 6 Perkins 7 Moore 8 Stephens 9 Hurst 10 Galt 39 See pages 42, 43 in booklet Swap

40 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Davies 2 Martin 3 Evans 4 Mason 5 Ball 6 Perkins 7 Moore 8 Hurst 9 Stephens 10 Galt 40 See pages 42, 43 in booklet Swap

41 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 1 Davies 2 Martin 3 Evans 4 Mason 5 Ball 6 Perkins 7 Moore 8 Hurst 9 Galt 10 Stephens 41 See pages 42, 43 in booklet At the end of this first pass, Stephens is in the correct position alphabetically. This is repeated for a second pass starting at position 1 and again and again until the array is completely sorted.

42 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java Exchange Sort 42 Consider an array of 10,000 surnames, called names, indexed from 0 to 9,999. for (i=1; i<=9999; i++) { for (j=0; j< 9999; j++) { if (names[j] > names[j+1]) { temp = names[j]; names[j] = names[j+1]; names[j+1] = temp; }

43 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Summary Operations on arrays – Initialisation – Assignment – Retrieval – Searching – linear and binary – Sorting – exchange sort 43


Download ppt "Faculty of Sciences and Social Sciences HOPE Data Structures 2 Stewart Blakeway"

Similar presentations


Ads by Google