1 / 22

Finding Min & Max

Finding Min & Max. Given a collection Output Find the minimum. 7. 12. 5. 22. 3. 32. Find Min. Function find_min ( Arr Array, N Num ) m in_val  Arr [0] For ( i = 1; i < N; i ++) If ( min_val > Arr [ i ]) Then min_val = Arr [ i ]; Endif Endfor End find_min ;. 7.

ahmed-cain
Download Presentation

Finding Min & Max

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Finding Min & Max • Given a collection • Output • Find the minimum 7 12 5 22 3 32

  2. Find Min Function find_min(Arr Array, N Num) min_val Arr[0] For (i = 1; i < N; i++) If ( min_val > Arr[i]) Then min_val = Arr[i]; Endif Endfor End find_min; 7 12 5 22 3 32 7

  3. Find Min Function find_min(Arr Array, N Num) min_val Arr[0] For (i = 1; i < N; i++) If ( min_val > Arr[i]) Then min_val = Arr[i]; Endif Endfor End find_min; 7 12 5 22 3 32 7

  4. Find Min Function find_min(Arr Array, N Num) min_val Arr[0] For (i = 1; i < N; i++) If ( min_val > Arr[i]) Then min_val = Arr[i]; Endif Endfor End find_min; 7 12 5 22 3 32 7

  5. Find Min Function find_min(Arr Array, N Num) min_val Arr[0] For (i = 1; i < N; i++) If ( min_val > Arr[i]) Then min_val = Arr[i]; Endif Endfor End find_min; 7 12 5 22 3 32 5

  6. Find Min Function find_min(Arr Array, N Num) min_val Arr[0] For (i = 1; i < N; i++) If ( min_val > Arr[i]) Then min_val = Arr[i]; Endif Endfor End find_min; 7 12 5 22 3 32 5

  7. Find Min Function find_min(Arr Array, N Num) min_val Arr[0] For (i = 1; i < N; i++) If ( min_val > Arr[i]) Then min_val = Arr[i]; Endif Endfor End find_min; 7 12 5 22 3 32 5

  8. Find Min Function find_min(Arr Array, N Num) min_val Arr[0] For (i = 1; i < N; i++) If ( min_val > Arr[i]) Then min_val = Arr[i]; Endif Endfor End find_min; 7 12 5 22 3 32 3

  9. Find Min Function find_min(Arr Array, N Num) min_val Arr[0] For (i = 1; i < N; i++) If ( min_val > Arr[i]) Then min_val = Arr[i]; Endif Endfor End find_min; 7 12 5 22 3 32 3

  10. Find Min: Algorithm Analysis Function find_min(Arr Array, N Num) min_val Arr[0] For (i = 1; i < N; i++) If ( min_val > Arr[i]) Then min_val = Arr[i]; Endif Endfor End find_min; Loop executes N-1 times (N-1 comparisons)

  11. What About • Find the maximum • Same as find minimum • Needs N-1 comparisons • Find the minimum and maximum • Find the minimum  N-1 comparisons • Find the maximum N-1 comparisons • Total  2(N-1) comparisons Can we do better ??

  12. Find Min & Max: Naïve Way Function find_min(Arr Array, N Num) min_val Arr[0] max_val  Arr[0] For (i = 1; i < N; i++) If ( min_val > Arr[i]) Then min_val = Arr[i]; Endif If ( max_val < Arr[i]) Then max_val = Arr[i]; Endif Endfor End find_min; Loop N-1 times 2 comparisons

  13. Find Min & Max: Better Way • Step 1: Compare pairs together • Move the smaller value to SmallList • Move the larger value to LargeList 7 12 5 22 3 32 7 5 3 12 22 32 LargeList SmallList

  14. Find Min & Max: Better Way • Step 1: Compare pairs together • Move the smaller value to SmallList • Move the larger value to LargeList 7 12 5 22 3 32 So far N/2 comparisons 7 5 3 12 22 32 LargeList SmallList

  15. Find Min & Max: Better Way • Step 2: Get Min and Max • Get min from SmallList (using naïve way)  (N/2 -1) comparisons • Get max from LargeList (using naïve way)  (N/2 -1) comparisons • Total = 3 (N/2) - 2 comparisons 7 5 3 12 22 32 LargeList SmallList Max = 32 Min = 3

  16. Find Min & Max: Better Way • Step 2: Get Min and Max • Get min from SmallList (using naïve way)  (n/2 -1) comparisons • Get max from LargeList (using naïve way)  (n/2 -1) comparisons • Total:~= 3 (N/2) comparisons Wait !!!! There is a hidden extra cost What about Memory Usage? 7 5 3 12 22 32 LargeList SmallList Max = 32 Min = 3

  17. Analysis of the Two Algorithms • Naïve Way • (Time = CPU = Number of comparisons)  2(N-1) • (Memory = Storage)  2 extra spaces • Other way • (Time = CPU = Number of comparisons)  3(N/2) -2 • (Memory = Storage)  N extra spaces

  18. Ok…We can do better • Make the SmallList and LargeList only one item • When you add to either lists compare with the existing value and replace if needed • Now Storage  2 extra spaces only 7 12 5 22 3 32 SmallList LargeList

  19. Ok…We can do better • Make the SmallList and LargeList only one item • When you add to either lists compare with the existing value and replace if needed • Now Storage  2 extra spaces only 7 12 5 22 3 32 7 12 SmallList LargeList

  20. Ok…We can do better • Make the SmallList and LargeList only one item • When you add to either lists compare with the existing value and replace if needed • Now Storage  2 extra spaces only 7 12 5 22 3 32 5 22 SmallList LargeList

  21. Ok…We can do better • Make the SmallList and LargeList only one item • When you add to either lists compare with the existing value and replace if needed • Now Storage  2 extra spaces only 7 12 5 22 3 32 3 32 SmallList LargeList

  22. Analysis of the Two Algorithms • Naïve Way • (Time = CPU = Number of comparisons)  2(N-1) • (Memory = Storage)  2 extra spaces • Other way • (Time = CPU = Number of comparisons)  3(N/2)-2 • (Memory = Storage)  2 extra spaces

More Related