งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Chapter 1/1 Arrays. Introduction Data structures are classified as either linear or nonlinear Linear structures: elements form a sequence or a linear.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Chapter 1/1 Arrays. Introduction Data structures are classified as either linear or nonlinear Linear structures: elements form a sequence or a linear."— ใบสำเนางานนำเสนอ:

1 Chapter 1/1 Arrays

2 Introduction Data structures are classified as either linear or nonlinear Linear structures: elements form a sequence or a linear list –Array and Linked lists Nonlinear structures: such as trees and graphs

3 Operations Operations perform on any linear structure –Traversal: processing each element in the list –Search: Finding the location of the element with a given value or the record with a given key –Insertion: Adding a new element to the list –Deletion: Removing an element from the list –Sorting: Arranging the elements in some type of order –Merging: Combining two lists into a single list

4 Linear Arrays A list of finite number n of homogeneous data elements 1. The elements of the array are referenced respectively an index set consisting of n consecutive numbers 2. The elements of the array are stored respectively in successive memory locations The number n of element is called the length or size of the array

5 Linear Arrays (2) Length = UB - LB + 1, where UB, upper bound, is the largest index and LB, lower bound, is the smallest index Array notations: 1. A 1, A 2, A 3, …, A n or 2. A(1), A(2), A(3), …, A(4) or 3. A[1], A[2], A[3], …, A[4]

6 Example 1 DATA[1] = 247DATA[2] = 56 DATA[3] = 429 DATA[4] = 135DATA[5] = 87 DATA[6] = 156 Sometimes, we will denote such an array as follow: DATA: 247, 56, 429, 135, 87, 156

7 Example 1(2) 156 87 135 429 56 247 1568713542956247 DATA 123456123456 1 2 3 4 5 6 Fig. 1: The array DATA (a) (b)

8 Example 2 An automobile company users an array AUTO to record the number of automobiles sold each year from 1932 through 1984 As the index is begun 1932, so that AUTO[K] = number of automobiles sold in the year K LB = 1932 and UB = 1984 Length = UB - LB + 1 = 1984 -1930 + 1 = 55 That is, AUTO contains 55 elements

9 Linear Arrays Representation in Memory Let LA be a linear array in the memory of the computer LOC(LA[K]) = address of the element LA[K] of the array LA LOC(LA[K]) = Base(LA) + w(K - lower bound) where, Base(LA) = the address of the first element of LA w = number of words per memory cell

10 Linear Arrays Representation in Memory (2) 1000 1001 1002 1003 1004 Fig. 2 Compute memory...

11 Example 3 Array AUTO records the number of automobiles sold each year from 1932 through 1984 Suppose Base(AUTO) = 200 and w = 4 words LOC(AUTO[1932]) = 200, LOC(AUTO[1933]) = 204, LOC(AUTO[1934]) = 208, … LOC(AUTO[1965]) = ?

12 200 201 202 203 204 205 206 207 208 209 210... AUTO[1932] AUTO[1933] Fig. 3: Array AUTO in memory Example 3 (2)

13 Example 3 (3) LOC(AUTO[1965]) = Base(AUTO) + w(1965 - LB) = 200 + 4(1965 - 1932) = 332

14 Two-Dimensional Arrays Representation Let A be a two-dimensional m * n array (a) Column-major order (b) Row-major order

15 Example of Two-dimensional Arrays (1,1) (2,1) (3,1) (1,2) (2,2) (3,2) (1,3) (2, 3) (3,3) (1,4) (2,4) (3,4) Column1 Column2 Column3 Column4 (a) Column –major order Fig. 4

16 (1,1) (1,2) (1,3) (1,4) (2,1) (2,2) (2, 3) (2,4) (3,1) (3,2) (3,3) (3,4) Row1 Row2 Row3 (b) Row-major orderFig. 4 Example of Two-dimensional Arrays (2)

17 Column-major order LOC(A[J, K]) = Base(LA) + w[m(K-1) + (J-1)] where Base(A) = the address of the first element A[1,1] of A m = a number of rows in one column

18 Row-major order LOC(A[J, K]) = Base(LA) + w[n(J-1) + (K-1)] where Base(A) = the address of the first element A[1,1] of A n = a number of columns in one row

19 Example 4 From Figure 4, if BASE( LA ) = 200 and w = 4 what is the address of LOC( LA [3, 2]) ? 1. Column-major order 2. Row-major order

20 General Multidimensional Arrays General multidimensional arrays are defined analogously An n-dimensional m 1 * m 2 * … * m n array B is a collection of m 1 m 2 … m n data elements Each element is specified by a list of n integers, K 1, K 2,…, K n, called subscripts, with the property that 1 <= K 1 <= m 1, 1 <= K 2 <= m 2, …, 1 <= K 1 <= m n, The element of B with subscripts will denoted by B K1, K2,…, K3 or B[K 1, K 2,…, K n ]

21 Example 5 Suppose B is a three-dimensional 2 * 4 * 3 array Then B contains 234 = 24 elements (as shown in Fig. 5) They appear in three layers, called pages Each page consists of the 24 rectangular array of elements with the same third subscript

22 B[1,1,3] B[1,2,3] B[1,3,3] B[1,4,3] B[2,1,2] B[2,2,3] B[2,3,3] B[2,4,3] B[1,1,2] B[1,2,2] B[1,3,2] B[1,4,2] B[2,1,2] B[2,2,2] B[2,3,2] B[2,4,2] B[1,1,1] B[1,2,1] B[1,3,1] B[1,4,1] B[2,1,1] B[2,2,1] B[2,3,1] B[2,4,1] Fig. 5 Page 3 Page 2 Page 1

23 Bsubscripts (1,1,1) (2,1,1) (1,2,1) (2,2,1) (2,3,1)............ (1,4,3) (2,4,3) Bsubscripts (1,1,1) (1,1,2) (1,1,3) (1,2,1) (1,2,2)............ (2,4,2) (2,4,3) (a) Column –major order. (b) Row-major order. Fig.6

24 General Multidimensional Arrays (3) LOC(C[K 1, K 2,…, K n ]) is computed by 1. Column major Base(C) + w[(((… (E N L N-1 + E N-1 ) L n-1 ) + … + E 3 ) L 2 + E 2 ) L 1 ) + E 1 ] 2. Row major Base(C) + w[(…(E 1 L 2 + E 2 ) L 3 + E 3 ) E 2 )L 4 + … + E N-1 ) L N + E N ] where Base(C) = the address of the first element of C w = the number of words per memory location E i = K i - lower bound L i = upper bound - lower bound + 1

25 Example 5 From figures 5 and 6 assume array is arranged by column major C is array 3 dimensional of 2 * 4 * 3 where Base = 200 and w = 4 what is the address of LOC[C[1, 3, 2]?

26 Example 6 A 3 dimensional array MAZE(2:8, -4:1, 6:10) It is organized in column major Base(MAZE) = 200 and w = 4 what is the address of MAZE[5,-1, 8] ?

27 Assignment 1 ให้นิสิตเขียน Pseudocode และโปรแกรมสำหรับ จัดการข้อมูลในแถวลำดับ (Arrays) ซึ่ง ประกอบด้วยรายละเอียดของนิสิตกลุ่มหนึ่ง ข้อมูล รหัสนิสิต ( จำนวนเต็ม 8 หลัก ), ชื่อ - สกุลนิสิต และ GPA กิจกรรม 1. เพิ่มข้อมูลในแถวลำดับ 2. ลบข้อมูลออกจากแถวลำดับ โดยใช้รหัสนิสิต เป็นคีย์หลัก 3. ปรับปรุงข้อมูลในแถวลำดับ โดยใช้รหัสนิสิตเป็น คีย์หลัก 4. พิมพ์ข้อมูลในแถวลำดับ

28 Assignment 1(2) ลักษณะงาน : เป็นงานเดี่ยว ต้องนำเสนองานด้วยตนเอง ( สอบปากเปล่า ) พร้อม Pseudocode ในเวลาคนละไม่เกิน 10 นาที กำหนดส่ง ระหว่างวันที่ 24-28 พ. ย. ( เลือกกำหนดวันใดวัน หนึ่ง จำนวน 3 ชั่วโมงติดต่อกัน / กลุ่ม สิ่งที่ไม่พึงปรารถนา ( ปรับตก ) – ไม่สามารถนำเสนอในเวลาที่กำหนด ( คนละ 10 นาที ) – ส่งไม่ตรงตามเวลาที่ตกลงไว้ – งานที่นำเสนอไม่ใช่ผลงานของตนเอง ( ลอกหรือ ให้ผู้อื่นทำงานให้ )


ดาวน์โหลด ppt Chapter 1/1 Arrays. Introduction Data structures are classified as either linear or nonlinear Linear structures: elements form a sequence or a linear.

งานนำเสนอที่คล้ายกัน


Ads by Google