• Tidak ada hasil yang ditemukan

Fundamentals of Programming

N/A
N/A
Protected

Academic year: 2025

Membagikan "Fundamentals of Programming"

Copied!
32
0
0

Teks penuh

(1)

Fundamentals of Programming

session 20

Searching Arrays

(2)

Mean, median and mode

Write a program printing mean, median, and mode of an arrays of integers. You can assume that array elements are integers between 0 and 19.

stats.c

(3)

Mean, median and mode

Write a program printing mean, median, and mode of an arrays of integers. You can assume that array elements are integers between 0 and 19.

stats.c

(4)

Mean, median and mode

Assume that the array elements are integers between 0 and 19.

stats.c

(5)

Static vs automatic arrays

stats2.c

(6)

Median

stats.c

(7)

Median

stats.c

side effect?

(8)

Median

stats2.c

(9)

Searching arrays

25 19 14 18 27 6 32 18 20 1 21

● Is the number 19 in the array?

● What is its index?

(10)

linear search

linearsearch.c

(11)

linear search

linearsearch.c

(12)

linear search

● Average no. of elements to examine

● What if the array is sorted?

1 6 14 16 18 19 20 21 25 27 32

(13)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

search for 37

(14)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

search for 37

(15)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

search for 37

(16)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

search for 37

(17)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

search for 37

(18)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

search for 37

(19)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

search for 37

(20)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

search for 37

(21)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

search for 37

(22)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

low high

(23)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

low middle high

(24)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

low middle high

(25)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

low middle high

(26)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

low middle high

(27)

Binary search

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

low middle high

(28)

1 6 14 16 18 19 20 21 25 27 32 37 40 42 44 48

low middle high

● Assume size > 0

○ Can we ever have high <= low?

○ Why high = low + 1 out of the loop?

○ What if size == 1?

● What if size == 0?

● How many comparisons?

● Compare to linear search

○ size = 64

○ size = 1024

○ size ≃ 1000,000,000

● Sorting the array first?

(29)

2D arrays

0 0 0 0 0 0

0 1 2 3 4 5

0 2 4 6 8 10

0 3 6 9 12 15

0 4 8 12 16 20

0 5 10 15 20 25

(30)

2D arrays

0 0 0 0 0 0

0 1 2 3 4 5

0 2 4 6 8 10

0 3 6 9 12 15

0 4 8 12 16 20

0 5 10 15 20 25

https://advancedmathclubsk.weebly.com/matrices.html

(31)

2D arrays

https://hiveminer.com/Tags/desert,isfahan/Recent

(32)

Referensi

Dokumen terkait

¾ Students are able to explain the difference between One-Dimensional Arrays and other data types ¾ Students are able to compose a program to solve a problem that has data type

The next step, the writer calculated the scores of mean, median, mode, standard error of mean, and standard deviation using SPSS 21 program as follows.. 0 5 10 15

Listing 6.1 standardsquareroot.py imported the square root function as follows: from math import sqrt A program that needs to compute square roots, common logarithms, and the

Chapter 8 Function The first example in the slide: mean median mode they are "average" in different senses.. 2 write a program to get input and use findmax to sort the

In statistical analyses, which one of the following statements is true about the mean, median and mode of a normal distribution?. A The mean is always greater than the median and mode

Write a Python program to do the following operations: Library: NumPy i Create multi-dimensional arrays and find its shape and dimension ii Create a matrix full of zeros and ones

ALGORITHM OF BUBBLE SORT Bubble sort algorithm starts by comparison of the first two elements of an array and swapping if required, i.e., if you want to sort the elements of an array

This document is a daily lesson plan for a math class that covers the topic of measures of central tendency, including mean, median, and