Your SlideShare is downloading.
×

×
Saving this for later?
Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.

Text the download link to your phone

Standard text messaging rates apply

Like this presentation? Why not share!

- Dual Linear Array Probe for Corrosi... by Olympus IMS 154 views
- Arrays Basics by nik1020 211 views
- Array linear data_structure_2 (1) by eShikshak 2678 views
- Chapter 3 Arrays in Java by Khirulnizam Abd R... 1010 views
- Arrays Data Structure by student 198 views
- C Programming : Arrays by Rozy Gagan 688 views
- Arrays In C++ by Awais Alam 1449 views
- Sorting Algorithms by Mohammed Hussein 3979 views
- Problem Solving with Algorithms and... by Yi-Lung Tsai 2936 views
- Data structure and its types by Navtar Sidhu Brar 83983 views
- Bubble sort by Manek Ar 1096 views
- Bubble sort by rmsz786 653 views

299

Published on

No Downloads

Total Views

299

On Slideshare

0

From Embeds

0

Number of Embeds

0

Shares

0

Downloads

33

Comments

0

Likes

1

No embeds

No notes for slide

- 1. Lecture -3 onData structures Array
- 2. ArrayData structures are classified as either linear or nonlinear.A data structure is said to be linear if its elements form a sequence or a linearlist.There are two basic ways of representing such linear structures in memory.One way is to have the linear relationship between the elements represented bymeans of sequential memory locations. These linear structures are calledarrays.The other way is to have the linear relationship between the elementsrepresented by means of pointers or links. These linear structures are calledlinked lists.Nonlinear structures are trees and graphs.
- 3. Linear ArraysA linear array is a list of finite number n of homogeneous data elements such that :a) The elements of the array are referenced respectively by an index set consisting of n consecutive numbers.b) The elements of the array are stored respectively in successive memory locations.The number n of elements is called the length or size of the array.Three numbers define an array : lower bound, upper bound, size.a. The lower bound is the smallest subscript you can use in the array (usually 0)b. The upper bound is the largest subscript you can use in the arrayc. The size / length of the array refers to the number of elements in the array , It can be computed as upper bound - lower bound + 1Let, Array name is A then the elements of A is : a1,a2….. anOr by the bracket notation A[1], A[2], A[3],…………., A[n]The number k in A[k] is called a subscript and A[k] is called a subscripted variable.
- 4. Linear ArraysExample :A linear array DATA consisting of the name of six elements DATA DATA[1] = 247 1 247 DATA[2] = 56 2 56 3 DATA[3] = 429 429 4 135 DATA[4] = 135 5 87 DATA[5] = 87 6 156 DATA[6] = 156
- 5. Linear ArraysExample :An automobile company uses an array AUTO to record the number of auto mobilesold each year from 1932 through 1984.AUTO[k] = Number of auto mobiles sold in the year KLB = 1932UB = 1984Length = UB – LB+1 = 1984 – 1930+1 =55
- 6. Representation of linear array in memoryLet LA be a linear array in the memory of the computer. The memory of thecomputer is a sequence of addressed locations. LA10001001 The computer does not need to keep track of the1002 address of every element of LA, but needs to keep1003 track only of the first element of LA, denoted by1004 Base(LA)1005 Called the base address of LA. Using this address Base(LA), the computer calculates the address of any element of LA by the following formula : LOC(LA[k]) = Base(LA) + w(K – lower bound) Where w is the number of words per memory cell forFig : Computer memory the array LA
- 7. Representation of linear array in memory200201 Example : An automobile company uses an array AUTO to record202 the number of auto mobile sold each year from 1932203 AUTO[1932] through 1984. Suppose AUTO appears in memory as204 pictured in fig A . That is Base(AUTO) = 200, and w = 4205 words per memory cell for AUTO. Then,206 LOC(AUTO[1932]) = 200, LOC(AUTO[1933]) =204207 AUTO[1933] LOC(AUTO[1934]) = 208 the address of the array element for the year K = 1965208 can be obtained by using :209 LOC(AUTO[1965]) = Base(AUTO) + w(1965 – lower210 AUTO[1934] bound)211 =200+4(1965-1932)=332212 Fig : A
- 8. Traversing linear arrays Print the contents of each element of DATA or Count the number ofelements of DATA with a given property. This can be accomplished bytraversing DATA, That is, by accessing and processing (visiting) eachelement of DATA exactly once. Algorithm 2.3: Given DATA is a linear array with lower bound LB andupper bound UB . This algorithm traverses DATA applying an operationPROCESS to each element of DATA. 1. Set K : = LB. 2. Repeat steps 3 and 4 while K<=UB: 3. Apply PROCESS to DATA[k] 4. Set K : = K+1. 5. Exit.
- 9. Traversing linear arrays Example : An automobile company uses an array AUTO to record the number of auto mobile sold each year from 1932 through 1984. a) Find the number NUM of years during which more than 300 automobiles were sold. b) Print each year and the number of automobiles sold in that year1. Set NUM : = 0.2. Repeat for K = 1932 to 1984: 1. Repeat for K = 1932 to 1984:if AUTO[K]> 300, then : set NUM : = NUM+1 Write : K, AUTO[K]3. Exit. 2. Exit.
- 10. Inserting and DeletingInserting refers to the operation of adding another element to the ArrayDeleting refers to the operation of removing one element from the ArrayInserting an element somewhere in the middle of the array require that eachsubsequent element be moved downward to new locations to accommodate thenew element and keep the order of the other elements.Deleting an element somewhere in the middle of the array require that eachsubsequent element be moved one location upward in order to “fill up” thearray. Fig shows Milon Inserted, Sumona deleted. STUDENT STUDENT STUDENT 1 Dalia Rahaman 1 Dalia Rahaman Sumona 1 Dalia Rahaman Sumona 2 2 Milon 2 Milon Mubtasim Fuad 3 Mubtasim Fuad 3 Mubtasim Fuad 3 Anamul Haque 4 Anamul Haque 4 Anamul Haque 4 5 5 5 6 6 6
- 11. InsertionINSERTING AN ELEMENT INTO AN ARRAY:Insert (LA, N, K, ITEM)Here LA is linear array with N elements and K is a positive integer such thatK<=N.This algorithm inserts an element ITEM into the Kth position in LA.ALGORITHMStep 1. [Initialize counter] Set J:=NStep 2. Repeat Steps 3 and 4] while J>=KStep 3. [Move Jth element downward] Set LA [J+1]: =LA [J]Step 4. [Decrease counter] Set J:=J-1[End of step 2 loop]Step 5 [Insert element] Set LA [K]: =ITEMStep 6. [Reset N] Set N:=N+1Step 7. Exit
- 12. DeletionDELETING AN ELEMENT FROM A LINEAR ARRAYDelete (LA, N, K, ITEM)ALGORITHMStep 1. Set ITEM: = LA [K]Step 2. Repeat for J=K to N-1[Move J+1st element upward] Set LA [J]: =LA [J+1][End of loop]Step 3 [Reset the number N of elements in LA] Set N:=N-1Step 4. Exit
- 13. Bubble sortBubble sort is one of the easiest sort algorithms. It is called bubble sort becauseit will bubble values in your list to the top. Algorithm Bubble_Sort (DATA, N): 1. Repeat steps 2 and 3 for K = 1 to N-1. 2. Set PTR: =1.[Initializes pass pointer PTR] 3. Repeat while PTR<=N-K: [Executes pass] a) If DATA[PTR]>DATA[PTR+1],then: TEMP := DATA[PTR], DATA[PTR] := DATA[PTR+1],DATA[PTR+1] := temp [End of if structure] b) Set PTR: =PTR+1 [End of inner loop] [End of step 1 Outer loop] 4. Exit
- 14. Sorting : Bubble sort• Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 42 77 101
- 15. "Bubbling Up" the Largest Element• Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 77 42 35 12 101 5
- 16. "Bubbling Up" the Largest Element• Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42Swap77 12 101 77 42 35 5
- 17. "Bubbling Up" the Largest Element• Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42 77 Swap77 35 35 12 101 5
- 18. "Bubbling Up" the Largest Element• Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 12Swap12 77 101 42 35 77 5
- 19. "Bubbling Up" the Largest Element• Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42 35 12 77 101 5 No need to swap
- 20. "Bubbling Up" the Largest Element• Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42 35 12 77 5 Swap101 101 5
- 21. "Bubbling Up" the Largest Element• Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42 35 12 77 5 101 Largest value correctly placed
- 22. Putting It All Together
- 23. Items of Interest• Notice that only the largest value is correctly placed• All other values are still out of order• So we need to repeat this process 1 2 3 4 5 6 42 35 12 77 5 101 Largest value correctly placed
- 24. Repeat “Bubble Up” How Many Times?• If we have N elements…• And if each time we bubble an element, we place it in its correct location…• Then we repeat the “bubble up” process N – 1 times.• This guarantees we’ll correctly place all N elements.
- 25. “Bubbling” All the Elements 1 2 3 4 5 6 42 35 12 77 5 101 1 2 3 4 5 6 35 12 42 5 77 101 1 2 3 4 5 6N-1 12 35 5 42 77 101 1 2 3 4 5 6 12 5 35 42 77 101 1 2 3 4 5 6 5 12 35 42 77 101
- 26. Reducing the Number of Comparisons 1 2 3 4 5 677 42 35 12 101 5 1 2 3 4 5 642 35 12 77 5 101 1 2 3 4 5 635 12 42 5 77 101 1 2 3 4 5 612 35 5 42 77 101 1 2 3 4 5 612 5 35 42 77 101
- 27. Summary• “Bubble Up” algorithm will move largest value to its correct location (to the right)• Repeat “Bubble Up” until all elements are correctly placed: – Maximum of N-1 times – Can finish early if no swapping occurs• We reduce the number of elements we compare each time one is correctly placed
- 28. Complexity of the bubble sort algorithmThe time for a sorting algorithm is measured in terms of the number ofcomparisons. The number f(n) of comparisons in the bubble sort is easilycomputed. Specifically there are n -1 comparisons during first pass, which placesthe largest element in the last position, there are n -2 comparisons in the secondstep, which places the second largest element in the next – to - last position, andso on. Thusf(n) = (n-1)+(n-2)+. . . +2+1 =n(n-1)/2=n2/2+O(n)In other words, The time required to execute bubble sort algorithm is proportional ton2, where n is the number of input items.
- 29. Selection Sort• SelectionSort(A,N) for i:=1 to N-1 do for j:=i+1 to N-1 do if A[i] > A[j] then temp:=A[i] A[i] := A[j] A[j] := temp
- 30. Insertion Sort:• Insertionsort(A,N) for j:=2 to N key:=A[j] i:=j-1 while i>0 and A[i] > key do A[i]:=A[i+1] i-- A[i+1]:=key

Be the first to comment