ARRAYS - PowerPoint PPT Presentation

About This Presentation
Title:

ARRAYS

Description:

vowels has size 6, only 3 values are initialized. ... Initial conditions : unsorted. x: 0. size. Selection Sort. General situation : remainder, unsorted ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 37
Provided by: Sudeshn7
Category:

less

Transcript and Presenter's Notes

Title: ARRAYS


1
ARRAYS
  • Lecture 12
  • 27.2.2001.

2
Add an element in the frontShifting an array
  • / Shift x0,x1, ... ,xn-1 one position
    upwards (rightwards)to make space for a new
    element at x0
  • Insert the value newval at x0, update size
    /
  • int xMAX, size, i
  • . . .
  • for (isize igt1 ii-1)
  • xi xi-1
  • x0 newval
  • size

3
Shifting Array Elements
?
5
7
4
size 3 newval 0
5
5
7
4
5
7
7
4
0
4
7
5
5
7
4
4
Delete the ith element of the array shift left
  • / Shift xi1, ... , xn-1, one position to
    the left thus deleting item xi /
  • for (ki klt(size-1) k)
  • xk xk1
  • size size-1

size 6
1
2
3
5
4
6
1
2
4
5
4
6
1
2
4
5
5
6
size 5
1
2
4
6
5
6
5
Array Initialization
  • int marks10 10,8,7,9,6,10,6,7,8,8
  • marks has size 4, all values are initialized
  • char vowels6 'a','e','i'
  • vowels has size 6, only 3 values are initialized.
  • Can not use this notation in assignment
    statement.
  • Int w10
  • w 1,2,3,4 Syntax error
  • double x 1.2,3.4,8.1,0.9 / x has size 4,
    all values initialized /
  • double x Illegal

6
Passing array elements to functions
  • Array element can be passed to functions as
    ordinary arguments.
  • IsFactor (xi, x0)
  • sin (x5)

7
Passing Arrays to a Function
  • An array name can be used as an argument to a
    function.
  • Permits the entire array to be passed to the
    function.
  • The way it is passed differs from that for
    ordinary variables.
  • Rules
  • The array name must appear by itself as argument,
    without brackets or subscripts.
  • The corresponding formal argument is written in
    the same manner.
  • Declared by writing the array name with a pair of
    empty brackets.

8
Whole array as Parameters
  • define ASIZE 200
  • double average (int a)
  • int i, total0
  • for (i0 iltASIZE i)
  • total total ai
  • return ((double) total / (double) ASIZE)
  • fun (. . .)
  • int xASIZE double x_avg
  • . . .
  • x_avg average (x)
  • . . .

9
Arrays as Output Parameters
  • void VectorSum (int a, int b, int vsum, int
    length)
  • int i
  • for (i0 iltlength ii1)
  • vsumi ai bi
  • int main (void)
  • int x3 1,2,3, y3 4,5,6, z3
  • VectorSum (x, y, z, 3)
  • PrintVector (z, 3)
  • void PrintVector (int a, int length)
  • int i
  • for (i0 iltlength i) printf (d , ai)

10
The Actual Mechanism
  • When an array is passed to a function, the values
    of the array elements are not passed to the
    function.
  • The array name is interpreted as the address of
    the first array element.
  • The formal argument therefore becomes a pointer
    to the first array element.
  • When an array element is accessed inside the
    function, the address is calculated using the
    formula stated before.
  • Changes made inside the function are thus also
    reflected in the calling program.

11
Array operations
int readarray (int x, int size) int i
for (i0 iltsize i) scanf(d, xi)
return size
  • define MAXS 100
  • int insert (int, int, int, int)
  • int delete (int, int, int)
  • int getelement (int, int, int)
  • int readarray (int, int)
  • main ()
  • int aMAXS
  • int size
  • size readarray (a, 10)
  • size insert (a, size, 4, 7)
  • x getelement (a, size, 3)
  • size delete (a, size, 3)

int getelement (int x, int size, int pos)
if (pos ltsize) return xpos return -1
int insert (int x, int size, int pos. int
val) for (ksize kgtpos k--) xk
xk-1 xpos val return size1
12
  • void reverse (int x, int size)
  • int i
  • for (i0 ilt (size/2) i)
  • temp xsize-i-1
  • xsize-1-1 xi
  • xi temp

int findmax (int x, int size) int i,
max max x0 for (i1 ilt size
i) if (xi gt max) max xi
return max
13
Strings
  • Strings are 1-dimensional arrays of type char.
  • By convention, a string in C is terminated by the
    end-of-string sentinel \0, or null character.
  • String constant abc is a character array of
    size 4, with the last element being the null
    chaaracter \0.
  • char s abc

a
b
c
\0
14
Searching an ArrayLinear and Binary Search
15
Searching
  • Check if a given element (key) occurs in the
    array.
  • If the array is unsorted
  • start at the beginning of the array
  • inspect every element to see if it matches the key

16
Linear Search
  • / If key appears in a0..size-1, return its
    location, pos, s.t. apos key. If key is not
    found, return -1 /
  • int search (int a, int size, int key)
  • int pos 0
  • while (pos lt size apos ! key)
  • pos
  • if (posltn)
  • return pos
  • return -1

17
Linear Search
  • int x 12,-3, 78,67,6,50,19,10
  • Trace the following calls
  • search (x, 8,6)
  • search (x,8,5)

18
Searching a sorted array
  • Binary search works if the array is sorted
  • Look for the target in the middle
  • If you dont find it, you can ignore half of the
    array, and repeat the process with the other half.

19
Binary Search Strategy
  • What we want Find split betwen values larger
    and smaller than x

0
L
R
x
n
ltkey
gtkey
  • Situation while searching

0
n
L
R
x
ltkey
?
gtkey
  • Step Look at (LR)/2. Move L or R to the
    middle depending on test.

20
Binary Search
  • / If key appears in x0..size-1, return its
    location, pos s.t. xposkey. If not found,
    return -1 /
  • int binsearch (int x, int size, int key)
  • int L, R, mid
  • ______________________
  • while (_________________)
  • ____________________

21
Binary Search
  • / If key appears in x0..size-1, return its
    location, pos s.t. xposkey. If not found,
    return -1 /
  • int binsearch (int x, int size, int key)
  • int L, R, mid
  • ______________________
  • while (_________________)
  • mid (LR)/2
  • if (xmid lt key)
  • L mid
  • else R mid
  • ____________________

mid (LR)/2 if (xmid lt key) L
mid else R mid
22
Binary Search loop termination
  • / If key appears in x0..size-1, return its
    location, pos s.t. xposkey. If not found,
    return -1 /
  • int binsearch (int x, int size, int key)
  • int L, R, mid
  • ______________________
  • while (_________________)
  • mid (LR)/2
  • if (xmid lt key)
  • L mid
  • else R mid
  • ____________________

L1 ! R
23
Binary Search Return result
  • / If key appears in x0..size-1, return its
    location, pos s.t. xposkey. If not found,
    return -1 /
  • int binsearch (int x, int size, int key)
  • int L, R, mid
  • ______________________
  • while ( L1 ! R)
  • mid (LR)/2
  • if (xmid lt key)
  • L mid
  • else R mid

if (Lgt0 xLkey) return L else return -1
24
Binary Search Initialization
  • / If key appears in x0..size-1, return its
    location, pos s.t. xposkey. If not found,
    return -1 /
  • int binsearch (int x, int size, int key)
  • int L, R, mid
  • ______________________
  • while ( L1 ! R)
  • mid (LR)/2
  • if (xmid lt key)
  • L mid
  • else R mid
  • if (Lgt0 xLkey) return L
  • else return -1

L-1 Rsize
25
Binary Search Examples
-17 -5 3 6 12 21 45 63 50
Trace binsearch (x, 9, 3) binsearch (x, 9,
145) binsearch (x, 9, 45)
26
Is it worth the trouble ?
  • Suppose you had 1000 elements
  • Ordinary search (if key is a member of x) would
    require 500 comparisons on average.
  • Binary search
  • after 1st compare, left with 500 elements
  • after 2nd compare, left with 250 elements
  • After at most 10 steps, you are done.
  • What if you had 1 million elements ?

27
Sorting
  • Given an array x0, x1, ... , xsize-1
  • reorder entries so that
  • x0ltx1lt . . . ltxsize-1

28
Sorting Problem
  • What we want Data sorted in order
  • sorted x0ltx1lt . . . ltxsize-1

size
0
x
  • Initial conditions

size
0
unsorted
x
29
Selection Sort
  • General situation

k
0
size
x
remainder, unsorted
smallest elements, sorted
  • Step
  • Find smallest element, mval, in xk..size-1
  • Swap smallest element with xk, then increase
    k.

mval
0
size
k
x
smallest elements, sorted
30
Subproblem
  • / Yield location of smallest element intx in
    x0 .. size-1/
  • int min_loc (int x, int , int size)
  • int j, pos / xpos is the smallest element
    found so far /
  • pos k
  • for (jk1 jltsize j)
  • if (xi lt xpos)
  • pos j
  • return pos

31
Selection Sort
  • / Sort x0..size-1 in non-decreasing order /
  • int selsort (int x, int size)
  • int k, m
  • for (k0 kltsize-1 k)
  • m min_loc(x, k, size)
  • temp ak
  • ak am
  • am temp

32
Example
x
x
3
12
-5
6
142
21
-17
45
-17
-5
3
6
12
21
142
45
x
-17
12
-5
6
142
21
3
45
x
-17
-5
3
6
12
21
45
142
x
-17
-5
12
6
142
21
3
45
x
-17
-5
3
6
142
21
12
45
x
-17
-5
3
6
12
21
142
45
33
Analysis
  • How many steps are needed to sort n things ?
  • Total number of steps proportional to n2

34
Insertion Sort
  • define MAXN 100
  • void InsertSort (int listMAXN, int size)
  • main ()
  • int index, size
  • int numbersMAXN
  • / Get Input /
  • size readarray (numbers)
  • printarray (numbers, size)
  • InsertSort (numbers, size)
  • printarray (numbers, size)

35
  • void InsertSort (int list, int size)
  • for (i1 iltsize i)
  • item listi
  • for (ji-1 (jgt0) (listj gt i) j--)
  • listj1 listj
  • listj1 item

36
Common pitfalls with arrays in C
  • Exceeding the array bounds
  • int array10for (i0 ilt10 i) arrayi
    0
  • C does not support array declaratiions with
    variable expressions.
  • void fun (int array, int size) int
    tempsize . . .
Write a Comment
User Comments (0)
About PowerShow.com