CS1010E Programming Methodology Tutorial 6 Arrays and Search

42
CS1010E Programming Methodology Tutorial 6 Arrays and Search

description

CS1010E Programming Methodology Tutorial 6 Arrays 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 !!!! - PowerPoint PPT Presentation

Transcript of CS1010E Programming Methodology Tutorial 6 Arrays and Search

Page 1: CS1010E Programming Methodology Tutorial 6 Arrays and Search

CS1010E Programming MethodologyTutorial 6

Arrays and Search

Page 2: CS1010E Programming Methodology Tutorial 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} ?;

Page 3: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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?

Page 4: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Question 1

• printArray(list1, 5);• 11 22 33 44 55

11 22 33 44 55 99 99 99 99 99list1 list2

Page 5: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Question 1

• passElement(list1[0]);• printArray(list1, 5);

• 11 22 33 44 55

11 22 33 44 55 99 99 99 99 99list1 list2

Page 6: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Question 1

• changeElements(list2); • printArray(list2, 5);

• ??

11 22 33 44 55 99 99 99 99 99list1 list2

list[2] list[4]

Page 7: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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]

Page 8: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Question 1

• copyArray(list2, list1, 5) );

• printArray(list2, 5); • ???

11 22 33 44 55 99 99 77 99 88list1 list2

Page 9: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Question 1

• copyArray(list2, list1, 5) );

• printArray(list2, 5);

11 22 33 44 55 11 99 77 99 88list1 list2

List1[0] List2[0]

Page 10: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Question 1

• copyArray(list2, list1, 5) );

• printArray(list2, 5);

11 22 33 44 55 11 22 77 99 88list1 list2

List1[1] List2[1]

Page 11: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Question 1

• copyArray(list2, list1, 5) );

• printArray(list2, 5);

11 22 33 44 55 11 22 33 99 88list1 list2

List1[2] List2[2]

Page 12: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Question 1

• copyArray(list2, list1, 5) );

• printArray(list2, 5);

11 22 33 44 55 11 22 33 44 88list1 list2

List1[3] List2[3]

Page 13: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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]

Page 14: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Question 1

• printPattern(list1,5); •???

11 22 33 44 55 11 22 33 44 55list1 list2

Page 15: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 16: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 17: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 18: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 19: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 20: CS1010E Programming Methodology Tutorial 6 Arrays and Search
Page 21: CS1010E Programming Methodology Tutorial 6 Arrays and Search
Page 22: CS1010E Programming Methodology Tutorial 6 Arrays and Search
Page 23: CS1010E Programming Methodology Tutorial 6 Arrays and Search
Page 24: CS1010E Programming Methodology Tutorial 6 Arrays and Search
Page 25: CS1010E Programming Methodology Tutorial 6 Arrays and Search
Page 26: CS1010E Programming Methodology Tutorial 6 Arrays and Search
Page 27: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 28: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Question 3 (a)

• Translate Algorithm I into Code:

Direct Translate

Cleaner version

Page 29: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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;

Page 30: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 31: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 32: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 33: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 34: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 35: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Question 3 (b)

• Translate Algorithm II into Code

Code for Algorithm II

Page 36: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 37: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 38: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 39: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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

Page 40: CS1010E Programming Methodology Tutorial 6 Arrays and Search

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]

Page 41: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Question 3 (c)

• Translate Algorithm III into code:

Code for Algorithm III

Page 42: CS1010E Programming Methodology Tutorial 6 Arrays and Search

Thank you See you next week!!