Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Arrays and Lists Part 2 More List Operations.

Similar presentations


Presentation on theme: "Data Structures Arrays and Lists Part 2 More List Operations."— Presentation transcript:

1 Data Structures Arrays and Lists Part 2 More List Operations

2 Last week reviewed using arrays of simple types integers looked at array of objects declare the array create the array create objects and store them in the array introduced list abstract data type modelling a list with an array methods for creating a list adding elements to the end of a list printing all elements in a list example list of integers exercise – list of Student objects

3 This week more list operations adding an element to any position in the list deleting an element returning the element at a given position searching sorting 3631 myArray = 01234567 next = 4

4 Adding an element at position pos want to add a new element (8) at array index pos (1) don't want to lose the element (6) which is already in this position need to shuffle elements in positions 1, 2 and 3 up to make room 3631 myArray = 01234567 next = 4 newData = 8 pos= 1

5 Adding an element at position pos also need to increase next by 1 above diagram shows array after addition should first check whether pos is valid not less than 0 or greater than next 38631 myArray = 01234567 next = 5 newData = 8 pos= 1

6 public boolean add (int pos, int newData) { if (pos next) return false;// position not valid else if (next >= myArray.length) return false;// array is already full else { for (int i = next; i > pos ; i--) myArray[ i ] = myArray[ i - 1]; myArray[pos] = newData; next++; } return true; } Adding an element at position pos

7 want to delete the element (6) at position pos (1) first make sure pos is a valid position greater than or equal to zero and less than next need to shuffle elements in positions 2 and 3 down to fill the ‘hole’ and decrease next by 1 3631 myArray = 01234567 next = 4 pos= 1 Deleting the element at position pos

8 there is no need to actually delete the element at position next it is no longer part of the list won’t be printed out in print algorithm next time a new element is added, it will be overwritten 3311 myArray = 01234567 next = 3 pos= 1 Deleting the element at position pos

9 public boolean remove(int pos) { if (pos >= 0 && pos < next) // position valid? { next--; // decrease number in list by 1 for (int i=pos; i<(next); i++) // fill the gap myArray[i] = myArray[i+1]; return true; } return false; // only executed if pos not valid }

10 Returning the element at a given position return element (6) at array index pos (1) check first to make sure pos is valid public int get(int pos) { if (pos >= 0 && pos < next) // position valid? return myArray[pos]; else return -1;// dummy value - invalid position } 3631 myArray = 01234567 next = 4 pos= 1

11 Searching a list suppose you want to see if a given number (say 6) is in the list could check each array element in turn until you have either found a match or have reached the end of the list return the index of the element if it is found or a dummy value (-1 for example) if not 38631 myArray = 01234567 next = 5 searchValue= 6

12 Searching a list public int find(int searchValue) { for (int i=0; i<next; i++) { if (myArray[i] == searchValue) return i;// return the position } return -1;// this indicates not found }

13 Searching a list try this algorithm with different search values is this the most efficient way to search? would need to do up to 1000 comparisons on a 1000 element list would you search for a name in a phone book this way? 38631 myArray = 01234567 next = 5 searchValue= 6

14 Binary search if the list is sorted, it is more efficient to do a binary search start with the middle element in the list if the search value is smaller look at the element halfway between element 0 and the middle if it is bigger look at the element halfway between the middle and the last element repeat until you have found it, or found it isn’t there a 1000 element list requires only 10 comparisons! 13368 myArray = 01234567 next = 5 searchValue= 6

15 Algorithms and efficiency definition of 'algorithm' a detailed sequence of actions to perform in order to accomplish some task we have been looking at algorithms for list operations we have seen 2 different algorithms for searching one (binary search) is more efficient than the other (linear search) when searching a sorted list but only the linear search algorithm will work on an unsorted list how can we sort a list? there are many different algorithms some are more efficient than others and some are easier to understand than others!

16 Selection sort the selection sort algorithm is easy to understand not the most efficient sort available start by stepping through the entire list and selecting the smallest element put it in position 0 by swapping with the element originally at position 0 don't move any of the other elements 38631 myArray = 01234567 next = 5

17 Selection sort you now have a next-1 element unsorted list where next is the number of elements in the original list now go through the remaining list (ignore position 0) and select the smallest element swap it into position 1 and so on the position we are selecting for goes up by one each time the size of the unsorted list decreases by one each time until we have put the second largest element in the second highest position the largest element will now be in the highest position by default

18 Selection sort public void sortList() { for (int i = 0; i < next - 1; i++) { int pos = i; // remember position of lowest value for (int j = i + 1; j < next; j++) { if (myArray [ j ] < myArray[pos]) pos = j; // update pos } if (pos != i) { // swap if pos has changed int temp = myArray[pos]; myArray[pos] = myArray[i]; myArray[i] = temp; }

19 List algorithms we have covered many list algorithms adding elements to the end of a list printing all elements in a list adding an element to any position in the list deleting an element returning the element in a given position searching sorting need to step through the code to understand it use different example values look at effect of each step

20 Lists of objects can modify all these algorithms to handle lists of objects for example, Student objects search operation needs a search key for example, student name or student number if (studentList[i].getName( ).equals ( searchName) ) ….. when sorting Strings use String compareTo(String str) method operations can return null as a dummy value instead of an object


Download ppt "Data Structures Arrays and Lists Part 2 More List Operations."

Similar presentations


Ads by Google