Download presentation
Presentation is loading. Please wait.
Published byHillary Francis Modified over 8 years ago
1
ARRAYS Lecture 14 11.2.2002.
2
void reverse (int x[], int size) { int i; for (i=0; i< (size/2); i++) temp = x[size-i-1] ; x[size-1-1] = x[i] ; x[i] = temp; } int findmax (int x[], int size) { int i, max; max = x[0]; for (i=1; i< size; i++) if (x[i] > max) max = x[i] ; return max; }
3
int findmax (int x[], int size) { if (size ==1) return x[0] ; maxl = findmax (x, size-1) ; if (x[0] > max) max = x[0]; 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” ; abc\0
5
Searching an Array: Linear 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 a[0..size-1], return its location, pos, s.t. a[pos] == key. If key is not found, return -1 */ int search (int a[], int size, int key){ int pos = 0; while (pos < size && a[pos] != key) pos++; if (pos<n) 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 don’t 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 : <=key>key <=key?>key x: 0 n LR 0nLR Situation while searching : Step : Look at [(L+R)/2]. Move L or R to the middle depending on test.
11
Binary Search /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int binsearch (int x[], int size, int key){ int L, R, mid; ______________________; while (_________________){ } ____________________ ; }
12
/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int binsearch (int x[], int size, int key){ int L, R, mid; ______________________; while (_________________){ mid = (L+R)/2; if (x[mid] <= key) L = mid; else R = mid; } ____________________ ; } Binary Search mid = (L+R)/2; if (x[mid] <= key) L = mid; else R = mid;
13
/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int binsearch (int x[], int size, int key){ int L, R, mid; ______________________; while (_________________){ mid = (L+R)/2; if (x[mid] <= key) L = mid; else R = mid; } ____________________ ; } Binary Search: loop termination L+1 != R
14
/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int binsearch (int x[], int size, int key){ int L, R, mid; ______________________; while ( L+1 != R){ mid = (L+R)/2; if (x[mid] <= key) L = mid; else R = mid; } Binary Search: Return result if (L>=0 && x[L]==key) return L; else return -1;
15
/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int binsearch (int x[], int size, int key){ int L, R, mid; ______________________; while ( L+1 != R){ mid = (L+R)/2; if (x[mid] <= key) L = mid; else R = mid; } if (L>=0 && x[L]==key) return L; else return -1; } Binary Search: Initialization L=-1; R=size;
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 x[0], x[1],..., x[size-1] reorder entries so that x[0]<=x[1]<=... <=x[size-1]
19
Sorting Problem What we want : Data sorted in order sorted : x[0]<=x[1]<=... <=x[size-1] x: 0 size Initial conditions : unsorted x: 0 size
20
Selection Sort General situation : remainder, unsortedsmallest elements, sorted 0size k Step : Find smallest element, mval, in x[k..size-1] Swap smallest element with x[k], then increase k. x: smallest elements, sorted 0sizek x: mval
21
Subproblem : : /* Yield location of smallest element int[x] in x[0.. size-1];*/ int min_loc (int x[], int, int size) int j, pos; /* x[pos] is the smallest element found so far */ pos = k; for (j=k+1; j<size; j++) if (x[i] < x[pos]) pos = j; return pos; }
22
Selection Sort /* Sort x[0..size-1] in non-decreasing order */ int selsort (int x[], int size){ int k, m; for (k=0; k<size-1; k++){ m = min_loc(x, k, size); temp = a[k]; a[k] = a[m]; a[m] = temp; }
23
Example -1712-5614221345 x: 312-5614221-1745 x: -17-512614221345 x: -17-536142211245 x: -17-536122114245 x: -17-536122114245 x: -17-536122145142 x:
24
Analysis How many steps are needed to sort n things ? Total number of steps proportional to n 2
25
Insertion Sort #define MAXN 100 void InsertSort (int list[MAXN], int size) ; main (){ int index, size; int numbers[MAXN]; /* Get Input */ size = readarray (numbers) ; printarray (numbers, size) ; InsertSort (numbers, size) ; printarray (numbers, size) ; }
26
void InsertSort (int list[], int size){ for (i=1; i<size; i++) item = list[i] ; for (j=i-1; (j>=0)&& (list[j] > i); j--) list[j+1] = list[j]; list[j+1] = item ; }
27
Common pitfalls with arrays in C Exceeding the array bounds int array[10]; for (i=0; i<=10; i++) array[i] = 0; C does not support array declaratiions with variable expressions. void fun (int array, int size){ int temp[size] ;... }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.