Presentation on theme: "Data Structures Arrays. Array Most commonly used data structure Ordered array, in which the data is stored in ascending (or descending) key order. This."— Presentation transcript:
Data Structures Arrays
Array Most commonly used data structure Ordered array, in which the data is stored in ascending (or descending) key order. This arrangement makes possible a fast way of searching for a data item: the binary search.
Example Attendance monitoring program for cricket team --- using Array. ◦ Insert player at arrival ◦ Search player ◦ Delete player after departure Imagine that each player has been issued a team shirt with the player’s number on the back. To make things visually interesting, the shirts come in a variety of colors --- number and color stored in array Create an applet viewer for it --- insert player and randomly choose color NO DUPLICATION
Insertion / Searching Each button press in a Workshop applet corresponds to a step that an algorithm carries out. The more steps required, the longer the algorithm takes. Insertion taking One step because no empty cell in array ---- sequence (assume not doing dup) Searching will take more steps ◦ Average is half of steps like N/2 ◦ Worst case scenario is N The time an algorithm takes to execute is proportional to the number of steps, so searching takes much longer on the average (N/2 steps) than insertion (one step).
Deletion 2 step procedure ◦ Search required item + Clear cell ◦ Fill holes/empty cell A deletion requires (assuming no duplicates are allowed) searching through an average of N/2 elements and then moving the remaining elements (an average of N/2 moves) to fill up the resulting hole. This is N steps in all.
Duplicate Issues Primary key cannot duplicate Human error chance so check all data items before insertion. Searching with duplicates ◦ N moves required (whole array search) Insertion with duplicates ◦ N moves to search duplicates ◦ 1 step insertion Deletion with duplicates ◦ N moves to search and N/2 moves to shift cells ◦ Average will be based on how many duplicates
Average moves for 3 operations Create program for this example Manage it with classes so that class can be used for some other purpose. E.g separate array insertion separately. Like store serial # of travelers cheque Apply abstraction
Ordered Array Useful when more searching ◦ Linear Search ◦ Binary Search