Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Algorithms & Pseudocode Orders of Magnitude Sequential Searching Finding Minimum value Linear Search Sorting Insertion Sort Selection Sort CSE 30331.

Similar presentations


Presentation on theme: "1 Algorithms & Pseudocode Orders of Magnitude Sequential Searching Finding Minimum value Linear Search Sorting Insertion Sort Selection Sort CSE 30331."— Presentation transcript:

1 1 Algorithms & Pseudocode Orders of Magnitude Sequential Searching Finding Minimum value Linear Search Sorting Insertion Sort Selection Sort CSE Lecture 3 – Algorithms I

2 2 Algorithms & Pseudocode Algorithm Well defined computational process (sequence of steps) that takes some input(s) and produces some output(s) Why discuss algorithms? Facilitate design of software modules Analyze costs (time and space) of software module in a manner that is independent of platform Pseudocode Method of writing algorithms that is independent of programming language

3 3 Constant Time Algorithms An algorithm is Θ(1) when its running time is independent of the number of data items. The algorithm runs in constant time. The storing of the element involves a simple assignment statement and thus has efficiency Θ(1).

4 4 Linear Time Algorithms An algorithm is Θ(n) when its running time is proportional to the size of the list. When the number of elements doubles, the number of operations doubles.

5 5 Less Efficient Algorithms Quadratic Algorithms: Θ (n 2 ) practical only for relatively small values of n. When n doubles, running time quadruples. Cubic Algorithms: Θ (n 3 ) efficiency is generally poor; doubling the size of n increases the running time eight-fold. Exponential: Θ (2 n ) Awful, but some algorithms have no better algorithmic solution

6 6 Algorithm Efficiency

7 7 Logarithmic Time Algorithms The logarithm of n, base 2, is commonly used when analyzing computer algorithms. Ex.lg(2) = log 2 (2) = 1 lg(1024) = log 2 (1024) = 10 When compared to the functions n and n 2, the function lg(n) grows very slowly.

8 8 A Simple Linear Algorithm Example: Find the minimum element in an array A Assume A contains n elements (n >= 1) Assume elements are not ordered Basic technique is sequential min(A,n) cost times smallest = A[0] c1 1 for i = 1 to (n-1) c2 n if A[i] < smallest c3 n-1 smallest = A[i] c4 0..n-1 return smallest c5 1

9 9 Analysis of min(A,n) min(A,n) cost times smallest = A[0] c1 1 for i = 1 to (n-1) c2 n if A[i] < smallest c3 n-1 smallest = A[i] c4 0..n-1 return smallest c5 1 Best: A[0] is min T(n) = c1 + c2(n) + c3(n-1) c5 T(n) = (c2+c3)(n) + (c1+c5-c3) = an + b linear or Θ(n) Worst: A[n-1] is min and A[i] > A[i+1] for all i=[0,n) T(n) = c1 + c2(n) + c3(n-1) + c4(n-1) + c5 T(n) = (c2+c3+c4)(n) + (c1+c5-c3-c4) = cn + d linear or Θ(n)

10 10 Linear Search Example: Find the target element t in an array A Assume A contains n elements (n >= 1) Assume elements are not ordered Basic technique is linear search Return value is index of smallest element in range 0..n, where n indicates not found linearSearch(A,n,t) cost times i = 0 c1 1 while (i < n) and (A[i] != t) c2 1..n+1 i = i + 1 c3 0..n return i c4 1

11 11 Linear Search linearSearch(A,n,t) cost times i = 0 c1 1 while (i < n) and (A[i] != t) c2 1..n+1 i = i + 1 c3 0..n return i c4 1 Worst: t is not in A T(n) = c1 + c2(n+1) + c3(n) + c4 T(n) = (c2+c3)(n) + (c1+c2+c4) = an + blinear or Θ(n) Average: A[k] is t, where k = n/2 T(n) = c1 + c2(k+1) + c3(k) + c4 T(n) = (c2+c3)(k) + (c1+c2+c4) = cn + d T(n) = ((c2+c3)/2)(n) + (c1+c2+c4) = cn + dlinear or Θ(n)

12 12 Insertion Sort On each pass the next element is inserted into the portion of the array previously sorted The sort is “in place” The array is logically separated into two parts [..sorted..] [..unsorted..] The index j marks the location of the key and separates sorted from unsorted portions of the array

13 13 Example of Insertion Sort [ ] [ ] original array [ 6 ] [ ] prior to 1 st iteration [ 2 6 ] [ ] after 1 st iteration [ ] [ 4 3 ] after 2 nd iteration [ ] [ 3 ] after 3 rd iteration [ ] [ ] after last iteration

14 14 Insertion Sort Invariant: A[0..j-1] is the same collection of elements as in the previous iteration BUT they are now sorted On each iteration A[j] is the element that is inserted into A[0..j-1] producing a sorted A[0..j] A[j+1..n-1] remains unsorted

15 15 Insertion Sort Algorithm Sort n elements of A into non-decreasing order insertionSort(A,n) cost times for j = 1 to (n-1) c1 n key = A[j] c2 n-1 // insert A[j] into sorted sequence A[0..j-1] i = j – 1 c3 n-1 while (i >= 0) and (A[i] > key) c4 f A[i+1] = A[i] c5 g i = i – 1 c6 g A[i+1] = key c7 n-1 f = g =

16 16 Insertion Sort Analysis Best: already presorted t j is number of times the while loop checks limits and pairs of elements during shifting of elements in A[0..j-1] The loop terminates on first check since A[j] <= A[j+1] for all j T(n) = c1(n) + (c2+c3+c7)(n-1) + c4(f) + (c5+c6)(g) t j = 1, so f = = (n-1) and g = = 0 T(n) = (c1+c2+c3+c4+c7)n – (c2+c3+c4+c7) Linear Θ(n)

17 17 Insertion Sort Analysis Worst: presorted in reverse order Shifting loop has to compare and shift every element in A[0..j-1] because initial order ensures A[k] > A[j] for all k < j T(n) = c1(n) + (c2+c3+c7)(n-1) + c4(f) + (c5+c6)(g) t j = j, so f = = n(n-1)/2 and g = = (n-1)(n-2)/2 T(n) = c1(n) + (c2+c3+c7)(n-1) + c4(n(n-1)/2 ) + (c5+c6)((n-1)(n-2)/2) T(n) = an 2 + bn + c Quadratic Θ( n 2 )

18 18 Selection Sort On each pass the smallest element in the unsorted sub-array is selected for appending to the sorted sub-array The sort is “in place” The array is logically separated into two parts [..sorted..] [..unsorted..] The index k separates sorted from unsorted portions of the array

19 19 Example of Selection Sort [ ] [ ] original array [ 2 ] [ ] after 1 st iteration [ 2 3 ] [ ] after 2 nd iteration [ ] [ 5 6 ] after 3 rd iteration [ ] [ 6 ] after last iteration

20 20 Selection Sort Invariant: A[0..k-1] is sorted and all elements in A[0..k-1] are less than or equal to all elements in A[k..n-1] On each iteration A[smallIndex] is the element that is appended to A[0..k-1] producing a sorted A[0..k] A[k+1..n-1] remains unsorted

21 21 Selection Sort Algorithm Sort n elements of A into non-decreasing order selectionSort(A,n) cost times for k = 0 to n-2 c1 n // scan unsorted sublist to find smallest value smallIndex = k c2 n-1 for j = k+1 to n-1 c3 f if A[j] < A[smallIndex] c4 g smallIndex = j c5 h // swap smallest value with leftmost if smallIndex != k c6 n-1 swap(A[k],A[smallIndex]) c7 0..n-1 f = g = h =

22 22 Selection Sort Analysis Best: already presorted t k is 0, since the leftmost element of the unsorted sub-array is always the smallest element No swaps are performed, since the array is already in order T(n) = c1(n) + (c2+c6)(n-1) + c3(f) + c4(g) f = = n(n-1)/2 and g = = (n-1)(n-2)/2 T(n) = (c1+c2+c6)n – c6 + c3(n(n-1)/2) + c4((n-1)(n-2)/2) T(n) = an 2 + bn + c Quadratic Θ(n 2 )

23 23 Selection Sort Analysis Worst: presorted in reverse order Searching for smallest requires changing smallIndex on every comparison in A[0..j-1] because initially A[k] > A[j] for all k < j A swap occurs n-1 times T(n) = c1(n) + (c2+c6+c7)(n-1) + c3(f) + c4(g) + c5(h) t k = k, so f = = n(n-1)/2 and g = h = = (n-1)(n-2)/2 T(n) = c1(n) + (c2+c6+c7)(n-1) + c3(n(n-1)/2) + (c4+c5)((n-1)(n-2)/2 ) T(n) = an 2 + bn + c Quadratic Θ(n 2 )

24 24 Binary Search Algorithm Case 1: A match occurs. The search is complete and mid is the index that locates the target. if (midValue == target) // found match return mid;

25 25 Binary Search Algorithm Case 2: The value of target is less than midvalue and the search must continue in the lower sublist. if (target < midvalue) // search lower sublist { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/3320162/11/slides/slide_24.jpg", "name": "25 Binary Search Algorithm Case 2: The value of target is less than midvalue and the search must continue in the lower sublist.", "description": "if (target < midvalue) // search lower sublist

26 26 Binary Search Algorithm Case 3: The value of target is greater than midvalue and the search must continue in the upper sublist. if (target > midvalue)// search upper sublist

27 27 Successful Binary Search Search for target = 23 Step 1: Indices first = 0, last = 9, mid = (0+9)/2 = 4. Since target = 23 > midvalue = 12, step 2 searches the upper sublist with first = 5 and last = 9.

28 28 Successful Binary Search Step 2: Indices first = 5, last = 9, mid = (5+9)/2 = 7. Since target = 23 < midvalue = 33, step 3 searches the lower sublist with first = 5 and last = 7.

29 29 Successful Binary Search Step 3: Indices first = 5, last = 7, mid = (5+7)/2 = 6. Since target = midvalue = 23, a match is found at index mid = 6.

30 30 Unsuccessful Binary Search Search for target = 4. Step 1: Indices first = 0, last = 9, mid = (0+9)/2 = 4. Since target = 4 < midvalue = 12, step 2 searches the lower sublist with first = 0 and last = 4.

31 31 Unsuccessful Binary Search Step 2: Indices first = 0, last = 4, mid = (0+4)/2 = 2. Since target = 4 < midvalue = 5, step 3 searches the lower sublist with first = 0 and last 2.

32 32 Unsuccessful Binary Search Step 3: Indices first = 0, last = 2, mid = (0+2)/2 = 1. Since target = 4 > midvalue = 3, step 4 should search the upper sublist with first = 2 and last =2. However, since first >= last, the target is not in the list and we return index last = 9.

33 33 Binary Search Implementation int binSearch (const int arr[], int first, int last, int target) { int origLast = last; // original value of last while (first < last){ int mid = (first+last)/2; if (target == arr[mid]) return mid; // a match so return mid else if (target < arr[mid]) last = mid; // search lower sublist else first = mid+1; // search upper sublist } return origLast; // target not found }

34 34 Binary Search Analysis Each comparison divides the problem size in half Assume for simplicity that n = 2 k So, worst case … How many times do we divide n in half before there are no more values to check? T(n) = 1+ k = 1 + lg n Logarithmic Θ(lg n)


Download ppt "1 Algorithms & Pseudocode Orders of Magnitude Sequential Searching Finding Minimum value Linear Search Sorting Insertion Sort Selection Sort CSE 30331."

Similar presentations


Ads by Google