Title: ARRAYS
1ARRAYS
2Add 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
3Shifting Array Elements
?
5
7
4
size 3 newval 0
5
5
7
4
5
7
7
4
0
4
7
5
5
7
4
4Delete 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
5Array 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
6Passing array elements to functions
- Array element can be passed to functions as
ordinary arguments. - IsFactor (xi, x0)
- sin (x5)
7Passing 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.
8Whole 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)
- . . .
9Arrays 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)
-
-
10The 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.
11Array 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
13Strings
- 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
14Searching an ArrayLinear and Binary Search
15Searching
- 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
16Linear 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
17Linear Search
- int x 12,-3, 78,67,6,50,19,10
- Trace the following calls
- search (x, 8,6)
- search (x,8,5)
18Searching 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.
19Binary 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.
20Binary 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 (_________________)
-
- ____________________
21Binary 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
22Binary 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
23Binary 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
24Binary 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
25Binary Search Examples
-17 -5 3 6 12 21 45 63 50
Trace binsearch (x, 9, 3) binsearch (x, 9,
145) binsearch (x, 9, 45)
26Is 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 ?
27Sorting
- Given an array x0, x1, ... , xsize-1
- reorder entries so that
- x0ltx1lt . . . ltxsize-1
28Sorting Problem
- What we want Data sorted in order
- sorted x0ltx1lt . . . ltxsize-1
size
0
x
size
0
unsorted
x
29Selection Sort
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
30Subproblem
- / 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
-
31Selection 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
-
32Example
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
33Analysis
- How many steps are needed to sort n things ?
- Total number of steps proportional to n2
34Insertion 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
-
36Common 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 . . .