Download presentation
Presentation is loading. Please wait.
Published byMatilda Hill Modified over 9 years ago
1
Week # 2: Arrays
2
Data structure A particular way of storing and organising data in a computer so that it can be used efficiently Types of data structures Based on memory allocation o Static (or fixed sized) data structures (Arrays) o Dynamic data structures (Linked lists) Based on representation o Linear(Arrays/linked lists) o Non-linear(Trees/graphs)
3
You want to store 5 numbers in a computer Define 5 variables, e.g. num1, num2,..., num5 What, if you want to store 1000 numbers? Defining 1000 variables is a pity! Requires much programming effort Any better solution? Yes, some structured data type o Array is one of the most common structured data types o Saves a lot of programming effort (cf. 1000 variable names)
4
A collection of data elements in which all elements are of the same data type, hence homogeneous data o An array of students’ marks o An array of students’ names o An array of objects (OOP perspective!) elements (or their references) are stored at contiguous/ consecutive memory locations Array is a static data structure An array cannot grow or shrink during program execution – its size is fixed
5
Array name (data) Index/subscript (0...9) The slots are numbered sequentially starting at zero (Java, C++) If there are N slots in an array, the index will be 0 through N-1 Array length = N = 10 Array size = N x Size of an element = 40 Direct access to an element
6
All elements in the array must have the same data type Index: Value: 5101830455060657080 01234 5678 9 Index: Value: 5.510.218.545.660.5 0124 3 Index: Value: ‘A’10.255‘X’ 60.5 0124 3 Not an array
7
Array elements are stored at contiguous memory locations No empty segment in between values (3 & 5 are empty – not allowed) Index: Value: 5101830455060657080 01234 5678 9 Index: Value: 510184560657080 01234 5678 9
8
Declaring and Creating arrays ◦ Arrays are objects that occupy memory ◦ Created dynamically with keyword new int c[] = new int[ 12 ]; Equivalent to int c[]; // declare array variable c = new int[ 12 ]; // create array We can create arrays of objects too String b[] = new String[ 100 ];
9
Array_name[index] For example, in Java System.out.println(data[4]) will display 0 data[3] = 99 will replace -3 with 99
10
Array_name[index] For example, in Java System.out.println(data[4]) will display 0 data[3] = 99 will replace -3 with 99
11
Using an array initializer ◦ Use initializer list Items enclosed in braces ( {} ) Items in list separated by commas int n[] = { 10, 20, 30, 40, 50 }; Creates a five-element array Index values of 0, 1, 2, 3, 4 ◦ Do not need keyword new
12
To declare an array follow the type with (empty) []s int[] grade; //or int grade[]; //both declare an int array In Java arrays are objects so must be created with the new keyword To create an array of ten integers: int[] grade = new int[10]; Note that the array size has to be specified, although it can be specified with a variable at run-time
13
Calculating the value to store in each array element –Initialize elements of 10-element array to even integers
14
InitArray.java Line 10 Declare array as an array of int s Line 12 Create 10 int s for array Line 16 Use array index to assign array value
16
Summing the elements of an array ◦ Array elements can represent a series of values We can sum these values
17
Histogram.java Line 9 Declare array with initializer list Line 19 For each array element, print associated number of asterisks
19
data[ -1 ] always illegal data[ 10 ] illegal (10 > upper bound) data[ 1.5 ] always illegal data[ 0 ] always OK data[ 9 ] OK Q. What will be the output of? 1. data[5] + 10 2. data[3] = data[3] + 10
20
One dimensional (just a linear list) e.g., Only one subscript is required to access an individual element Two dimensional (matrix/table) e.g., 2 x 4 matrix (2 rows, 4 columns) 5101830455060657080 Col 0Col 1Col 2Col 3 Row 020256040 Row 130157090
21
Multidimensional arrays ◦ Tables with rows and columns Two-dimensional array Declaring two-dimensional array b[2][2] int b[][] = { { 1, 2 }, { 3, 4 } }; 1 and 2 initialize b[0][0] and b[0][1] 3 and 4 initialize b[1][0] and b[1][1] int b[][] = { { 1, 2 }, { 3, 4, 5 } }; row 0 contains elements 1 and 2 row 1 contains elements 3, 4 and 5
22
Creating multidimensional arrays ◦ Can be allocated dynamically 3 -by- 4 array int b[][]; b = new int[ 3 ][ 4 ]; Rows can have different number of columns int b[][]; b = new int[ 2 ][ ]; // allocate rows b[ 0 ] = new int[ 5 ]; // allocate row 0 b[ 1 ] = new int[ 3 ]; // allocate row 1
23
a[ 1 ][ 0 ]a[ 1 ][ 1 ]a[ 1 ][ 2 ]a[ 1 ][ 3 ] Row 0 Row 1 Row 2 Column 0Column 1Column 2Column 3 Row index Array name Column index a[ 0 ][ 0 ]a[ 0 ][ 1 ]a[ 0 ][ 2 ]a[ 0 ][ 3 ] a[ 2 ][ 0 ]a[ 2 ][ 1 ]a[ 2 ][ 2 ]a[ 2 ][ 3 ]
24
InitArray.jav a Line 16 Declare array1 with six initializers in two sublists Line 17 Declare array2 with six initializers in three sublists
25
InitArray.java Line 34 array[row].leng th returns number of columns associated with row subscript Line 35 Use double-bracket notation to access two- dimensional array values
26
Searching ◦ Finding elements in large amounts of data Determine whether array contains value matching key value ◦ Linear searching ◦ Binary searching
27
Linear search ◦ Compare each array element with search key If search key found, return element index If search key not found, return –1 (invalid index) ◦ Works best for small or unsorted arrays ◦ Inefficient for larger arrays
28
LinearSearch.java Line 11 Declare array of int s
29
LinearSearch.java Lines 39-42 Allocate 100 int s for array and populate array with even int s Line 50 Loop through array Lines 53-54 If array element at index matches search key, return index
30
LinearSearch.java Line 61 Invoked when user presses Enter Line 68 Invoke method linearSearch, using array and search key as arguments
31
Binary search ◦ Efficient for large, sorted arrays ◦ Eliminates half of the elements in search through each pass Compare middle array element to search key If element equals key Return array index If element is less than key Repeat search on first half of array If element is greater then key Repeat search on second half of array Continue search until element equals search key (success) Search contains one element not equal to key (failure)
32
Declae array of int s BinarySearch.java Line 14 Declare array of int s
33
BinarySearch.java Lines 48-51 Allocate 15 int s for array and populate array with even int s Line 56 Invoked when user presses Enter
34
BinarySearch.java Line 65 Invoke method binarySearch, using array and search key as arguments
35
BinarySearch.java Lines 93-94 If search key matches middle array element, return element index Lines 97-98 If search key is less than middle array element, repeat search on first array half Lines 101-102 If search key is greater than middle array element, repeat search on second array half Lines 112-137 Method build- Output displays array contents being searched
36
BinarySearch.java Line 128 Display an asterisk next to middle element
38
Sorting Motivation Generally, to arrange a list of elements in some order List of numbers 10 20 50 30 40 60 25(Unsorted list) 10 20 25 30 40 50 60 (Sorted list, ascending) 60 50 40 30 25 20 10 (Sorted list, descending) List of alphabets P A K I S T A N(Unsorted list) A A I K N P S T (Sorted list, ascending) T S P N K I A A (Sorted list, descending)
39
Sorting Algorithms 1.Bubble Sort 2.Selection Sort There are few more sorting algorithms; you can find them in a book on data structures and algorithms (or on the Web)
40
Bubble Sort Algorithm: Informal Repeatedly compare the elements at consecutive locations in a given list, and do the following until the elements are in required order: If elements are not in the required order, swap them (change their position) Otherwise do nothing
41
3 15 45 40 8 12 5 22 14 3 15 45 40 8 12 5 22 14 3 15 45 40 22 8 12 5 14 3 15 45 40 22 8 12 5 14 Bubble Sort in Action: Phase 1 3 15 45 40 8 12 22 5 14 3 15 45 40 8 22 12 5 14 3 15 45 40 22 8 12 5 14 3 45 15 40 22 8 12 5 14 45 3 15 40 22 8 12 5 14 8 comparisons8 comparisons
42
45 3 15 40 22 8 12 5 14 45 3 15 40 22 14 8 12 5 45 3 15 40 22 14 8 12 5 Bubble Sort in Action: Phase 2 45 3 15 40 22 8 12 14 5 45 3 15 40 22 8 14 12 5 45 3 15 40 22 14 8 12 5 45 3 40 15 22 14 8 12 5 45 40 3 15 22 14 8 12 5 7 comparisons7 comparisons
43
45 40 3 15 22 14 12 8 5 45 40 3 15 22 14 12 8 5 45 40 3 15 22 14 8 12 5 45 40 3 15 22 14 8 12 5 45 40 3 15 22 14 12 8 5 45 40 3 22 15 14 12 8 5 45 40 22 3 15 14 12 8 5 Bubble Sort in Action: Phase 3 6 comparisons6 comparisons
44
45 40 22 3 15 14 12 8 5 Bubble Sort in Action: Phase 4 45 40 22 3 15 14 12 8 5 45 40 22 3 15 14 12 8 5 45 40 22 3 15 14 12 8 5 45 40 22 3 15 14 12 8 5 45 40 22 15 3 14 12 8 5 5 comparisons5 comparisons
45
Bubble Sort in Action: Phase 5 45 40 22 15 3 14 12 8 5 4 comparisons4 comparisons 45 40 22 15 3 14 12 8 5 45 40 22 15 3 14 12 8 5 45 40 22 15 3 14 12 8 5 45 40 22 15 14 3 12 8 5
46
Bubble Sort in Action: Phase 6 3 comparisons3 comparisons 45 40 22 15 14 3 12 8 5 45 40 22 15 14 3 12 8 5 45 40 22 15 14 3 12 8 5 45 40 22 15 14 12 3 8 5
47
Bubble Sort in Action: Phase 7 2 comparisons2 comparisons 45 40 22 15 14 12 3 8 5 45 40 22 15 14 12 3 8 5 45 40 22 15 14 12 8 3 5
48
Bubble Sort in Action: Phase 8 1 comparison1 comparison 45 40 22 15 14 12 8 3 5 45 40 22 15 14 12 8 5 3
49
Bubble Sort Algorithm in Java void bubbleSort(int List[]) {int temp; int size = List.length; for (i = 0; i < size - 1; i++) { for (j = 0; j < size – (i + 1); j++) { if (List[j] > List[j+1]) {//swap temp = List[j]; List[j] = List[j+1]; List[j+1] = temp; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.