Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1010E Programming Methodology Tutorial 6 Arrays and Search C14,A15,D11,C08,C11,A02.

Similar presentations


Presentation on theme: "CS1010E Programming Methodology Tutorial 6 Arrays and Search C14,A15,D11,C08,C11,A02."— Presentation transcript:

1 CS1010E Programming Methodology Tutorial 6 Arrays and Search C14,A15,D11,C08,C11,A02

2 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} ?;

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?

4 Question 1 printArray(list1, 5); 11 22 33 44 55 1122334455 99 list1list2

5 Question 1 passElement(list1[0]); printArray(list1, 5); 11 22 33 44 55 1122334455 99 list1list2

6 Question 1 changeElements(list2); printArray(list2, 5); ?? 1122334455 99 list1list2 list[2] list[4]

7 Question 1 changeElements(list2); printArray(list2, 5); 99 99 77 99 88 1122334455 99 779988 list1list2 list[2] list[4]

8 Question 1 copyArray(list2, list1, 5) ); printArray(list2, 5); ??? 1122334455 99 779988 list1list2

9 Question 1 copyArray(list2, list1, 5) ); printArray(list2, 5); 1122334455 1199779988 list1list2 List1[0] List2[0]

10 Question 1 copyArray(list2, list1, 5) ); printArray(list2, 5); 1122334455 1122779988 list1list2 List1[1] List2[1]

11 Question 1 copyArray(list2, list1, 5) ); printArray(list2, 5); 1122334455 1122339988 list1list2 List1[2] List2[2]

12 Question 1 copyArray(list2, list1, 5) ); printArray(list2, 5); 1122334455 1122334488 list1list2 List1[3] List2[3]

13 Question 1 copyArray(list2, list1, 5) ); printArray(list2, 5); 11 22 33 44 55 1122334455 1122334455 list1list2 List1[4] List2[4]

14 Question 1 printPattern(list1,5); ??? 1122334455 1122334455 list1list2

15 Question 1 printPattern(list1,5); printArray(list+4, 1) 55 1122334455 1122334455 list1list2 i = 1list+ 5 – i = list + 4

16 Question 1 printPattern(list1,5); printArray(list+3, 1) 44 55 1122334455 1122334455 list1list2 i = 2list+ 5 – i = list + 3

17 Question 1 printPattern(list1,5); printArray(list+2, 1) 33 44 55 1122334455 1122334455 list1list2 i = 3list+ 5 – i = list + 2

18 Question 1 printPattern(list1,5); printArray(list+1, 1) 22 33 44 55 1122334455 1122334455 list1list2 i = 4list+ 5 – i = list + 1

19 Question 1 printPattern(list1,5); printArray(list+0, 1) 11 22 33 44 55 1122334455 1122334455 list1list2 i = 5list+ 5 – i = list + 0

20

21

22

23

24

25

26

27 Question 3 (a) 713203844528889909239 k Stops, return 4 key

28 Question 3 (a) Translate Algorithm I into Code: Direct Translate Cleaner version

29 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 element 1.tmp = list[start+n-1] 2.Reverse Scan list start+n-1  start +1 1.list[k] = list[k-1] 3.Set start element to 1.list[start] = temp;

30 Question 3(b) Algorithm II: 1.Store the last element 1.tmp = list[start+n-1] 2.Reverse Scan list start+n-1  start +1 1.list[k] = list[k-1] 3.Set start element to 1.list[start] = temp; 7132038445288899092 Start = 1, n = 4 Start + n -1 = 4 k [1, 4] tmp = 44

31 Question 3(b) Algorithm II: 1.Store the last element 1.tmp = list[start+n-1] 2.Reverse Scan list start+n-1  start +1 1.list[k] = list[k-1] 3.Set start element to 1.list[start] = temp; 7132038 5288899092 Start = 1, n = 4 k [1, 4] tmp = 44

32 Question 3(b) Algorithm II: 1.Store the last element 1.tmp = list[start+n-1] 2.Reverse Scan list start+n-1  start +1 1.list[k] = list[k-1] 3.Set start element to 1.list[start] = temp; 71320 385288899092 Start = 1, n = 4 k [1, 4] tmp = 44

33 Question 3(b) Algorithm II: 1.Store the last element 1.tmp = list[start+n-1] 2.Reverse Scan list start+n-1  start +1 1.list[k] = list[k-1] 3.Set start element to 1.list[start] = temp; 713 20385288899092 Start = 1, n = 4 k [1, 4] tmp = 44 Stops scan

34 Question 3(b) Algorithm II: 1.Store the last element 1.tmp = list[start+n-1] 2.Reverse Scan list start+n-1  start +1 1.list[k] = list[k-1] 3.Set start element to 1.list[start] = temp; 7441320385288899092 Start = 1, n = 4 k [1, 4] tmp = 44

35 Question 3 (b) Translate Algorithm II into Code Code for Algorithm II

36 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

37 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) 233033353240181927 list list[0…3] is sorted list[4] When scanning list[k], list[0…k-1] is sorted

38 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) 233033353240181927 list list[0…3] is sorted list[4] Insertion position Index = 2

39 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) 233033353240181927 list Rightshift[2, 3] list[4] Insertion position Index = 2

40 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) 233032333540181927 list list[0, 4] is sorted move to list[5]

41 Question 3 (c) Translate Algorithm III into code: Code for Algorithm III

42


Download ppt "CS1010E Programming Methodology Tutorial 6 Arrays and Search C14,A15,D11,C08,C11,A02."

Similar presentations


Ads by Google