430 likes | 576 Vues
This presentation provides a comprehensive overview of arrays in Java, covering their definition, initialization, and manipulation. It explores single and multi-dimensional arrays, demonstrating how to assign values, sort, and search through them effectively. Key concepts include linear and binary search methods, along with practical examples of retrieval and sorting techniques like bubble sort. Attendees will gain a solid understanding of how to handle arrays in Java, making their coding more efficient and effective.
E N D
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
Quick question – What is Shopping[1] and Shopping[4] ? • Shopping[1] is Crisps • Shopping[4] does not exist Shopping
Another question • int[] cars = new int[7];
Final question int[][] iceCreamSales = new int[7][15];
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
Operations on arrays: assignment • Scores[4] = 29;
Operations on arrays: retrieval • What would be the output of: System.out.println(Scores[3]+Scores[10]); 62
Example of retrieval • Write a fragment of Java that would display the third name in the array. System.out.println(PlayOffTeams[3]);
Example of retrieval • Write a fragment of Java that would display all the names in the array.
Example of retrieval inti; for (i= 1; i<=4; i++) { System.out.println(PlayOffTeams[i]); }
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
Initialisation • We can initialise Scores in Java like this: int j; for (j=1; j<=10; j++) { Scores[j] = 0; }
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
Different kinds of search available • Linear Search • Binary search
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
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
Binary Search • Think of a number between 1 and 1000 Figure 12
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
Binary Search • Search for Evans in the array of names which have been sorted into alphabetical order Figure 12
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
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
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
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
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
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
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
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
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
Exchange Sort • Simple • Slow
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
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
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
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
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
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
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
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
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
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.
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; } } }
Summary • Operations on arrays • Initialisation • Assignment • Retrieval • Searching – linear and binary • Sorting – exchange sort