strstr char str Ding Dong char res strstrstr




![strstr הפונקציה char str[] = “Ding Dong”; char *res = strstr(str, ”Don”); 6500 str strstr הפונקציה char str[] = “Ding Dong”; char *res = strstr(str, ”Don”); 6500 str](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-5.jpg)
![דוגמא – כמה פעמים מופיעה מילה בשיר int main() { const char rhyme[] דוגמא – כמה פעמים מופיעה מילה בשיר int main() { const char rhyme[]](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-6.jpg)








![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](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-15.jpg)
![Code – Main int a [] = {-5, -3, 0, 4, 8, 11, 22, Code – Main int a [] = {-5, -3, 0, 4, 8, 11, 22,](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-16.jpg)
![Output int a [] = {-5, -3, 0, 4, 8, 11, 22, 56, 57, Output int a [] = {-5, -3, 0, 4, 8, 11, 22, 56, 57,](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-17.jpg)




![חיפוש בינארי עם מצביעים int * binary. Search (int arr [ ], int חיפוש בינארי עם מצביעים int * binary. Search (int arr [ ], int](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-22.jpg)
![Main & Output int a [] = {-5, -3, 0, 4, 8, 11, 22, Main & Output int a [] = {-5, -3, 0, 4, 8, 11, 22,](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-23.jpg)
![חיפוש בינארי איטרטיבי int binary. Search(int arr [], int size, int quary) { חיפוש בינארי איטרטיבי int binary. Search(int arr [], int size, int quary) {](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-24.jpg)






![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;](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-31.jpg)
![Bubble Sort Code int main() { int i, a [] = {7, 2, 8, Bubble Sort Code int main() { int i, a [] = {7, 2, 8,](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-32.jpg)






![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) {](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-39.jpg)










- Slides: 49




![strstr הפונקציה char str Ding Dong char res strstrstr Don 6500 str strstr הפונקציה char str[] = “Ding Dong”; char *res = strstr(str, ”Don”); 6500 str](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-5.jpg)
strstr הפונקציה char str[] = “Ding Dong”; char *res = strstr(str, ”Don”); 6500 str D 6500 i n g D o n g res printf(“res=%pn”, res); printf(“res=%sn”, res); res=6505 res=Dong
![דוגמא כמה פעמים מופיעה מילה בשיר int main const char rhyme דוגמא – כמה פעמים מופיעה מילה בשיר int main() { const char rhyme[]](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-6.jpg)
דוגמא – כמה פעמים מופיעה מילה בשיר int main() { const char rhyme[] = "Humpty Dumpty sat on a wall, n " "Humpty Dumpty had a great fall. n " "All the king's horses, n" "And all the king's men, n" "Couldn't put Humpty together again. n"; const char humpty[] = "Humpty"; char *ptr = NULL; int count = 0; for (ptr = strstr(rhyme, humpty); ptr != NULL; ptr = strstr(ptr + 1, humpty)) { count++; } printf("The string %s appears %d timesn", humpty, count); return 0; }







דוגמא נחפש את . . . 56 9 8 7 6 5 4 3 2 1 0 index 97 57 56 22 11 8 4 0 -3 -5 value 13

דוגמא נחפש את . . . 4 9 8 7 6 5 4 3 2 1 0 index 97 57 56 22 11 8 4 0 -3 -5 value 14
![Code Binary Search int binary Search Recint arr int quary int start int Code – Binary Search int binary. Search. Rec(int arr[], int quary, int start, int](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-15.jpg)
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); } 15
![Code Main int a 5 3 0 4 8 11 22 Code – Main int a [] = {-5, -3, 0, 4, 8, 11, 22,](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-16.jpg)
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); } 16
![Output int a 5 3 0 4 8 11 22 56 57 Output int a [] = {-5, -3, 0, 4, 8, 11, 22, 56, 57,](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-17.jpg)
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)); 17



הבדלים מספריים 20 n 2 n lg n n 1 0 0 1 256 64 4 16 65, 536 2, 048 8 256 16, 777, 216 49, 152 12 4, 096 4, 294, 967, 296 1, 048, 565 16 65, 536 1, 099, 511, 627, 776 20, 971, 520 20 402, 653, 183 281, 474, 976, 710, 656 24 1, 048, 576 16, 777, 216

![חיפוש בינארי עם מצביעים int binary Search int arr int חיפוש בינארי עם מצביעים int * binary. Search (int arr [ ], int](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-22.jpg)
חיפוש בינארי עם מצביעים int * binary. Search (int arr [ ], int size, int quary) { int * middle; if (size == 0) return NULL; middle = arr + size/ 2; if (*middle == quary) return middle; if (*middle > quary) return binary. Search(arr, size/2, quary); else return binary. Search(arr+size/2+1, size-size/2 -1, quary); } 23
![Main Output int a 5 3 0 4 8 11 22 Main & Output int a [] = {-5, -3, 0, 4, 8, 11, 22,](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-23.jpg)
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); 24
![חיפוש בינארי איטרטיבי int binary Searchint arr int size int quary חיפוש בינארי איטרטיבי int binary. Search(int arr [], int size, int quary) {](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-24.jpg)
חיפוש בינארי איטרטיבי 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; } 25


הוספת מספר למערך ממויין /* * 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; } 27




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 31
![Bubble Sort Code void bubble Sortint arr int size int i j tmp Bubble Sort Code void bubble. Sort(int arr[], int size) { int i, j, tmp;](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-31.jpg)
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; } } 32
![Bubble Sort Code int main int i a 7 2 8 Bubble Sort Code int main() { int i, a [] = {7, 2, 8,](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-32.jpg)
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; } 33

מיון בועות – ניתוח סיבוכיות זמן ריצה 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 tmp = arr[j]; arr[j] = arr[j+1]; constant arr[j+1] = tmp; } } (n-1 + n-2 + n-3 + …. + 1) * const ~ ½ * n 2 34




מערכים ממויינים 2 איחוד p p p 1 2 5 7 9 p q q q 3 4 6 8 10 u u u u u 1 2 3 4 5 6 7 8 9 10 38

![Merge Sort partial Code void merge Sort Recint arr int start int end Merge Sort (partial) Code void merge. Sort. Rec(int arr[], int start, int end) {](https://slidetodoc.com/presentation_image/0f53dbbd0127ea13c4c041576262facd/image-39.jpg)
Merge Sort (partial) Code void merge. Sort. Rec(int arr[], int start, int end) { int 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); } 40

דוגמא - Merge Sort 41





Bucket Sort 46



