ARRAYS - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

ARRAYS

Description:

Initial conditions : unsorted. x: 0. size. Selection Sort. General situation : ... Total number of steps proportional to n2. Insertion Sort #define MAXN 100 ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 28
Provided by: Sudeshn7
Category:

less

Transcript and Presenter's Notes

Title: ARRAYS


1
ARRAYS
  • Lecture 14
  • 11.2.2002.

2
  • 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
3
int findmax (int x, int size) if (size
1) return x0 maxl findmax (x, size-1)
if (x0 gt max) max x0 return
max
4
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
5
Searching an ArrayLinear and Binary Search
6
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

7
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

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

9
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.

10
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.

11
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 (_________________)
  • ____________________

12
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
13
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
14
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
15
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
16
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)
17
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 ?

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

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

size
0
x
  • Initial conditions

size
0
unsorted
x
20
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
21
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

22
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

23
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
24
Analysis
  • How many steps are needed to sort n things ?
  • Total number of steps proportional to n2

25
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)

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

27
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