1 / 43

Data Structures 2

Data Structures 2. Stewart Blakeway blakews@hope.ac.uk. 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.

emera
Télécharger la présentation

Data Structures 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures 2 Stewart Blakeway blakews@hope.ac.uk

  2. 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. Quick question – What is Shopping[1] and Shopping[4] ? • Shopping[1] is Crisps • Shopping[4] does not exist Shopping

  4. Another question • int[] cars = new int[7];

  5. Final question int[][] iceCreamSales = new int[7][15];

  6. 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

  7. Operations on arrays: assignment • Scores[4] = 29;

  8. Operations on arrays: retrieval • What would be the output of: System.out.println(Scores[3]+Scores[10]); 62

  9. Example of retrieval • Write a fragment of Java that would display the third name in the array. System.out.println(PlayOffTeams[3]);

  10. Example of retrieval • Write a fragment of Java that would display all the names in the array.

  11. Example of retrieval inti; for (i= 1; i<=4; i++) { System.out.println(PlayOffTeams[i]); }

  12. Initialisation • When arrays are created they should be initialised, otherwise they will possibly contain random numbers or NULL values • The array Scores would want to start out life like this – each element contains 0

  13. Initialisation • We can initialise Scores in Java like this: int j; for (j=1; j<=10; j++) { Scores[j] = 0; }

  14. Searching and sorting arrays

  15. 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

  16. Different kinds of search available • Linear Search • Binary search

  17. 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 for simplicity, we will assume that each search takes 1ms

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

  19. Binary Search • Think of a number between 1 and 1000 Figure 12

  20. Binary Search • 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 Figure 12

  21. Binary Search • Search for Evans in the array of names which have been sorted into alphabetical order Figure 12

  22. 1 Ball • 2 Davies • 3 Evans • 4 Galt • 5 Hurst • 6 Martin • 7 Mason • 8 Moore • 9 Perkins • 10 Stephens See pages 37, 38 in booklet Searching for Evans

  23. 1 Ball • 2 Davies • 3 Evans • 4 Galt • 5 Hurst • 6 Martin • 7 Mason • 8 Moore • 9 Perkins • 10 Stephens See pages 37, 38 in booklet Searching for Evans

  24. 1 Ball • 2 Davies • 3 Evans • 4 Galt • 5 Hurst • 6 Martin • 7 Mason • 8 Moore • 9 Perkins • 10 Stephens See pages 37, 38 in booklet Searching for Evans

  25. 1 Ball • 2 Davies • 3 Evans • 4 Galt • 5 Hurst • 6 Martin • 7 Mason • 8 Moore • 9 Perkins • 10 Stephens See pages 37, 38 in booklet Searching for Evans

  26. 1 Ball • 2 Davies • 3 Evans • 4 Galt • 5 Hurst • 6 Martin • 7 Mason • 8 Moore • 9 Perkins • 10 Stephens See pages 37, 38 in booklet Searching for Evans

  27. 1 Ball • 2 Davies • 3 Evans • 4 Galt • 5 Hurst • 6 Martin • 7 Mason • 8 Moore • 9 Perkins • 10 Stephens See pages 37, 38 in booklet Searching for Evans

  28. Found Evans ! • 1 Ball • 2 Davies • 3 Evans • 4 Galt • 5 Hurst • 6 Martin • 7 Mason • 8 Moore • 9 Perkins • 10 Stephens Searching for Evans

  29. 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

  30. 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

  31. Exchange Sort • Simple • Slow

  32. No swap • 1 Davies • 2 Martin • 3 Perkins • 4 Evans • 5 Mason • 6 Ball • 7 Stephens • 8 Moore • 9 Hurst • 10 Galt See pages 42, 43 in booklet

  33. No swap • 1 Davies • 2 Martin • 3 Perkins • 4 Evans • 5 Mason • 6 Ball • 7 Stephens • 8 Moore • 9 Hurst • 10 Galt See pages 42, 43 in booklet

  34. See pages 42, 43 in booklet • 1 Davies • 2 Martin • 3 Perkins • 4 Evans • 5 Mason • 6 Ball • 7 Stephens • 8 Moore • 9 Hurst • 10 Galt Swap

  35. See pages 42, 43 in booklet • 1 Davies • 2 Martin • 3 Evans • 4 Perkins • 5 Mason • 6 Ball • 7 Stephens • 8 Moore • 9 Hurst • 10 Galt Swap

  36. See pages 42, 43 in booklet • 1 Davies • 2 Martin • 3 Evans • 4 Mason • 5 Perkins • 6 Ball • 7 Stephens • 8 Moore • 9 Hurst • 10 Galt Swap

  37. See pages 42, 43 in booklet • 1 Davies • 2 Martin • 3 Evans • 4 Mason • 5 Ball • 6 Perkins • 7 Stephens • 8 Moore • 9 Hurst • 10 Galt No Swap

  38. See pages 42, 43 in booklet • 1 Davies • 2 Martin • 3 Evans • 4 Mason • 5 Ball • 6 Perkins • 7 Stephens • 8 Moore • 9 Hurst • 10 Galt Swap

  39. 1 Davies • 2 Martin • 3 Evans • 4 Mason • 5 Ball • 6 Perkins • 7 Moore • 8 Stephens • 9 Hurst • 10 Galt See pages 42, 43 in booklet Swap

  40. 1 Davies • 2 Martin • 3 Evans • 4 Mason • 5 Ball • 6 Perkins • 7 Moore • 8 Hurst • 9 Stephens • 10 Galt See pages 42, 43 in booklet Swap

  41. See pages 42, 43 in booklet • 1 Davies • 2 Martin • 3 Evans • 4 Mason • 5 Ball • 6 Perkins • 7 Moore • 8 Hurst • 9 Galt • 10 Stephens 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. Java Exchange Sort 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. Summary • Operations on arrays • Initialisation • Assignment • Retrieval • Searching – linear and binary • Sorting – exchange sort

More Related