420 likes | 539 Vues
This tutorial explores essential concepts of arrays and search algorithms fundamental to programming methodology. It covers the declaration, initialization, and access of homogeneous data collections in arrays. Key topics include array length management, pass-by-reference behavior, and common array operations like searching, copying, and sorting elements. The tutorial includes practical coding examples, helping students grasp the importance of arrays in programming. By the end, learners will have a solid understanding of handling arrays effectively and implementing basic search algorithms.
E N D
CS1010E Programming MethodologyTutorial 6Arrays and Search C14,A15,D11,C08,C11,A02
Arrays • Homogenous Collection of Data • Declaration: • TYPE name [NumberofElement > 0] • int scores[10]; char name[5]; double prices[7]; etc • Initialization: • Initialize before use !!!! • Initializing each element separately • for(i=0;i<10;i++) arr[i] = something; • Initializing array at the time of declaration • int a[] = {1, 2, 3,4}; • What if int a[10] = {1,2,3} ?;
Arrays • Access by index: • a[10], a[0] etc. • Access by address: • *(a+ 10) Why? • Array Specials : • Array Name: • stores the address of the first element • Array Length: • Always keep the length of array!! This is vital! • Array is pass-by-reference • Why & how ? • No variable size: • inti = 10; inta[i]; IS WRONG!! What is the output? What is the output?
Question 1 • printArray(list1, 5); • 11 22 33 44 55 list1 list2
Question 1 • passElement(list1[0]); • printArray(list1, 5); • 11 22 33 44 55 list1 list2
Question 1 • changeElements(list2); • printArray(list2, 5); • ?? list1 list2 list[2] list[4]
Question 1 • changeElements(list2); • printArray(list2, 5); • 99 99 77 99 88 list1 list2 list[2] list[4]
Question 1 • copyArray(list2, list1, 5) ); • printArray(list2, 5); • ??? list1 list2
Question 1 • copyArray(list2, list1, 5) ); • printArray(list2, 5); list1 list2 List1[0] List2[0]
Question 1 • copyArray(list2, list1, 5) ); • printArray(list2, 5); list1 list2 List1[1] List2[1]
Question 1 • copyArray(list2, list1, 5) ); • printArray(list2, 5); list1 list2 List1[2] List2[2]
Question 1 • copyArray(list2, list1, 5) ); • printArray(list2, 5); list1 list2 List1[3] List2[3]
Question 1 • copyArray(list2, list1, 5) ); • printArray(list2, 5); • 11 22 33 44 55 list1 list2 List1[4] List2[4]
Question 1 • printPattern(list1,5); • ??? list1 list2
Question 1 • printPattern(list1,5); • printArray(list+4, 1) • 55 list1 list2 list+ 5 – i = list + 4 i = 1
Question 1 • printPattern(list1,5); • printArray(list+3, 1) • 44 55 list1 list2 list+ 5 – i = list + 3 i = 2
Question 1 • printPattern(list1,5); • printArray(list+2, 1) • 33 44 55 list1 list2 list+ 5 – i = list + 2 i = 3
Question 1 • printPattern(list1,5); • printArray(list+1, 1) • 22 33 44 55 list1 list2 list+ 5 – i = list + 1 i = 4
Question 1 • printPattern(list1,5); • printArray(list+0, 1) • 11 22 33 44 55 list1 list2 list+ 5 – i = list + 0 i = 5
Question 3 (a) • FindIndex(int list[], intnumElem, int key) • Return the position where the key should be inserted • Algorithm I : • Scan the list, [0..numElem -1] • Find the first element whose value is greater than key • if(list[k] key) continue • if(list[k] > key) break; • Return the position of that element key Stops, return 4
Question 3 (a) • Translate Algorithm I into Code: Direct Translate Cleaner version
Question 3 (b) • shiftRight(int list[], int start, int n) • Right shift 1 position in the range of [ start, start + n -1] • Right most element will become the first one • Algorithm II: • Store the last element • tmp = list[start+n-1] • Reverse Scan list start+n-1 start +1 • list[k] = list[k-1] • Set start element to • list[start] = temp;
Question 3(b) • Algorithm II: • Store the last element • tmp = list[start+n-1] • Reverse Scan list start+n-1 start +1 • list[k] = list[k-1] • Set start element to • list[start] = temp; Start = 1, n = 4 Start + n -1 = 4 [1, 4] tmp = 44
Question 3(b) • Algorithm II: • Store the last element • tmp = list[start+n-1] • Reverse Scan list start+n-1 start +1 • list[k] = list[k-1] • Set start element to • list[start] = temp; Start = 1, n = 4 [1, 4] tmp = 44
Question 3(b) • Algorithm II: • Store the last element • tmp = list[start+n-1] • Reverse Scan list start+n-1 start +1 • list[k] = list[k-1] • Set start element to • list[start] = temp; Start = 1, n = 4 [1, 4] tmp = 44
Question 3(b) • Algorithm II: • Store the last element • tmp = list[start+n-1] • Reverse Scan list start+n-1 start +1 • list[k] = list[k-1] • Set start element to • list[start] = temp; Start = 1, n = 4 [1, 4] tmp = 44 Stops scan
Question 3(b) • Algorithm II: • Store the last element • tmp = list[start+n-1] • Reverse Scan list start+n-1 start +1 • list[k] = list[k-1] • Set start element to • list[start] = temp; Start = 1, n = 4 [1, 4] tmp = 44
Question 3 (b) • Translate Algorithm II into Code Code for Algorithm II
Question 3 (c) • InsertionSort(int list[], int n) • Sort elements in list in increasing order • Algorithm III: • Scan list from [1, n-1] • Find insertion position • Index = findIndex(k + 1, list[k]); • Insert list[k] to that position • rightshift(index, k-index+1) When scanning list[k],list[0…k-1] is sorted
Question 3 (c) • Algorithm III: • For element list[k] • Find insertion position • Index = findIndex(k + 1, list[k]); • Insert list[k] to that position • rightshift(index, k-index+1) When scanning list[k],list[0…k-1] is sorted list list[0…3] is sorted list[4]
Question 3 (c) • Algorithm III: • For element list[k] • Find insertion position • Index = findIndex(k + 1, list[k]); • Insert list[k] to that position • rightshift(index, k-index+1) list Insertion position list[0…3] is sorted list[4] Index = 2
Question 3 (c) • Algorithm III: • For element list[k] • Find insertion position • Index = findIndex(k + 1, list[k]); • Insert list[k] to that position • rightshift(index, k-index+1) list Insertion position Rightshift[2, 3] Index = 2 list[4]
Question 3 (c) • Algorithm III: • For element list[k] • Find insertion position • Index = findIndex(k + 1, list[k]); • Insert list[k] to that position • rightshift(index, k-index+1) list list[0, 4] is sorted move to list[5]
Question 3 (c) • Translate Algorithm III into code: Code for Algorithm III
Thank you See you next week!!