Topic Number 2 Efficiency – Complexity Algorithm Analysis


Significant Improvement – Algorithm with Smaller Big O function



Download 1,07 Mb.
bet5/8
Sana14.06.2022
Hajmi1,07 Mb.
#669861
1   2   3   4   5   6   7   8
Bog'liq
topic2Efficiency Complexity AlgorithmAnalysis (1)

Significant Improvement – Algorithm with Smaller Big O function

  • CS 314
  • Problem: Given an array of ints replace any element equal to 0 with the maximum positive value to the right of that element. (if no positive value to the right, leave unchanged.)
  • Given:
  • [0, 9, 0, 13, 0, 0, 7, 1, -1, 0, 1, 0]
  • Becomes:
  • [13, 9, 13, 13, 7, 7, 7, 1, -1, 1, 1, 0]

Replace Zeros – Typical Solution

  • public void replace0s(int[] data){ for(int i = 0; i < data.length; i++){
  • if (data[i] == 0) {
  • int max = 0;
  • for(int j = i+1; j
  • data[i] = max; }
  • }
  • }
  • Assume all values are zeros. (worst case)
  • Example of a dependent loops.
  • Clicker 10 - Number of times j < data.length evaluated?
  • O(1) B. O(N) C. O(NlogN)
  • D. O(N2) E. O(N!)

Replace Zeros – Alternate Solution

  • CS 314
  • Efficiency - Complexity
  • public void replace0s(int[] data){ int max = Math.max(0, data[data.length – 1]);
  • int start = data.length – 2;
  • for (int i = start; i >= 0; i--) {
  • if (data[i] == 0)
  • data[i] = max;
  • else
  • max = Math.max(max, data[i]);
  • }
  • }
  • Clicker 11 - Big O of this approach?
  • O(1) B. O(N) C. O(NlogN)
  • D. O(N2) E. O(N!)

Clicker 12

  • Is O(N) really that much faster than O(N2)?
  • never
  • always
  • typically
  • Depends on the actual functions and the value of N.
  • 1000N + 250 compared to N2 + 10
  • When do we use mechanized computation?
  • N = 100,000
  • 100,000,250 < 10,000,000,010 (108 < 1010)

Download 1,07 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish