קורס תכנות שיעור עשירי: מיונים, חיפושים, וקצת סיבוכיות חישוב.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Garfield AP Computer Science
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Sorting Algorithms and Average Case Time Complexity
CMPS1371 Introduction to Computing for Engineers SORTING.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
1 Gentle Introduction to Programming Session 5: Sorting, Searching, Time- Complexity Analysis, Memory Model, Object Oriented Programming.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Data Structures Review Session 1
Algorithm Efficiency and Sorting
Searching Arrays Linear search Binary search small arrays
Data Structures/ Algorithms and Generic Programming Sorting Algorithms.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
CSE 373 Data Structures Lecture 15
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
EENG212 Algorithms and Data Structures
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
February 4, 2005 Searching and Sorting Arrays. Searching.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
CSC 211 Data Structures Lecture 13
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Survey of Sorting Ananda Gunawardena. Naïve sorting algorithms Bubble sort: scan for flips, until all are fixed Etc...
Sort Algorithms.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
CSCI 51 Introduction to Programming March 12, 2009.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
 MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
1 Programming for Engineers in Python Autumn Lecture 9: Sorting, Searching and Time Complexity Analysis.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
Chapter 16: Searching, Sorting, and the vector Type.
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction to Search Algorithms
Recitation 13 Searching and Sorting.
שיעור עשירי: מיונים, חיפושים, וקצת סיבוכיות חישוב
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
קורס תכנות שיעור עשירי: מיונים, חיפושים, קצת ניתוח זמני ריצה, קצת תיקון טעויות ועוד על רשימות.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Linear and Binary Search
Algorithm design and Analysis
CSC215 Lecture Algorithms.
Search,Sort,Recursion.
Search,Sort,Recursion.
Searching.
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

קורס תכנות שיעור עשירי: מיונים, חיפושים, וקצת סיבוכיות חישוב

משתנים, מצביעים וכתובות משתנה מוגדר ע"י type name משתנה מצביע מוגדר ע"י type * name & - אופרטור שמחזיר כתובת של משתנה * - אופרטור שמקבל מצביע מחזיר הערך

מערכים ומצביעים מצביע מכיל כתובת, שם המערך הוא כתובת ניתן לבצע השמה בין המערך ומצביע לאחר ההשמה ניתן להשתמש במצביע כאילו היה שם המערך ניתן להשתמש במערך כאילו היה מצביע int *ptr; int array[10]; ptr = array; ptr[2] = 25; *array = 4;

למה צריך מצביעים ? ✝ משתנה ניתן לשנות את ערכו אך ורק בפונקציה בה הוגדר מצביע נותן לנו גישה אל משתנים שהוגדרו בפונקציות אחרות דוגמא: הפונקציה scanf משנה ערכים של משתנים פונקציה יכולה לחשב מספר ערכים ולשמור אותם בכתובות שהועברו אליה (כמו להחזיר מספר ערכים מפונקציה) ✝ עוד סיבות בהמשך

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

6 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

Vast Data 7

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:

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

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

11 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]

12 An Example value index

13 Code – Binary Search int binarySearchRec(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 binarySearchRec(arr,quary,start,middle-1); else return binarySearchRec(arr,quary,middle+1,end); }

14 Code – Main int a [] = {-5,-3,0,4,8,11,22,56,57,97}; printf("%d\n",binarySearch(a,SIZE,0)); printf("%d\n",binarySearch(a,SIZE,-4)); printf("%d\n",binarySearch(a,SIZE,8)); printf("%d\n",binarySearch(a,SIZE,1)); printf("%d\n",binarySearch(a,SIZE,-5)); printf("%d\n",binarySearch(a,SIZE,9)); printf("%d\n",binarySearch(a,SIZE,7)); int binarySearch(int arr[], int size, int quary) { return binarySearchRec(arr,quary,0,size-1); }

15 Output int a [] = {-5,-3,0,4,8,11,22,56,57,97}; printf("%d\n",binarySearch(a,SIZE,0)); printf("%d\n",binarySearch(a,SIZE,-4)); printf("%d\n",binarySearch(a,SIZE,8)); printf("%d\n",binarySearch(a,SIZE,1)); printf("%d\n",binarySearch(a,SIZE,-5)); printf("%d\n",binarySearch(a,SIZE,9)); printf("%d\n",binarySearch(a,SIZE,7));

16 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,000 it will take ~ 20 step (instead of ~milion)

17 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

18 An Example nlg nn lg nn2n ,04865,536 4, ,15216,777,216 65, ,048,5654,294,967,296 1,048, ,971,5201,099,511,627,776 16,777, ,653,183281,474,976,710,656

19 Binary Search Using Pointers (“light”) int binarySearch(int arr [], int size, int quary) { return binarySearchRec(arr,quary,0,size-1); } int binarySearchRec(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 binarySearchRec(arr,quary,start,middle-1); else return binarySearchRec(arr,quary,middle+1,end); }

20 Binary Search Using Pointers int * binarySearch(int arr [], int size, int quary) { return binarySearchRec(quary,arr,arr+size-1); } int * binarySearchRec(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 binarySearchRec(quary,start,middle-1); else return binarySearchRec(quary,middle+1,end); }

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

Iterative Binary Search 22 int binarySearch(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; }

Add an Element to a Sorted Array Input: input array and the value 10 Step by step: ?10< ?10<

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

Bubble Sort 26 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

27 Bubble Sort Example (done)

28 Bubble Sort Code void bubbleSort(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; }

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

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

31 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

32 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

33 Merge Sort Algorithm 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 sortrecursively Merge the two sub-arrays back into one sorted arrayMerge

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

35 Merge Sort Example

36 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 sortrecursively Merge the two sub-arrays back into one sorted arrayMerge n + 2 * (n/2) * n/ * n/2 3 + … + 2 log(n) * n/2 log(n) = n + n + … + n = (n+1) * log(n) log(n) +1

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

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

39 “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 

40 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 … }

41 Implementation Sketch in C Memory Comperator code array