strlen int strlenconst char str int len 0

  • Slides: 40
Download presentation

 מצביעים ומחרוזות בעזרת מצביע strlen • מימוש int strlen(const char* str) { int

מצביעים ומחרוזות בעזרת מצביע strlen • מימוש int strlen(const char* str) { int len = 0; if (str == NULL) return -1; /* Error! */ while (*str != '') { len++; str++; } return len; }

Today’s class • Data • The importance of fast access to data • How

Today’s class • Data • The importance of fast access to data • How can it be achieved? • Preprocessing followed by fast retrieval • The most basic data structure: arrays • Preprocessing sorting • fast retrieval searching • Time complexity 6

Vast Data 7

Vast Data 7

Sorting • A sorted array is an array that its elements are arranged in

Sorting • A sorted array is an array that its elements are arranged in descending/ascending order • One of the most common application on arrays • An example of a sorted array: 1 2 5 8 9 13 67 8

What is Sorting Good For? • Finding a number in an array • Consider

What is Sorting Good For? • Finding a number in an array • Consider a large array (of length n) and multiple queries on whether a given number exists in the array (and what is its position in it) • Naive solution: given a number, traverse the array and search for it • Not efficient ~ n/2 steps for each search operation • Can we do better? • Sort the array as a preliminary step. Now search can be performed much faster! 9

Search in a Non-Sorted Array • Search for value in array • If value

Search in a Non-Sorted Array • Search for value in array • If value is not in array return -1 • Traverse the elements by the array’s order int regular_search(int array[], int size, int value) { int i; for (i = 0; i < size; i++) { if (array[i] == value) return i; } return -1; } 10

Binary Search • Input: • A sorted array of integers A • An integer

Binary Search • Input: • A sorted array of integers A • An integer query q • Output: • -1 if q is not a member of A • The index of q in A otherwise • Algorithm: • • Check the middle element of A If it is equal to q, return its index If it is >= q, search for q in A[0, …, middle-1] If it is < q, search for q in A[middle+1, . . . , end] 11

An Example index 0 1 2 3 4 5 6 7 8 9 value

An Example index 0 1 2 3 4 5 6 7 8 9 value -5 -3 0 4 8 11 22 56 57 97 12

Code – Binary Search int binary. Search. Rec(int arr[], int quary, int start, int

Code – Binary Search int binary. Search. Rec(int arr[], int quary, int start, int end) { int middle; if (start > end) return -1; middle = start + (end-start) / 2; if (arr[middle] == quary) return middle; if (arr[middle] > quary) return binary. Search. Rec(arr, quary, start, middle-1); else return binary. Search. Rec(arr, quary, middle+1, end); } 13

Code – Main int a [] = {-5, -3, 0, 4, 8, 11, 22,

Code – Main int a [] = {-5, -3, 0, 4, 8, 11, 22, 56, 57, 97}; printf("%dn", binary. Search(a, SIZE, 0)); printf("%dn", binary. Search(a, SIZE, -4)); printf("%dn", binary. Search(a, SIZE, 8)); printf("%dn", binary. Search(a, SIZE, 1)); printf("%dn", binary. Search(a, SIZE, -5)); printf("%dn", binary. Search(a, SIZE, 9)); printf("%dn", binary. Search(a, SIZE, 7)); int binary. Search(int arr[], int size, int quary) { return binary. Search. Rec(arr, quary, 0, size-1); } 14

Output int a [] = {-5, -3, 0, 4, 8, 11, 22, 56, 57,

Output int a [] = {-5, -3, 0, 4, 8, 11, 22, 56, 57, 97}; printf("%dn", binary. Search(a, SIZE, 0)); printf("%dn", binary. Search(a, SIZE, -4)); printf("%dn", binary. Search(a, SIZE, 8)); printf("%dn", binary. Search(a, SIZE, 1)); printf("%dn", binary. Search(a, SIZE, -5)); printf("%dn", binary. Search(a, SIZE, 9)); printf("%dn", binary. Search(a, SIZE, 7)); 15

So, How Fast is it? • Worst case analysis • Size of the inspected

So, How Fast is it? • Worst case analysis • Size of the inspected array: n n/2 n/4 …. . 1 • Each step is very fast (a small constant number of operations) • There are log 2(n) such steps • So it takes ~ log 2(n) steps per search • Much faster then ~ n • If n = 1, 000 it will take ~ 20 step (instead of ~milion) 16

A Word About Time Complexity • Two common resources are space, measured by the

A Word About Time Complexity • Two common resources are space, measured by the number of deferred operations, and time, measured by the number of primitive steps • What matters? • Small instances are not “interesting” • Quick operations are considered as constant • What matters is the order of times that constant operations are performed • Assume we have an array of size n = 1 million: • Order of n 2 ~ constant * trillion (Tera) • Order of n = constant * million (Mega) • Order of log(n) = constant * 20 17

An Example n lg n n 2 1 0 0 1 16 4 64

An Example n lg n n 2 1 0 0 1 16 4 64 256 8 2, 048 65, 536 4, 096 12 49, 152 16, 777, 216 65, 536 16 1, 048, 565 4, 294, 967, 296 20 20, 971, 520 1, 099, 511, 627, 776 24 402, 653, 183 281, 474, 976, 710, 656 1, 048, 576 16, 777, 216 18

Binary Search Using Pointers int * binary. Search. Rec(int quary, int * start, int

Binary Search Using Pointers int * binary. Search. Rec(int quary, int * start, int * end) { int * middle; if (start > end) return NULL; middle = start + (end - start) / 2; if (*middle == quary) return middle; if (*middle > quary) return binary. Search. Rec(quary, start, middle-1); else return binary. Search. Rec(quary, middle+1, end); } int * binary. Search(int arr [], int size, int quary) { return binary. Search. Rec(quary, arr+size-1); } 20

Main & Output int a [] = {-5, -3, 0, 4, 8, 11, 22,

Main & Output int a [] = {-5, -3, 0, 4, 8, 11, 22, 56, 57, 97}; int * ind = binary. Search(a, SIZE, 0); if (ind != NULL) printf("%dn", ind - a); 21

Iterative Binary Search int binary. Search(int arr [], int size, int quary) { int

Iterative Binary Search int binary. Search(int arr [], int size, int quary) { int start= 0, end = size - 1; int middle; while (start <= end) { middle = (start + end) / 2; if (quary == arr [middle]) return middle; if (quary < arr [middle]) end = middle - 1; if (quary > arr [middle]) start = middle + 1; } return -1; } 22

Add an Element to a Sorted Array • Input: input array and the value

Add an Element to a Sorted Array • Input: input array and the value 10 1 2 5 8 9 13 1 2 5 8 10 13 67 9 13 67 • Step by step: 67 >10? >10? 23

Add an Element to a Sorted Array /* * Requires: * 1. The elements

Add an Element to a Sorted Array /* * Requires: * 1. The elements of the array are sorted in ascending order. * 2. length < capacity * length is the current number of elements in the array * capacity is the maximum number of elements array */ void array_add(int array[], int length, int value) { int i, j; for (i = 0; i < length && array[i] <= value; i++); for (j = length; j > i; j--) array[j] = array[j - 1]; array[i] = value; } 24

Cons and Pros of Arrays • Direct access (random access) • Fast search (for

Cons and Pros of Arrays • Direct access (random access) • Fast search (for sorted arrays) • Fast access • Less appropriate for dynamic operations • Add element • Remove element • Update element (remove + add) • We will study data structure that have the second bullet properties later in the course 25

Bubble Sort • Repeatedly stepping through the array to be sorted, comparing each pair

Bubble Sort • Repeatedly stepping through the array to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order • The pass through the array is repeated until no swaps are needed the array is sorted • “Bubbles” the correct elements to the top of the array 26

Bubble Sort Example 7 2 8 5 4 2 7 5 4 8 2

Bubble Sort Example 7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8 2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 2 7 5 8 4 2 5 4 7 8 (done) 2 7 5 4 8 27

Bubble Sort Code void bubble. Sort(int arr[], int size) { int i, j, tmp;

Bubble Sort Code void bubble. Sort(int arr[], int size) { int i, j, tmp; for (i = size - 1; i > 0; --i) for (j = 0; j < i; ++j) if (arr[j] > arr[j+1]) { // swap tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp; } } 28

Bubble Sort Code int main() { int i, a [] = {7, 2, 8,

Bubble Sort Code int main() { int i, a [] = {7, 2, 8, 5, 4}; bubble. Sort(a, SIZE); for (i = 0; i < SIZE; ++i) printf("%d ", a[i]); printf("n"); return 0; } 29

Bubble Sort Time Complexity Array of size n void bubble. Sort(int arr[], int size)

Bubble Sort Time Complexity Array of size n void bubble. Sort(int arr[], int size) { int i, j; for (i = size - 1; i > 0; --i) n iterations for (j = 0; j < i; ++j) i iterations if (arr[j] > arr[j+1]) { // swap (with trick) arr[j] = arr[j] + arr[j+1]; arr[j+1] = arr[j] - arr[j+1]; constant arr[j] = arr[j] - arr[j+1]; } } (n-1 + n-2 + n-3 + …. + 1) * const ~ ½ * n 2 30

Time Complexity Examples • Find a maximum in a general array • Find the

Time Complexity Examples • Find a maximum in a general array • Find the maximum in a sorted array • Find the 5 th largest element in a sorted array • Answer n Fibonacci quarries, each limited by MAX • Find an element in a general array • Find an element in a sorted array 31

The Idea Behind Merge Sort • A small list will take fewer steps to

The Idea Behind Merge Sort • A small list will take fewer steps to sort than a large list • Fewer steps are required to construct a sorted list from two sorted lists than two unsorted lists 32

Merge Sort Algorithm • If the array is of length 0 or 1, then

Merge Sort Algorithm • If the array is of length 0 or 1, then it is already sorted. Otherwise: • Divide the unsorted array into two subarrays of about half the size • Sort each sub-array recursively by reapplying merge sort • Merge the two sub-arrays back into one sorted array 33

Merge Sort (partial) Code void merge. Sort. Rec(int arr[], int start, int end) {

Merge Sort (partial) Code void merge. Sort. Rec(int arr[], int start, int end) { middle = (end - start) / 2; if ((end - start) < 2) return; merge. Sort. Rec(arr, start, middle); merge. Sort. Rec(arr, middle+1, end); merge. Arrays(arr, start, middle+1, end); } void merge. Sort(int arr [], int size) { return merge. Sort. Rec(arr, 0, size-1); } 34

Merge Sort Example 35

Merge Sort Example 35

Merge Sort Time Complexity • • If the array is of length 0 or

Merge Sort Time Complexity • • If the array is of length 0 or 1, then it is already sorted. Otherwise: Divide the unsorted array into two sub-arrays of about half the size Sort each sub-array recursively by re-applying merge sort Merge the two sub-arrays back into one sorted array n + 2 * (n/2) + 22 * n/22 + 23 * n/23 + … + 2 log(n) * n/2 log(n) = n + … + n = (n+1) * log(n) +1 36

Bucket Sort • Linear-time sorting algorithm! • • But: restrictions on data – bounded

Bucket Sort • Linear-time sorting algorithm! • • But: restrictions on data – bounded integers… 37

Sorting Strings • So far we only sorted numbers • How would we sort

Sorting Strings • So far we only sorted numbers • How would we sort strings? • Lexicographical order ( )מילוני • To be continued in tirgul… 38

“Generic” Sort • Sort an array of int/long/double/float/char in descending/ascending order • What is

“Generic” Sort • Sort an array of int/long/double/float/char in descending/ascending order • What is the difference between these cases? • Algorithmically the same! • Code duplication 39

The Idea Behind“Generic” Sort (Cont. ) • Sort an array of int/long/double/float/char in descending/ascending

The Idea Behind“Generic” Sort (Cont. ) • Sort an array of int/long/double/float/char in descending/ascending order • What are the parameters? • Array type • Comparison between array elements • Sort (array type arr, comperator comp) { … if (comp(arr[i], arr[j]) < 0) swap … } 40

Implementation Sketch in C Comperator code array Memory 41

Implementation Sketch in C Comperator code array Memory 41