CS1010E Programming MethodologyTutorial 6
Arrays and Search
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:•int i = 10; int a[i]; IS WRONG!!
What is the output?
What is the output?
Question 1
• printArray(list1, 5);• 11 22 33 44 55
11 22 33 44 55 99 99 99 99 99list1 list2
Question 1
• passElement(list1[0]);• printArray(list1, 5);
• 11 22 33 44 55
11 22 33 44 55 99 99 99 99 99list1 list2
Question 1
• changeElements(list2); • printArray(list2, 5);
• ??
11 22 33 44 55 99 99 99 99 99list1 list2
list[2] list[4]
Question 1
• changeElements(list2); • printArray(list2, 5);
• 99 99 77 99 88
11 22 33 44 55 99 99 77 99 88list1 list2
list[2] list[4]
Question 1
• copyArray(list2, list1, 5) );
• printArray(list2, 5); • ???
11 22 33 44 55 99 99 77 99 88list1 list2
Question 1
• copyArray(list2, list1, 5) );
• printArray(list2, 5);
11 22 33 44 55 11 99 77 99 88list1 list2
List1[0] List2[0]
Question 1
• copyArray(list2, list1, 5) );
• printArray(list2, 5);
11 22 33 44 55 11 22 77 99 88list1 list2
List1[1] List2[1]
Question 1
• copyArray(list2, list1, 5) );
• printArray(list2, 5);
11 22 33 44 55 11 22 33 99 88list1 list2
List1[2] List2[2]
Question 1
• copyArray(list2, list1, 5) );
• printArray(list2, 5);
11 22 33 44 55 11 22 33 44 88list1 list2
List1[3] List2[3]
Question 1
• copyArray(list2, list1, 5) );
• printArray(list2, 5); • 11 22 33 44 55
11 22 33 44 55 11 22 33 44 55list1 list2
List1[4] List2[4]
Question 1
• printPattern(list1,5); •???
11 22 33 44 55 11 22 33 44 55list1 list2
Question 1
• printPattern(list1,5);
• printArray(list+4, 1)•55
11 22 33 44 55 11 22 33 44 55list1 list2
i = 1list+ 5 – i = list + 4
Question 1
• printPattern(list1,5);
• printArray(list+3, 1)• 44 55
11 22 33 44 55 11 22 33 44 55list1 list2
i = 2list+ 5 – i = list + 3
Question 1
• printPattern(list1,5);
• printArray(list+2, 1)• 33 44 55
11 22 33 44 55 11 22 33 44 55list1 list2
i = 3list+ 5 – i = list + 2
Question 1
• printPattern(list1,5);
• printArray(list+1, 1)• 22 33 44 55
11 22 33 44 55 11 22 33 44 55list1 list2
i = 4list+ 5 – i = list + 1
Question 1
• printPattern(list1,5);
• printArray(list+0, 1)• 11 22 33 44 55
11 22 33 44 55 11 22 33 44 55list1 list2
i = 5list+ 5 – i = list + 0
Question 3 (a)• FindIndex(int list[], int numElem, int key)
• Return the position where the key should be inserted
• Algorithm I :
1. Scan the list, [0..numElem -1]
2. Find the first element whose value is greater than key1. if(list[k] key) continue
2. if(list[k] > key) break;
3. Return the position of that element
7 13 20 38 44 52 88 89 90 92 39
k Stops, return 4key
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:
1. Store the last element1. tmp = list[start+n-1]
2. Reverse Scan list start+n-1 start +11. list[k] = list[k-1]
3. Set start element to1. list[start] = temp;
Question 3(b)
• Algorithm II:
1. Store the last element1. tmp = list[start+n-1]
2. Reverse Scan list start+n-1 start +1
1. list[k] = list[k-1]
3. Set start element to1. list[start] = temp;
7 13 20 38 44 52 88 89 90 92 Start = 1, n = 4
Start + n -1 = 4
k
[1, 4]
tmp = 44
Question 3(b)
• Algorithm II:
1. Store the last element1. tmp = list[start+n-1]
2. Reverse Scan list start+n-1 start +1
1. list[k] = list[k-1]
3. Set start element to1. list[start] = temp;
7 13 20 38 38 52 88 89 90 92 Start = 1, n = 4
k
[1, 4]
tmp = 44
Question 3(b)
• Algorithm II:
1. Store the last element1. tmp = list[start+n-1]
2. Reverse Scan list start+n-1 start +1
1. list[k] = list[k-1]
3. Set start element to1. list[start] = temp;
7 13 20 20 38 52 88 89 90 92 Start = 1, n = 4
k
[1, 4]
tmp = 44
Question 3(b)
• Algorithm II:
1. Store the last element1. tmp = list[start+n-1]
2. Reverse Scan list start+n-1 start +1
1. list[k] = list[k-1]
3. Set start element to1. list[start] = temp;
7 13 13 20 38 52 88 89 90 92 Start = 1, n = 4
k
[1, 4]
tmp = 44Stops scan
Question 3(b)
• Algorithm II:
1. Store the last element1. tmp = list[start+n-1]
2. Reverse Scan list start+n-1 start +1
1. list[k] = list[k-1]
3. Set start element to1. list[start] = temp;
7 44 13 20 38 52 88 89 90 92 Start = 1, n = 4
k
[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)
23 30 33 35 32 40 18 19 27list
list[0…3] is sorted list[4]
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)
23 30 33 35 32 40 18 19 27list
list[0…3] is sorted list[4]
Insertion position
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)
23 30 33 35 32 40 18 19 27list
Rightshift[2, 3]
list[4]
Insertion position
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)
23 30 32 33 35 40 18 19 27list
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!!