انتقل إلى المحتوى
أمين أبوزيد
BEG-039

الخوارزميات والتعقيد

  • 6 درس
  • 68 دقيقة قراءة
  • 3 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • Big O
  • التعقيد الزمني مقابل تعقيد المساحة
  • أفضل/متوسط/أسوأ الحالات

تعلّم أولًا

68 دقيقة قراءة · 3 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Express the time complexity of simple code in Big O (O(1), O(log n), O(n), O(n log n), O(n²)).
  2. Distinguish time and space complexity and best, average, and worst cases.
  3. Compare two solutions to the same problem by complexity and choose one.

الدروس

  1. 01لماذا نقيس النمو وليس الثواني12 د قراءة
  2. 02ترميز Big O11 د قراءة
  3. 03تحليل الحلقات والحلقات المتداخلة12 د قراءة
  4. 04تعقيد المساحة10 د قراءة
  5. 05الأفضل والمتوسط والأسوأ11 د قراءة
  6. 06عرض عملي: تحويل حل O(n²) إلى O(n)12 د قراءة

المختبر والتمارين

Lab

BEG-039-LAB1 · Analyze and improve

Outcomes: 1–3 · Time: ~3 h

  1. State the time complexity of eight provided snippets (constant, single loop, nested loop, halving loop, hidden contains, and so on) and write a one-line reason for each.
  2. Rewrite two O(n²) solutions (for example duplicate detection and "find a pair with a given sum") as O(n) using a Set or Map.
  3. Time old and new versions on three input sizes and record the table.

Acceptance: correct analysis; measured speed-up.

Exercises (assessment: complexity quiz of 10 snippets; six exercises)

IDOutcomeTask
BEG-039-EX011Give the Big O: a[5]; a single loop over n; two nested loops over n; a loop with i *= 2; a sort with Collections.sort.
BEG-039-EX021Simplify to Big O: 3n + 10, n² + n, 100, n log n + n.
BEG-039-EX032State the time and space complexity of reversing an array into a new array, and of reversing it in place.
BEG-039-EX042For linear search in an array, describe the best, average, and worst case and the input that causes each.
BEG-039-EX053Two solutions check whether a list has two equal items: nested loops, or a HashSet. Compare by time and space and choose one; say when you would choose the other.
BEG-039-EX061A loop over n calls list.remove(0) on an ArrayList each time. What is the total complexity and why?
أظهر الحلول
  • EX01 O(1); O(n); O(n²); O(log n); O(n log n).
  • EX02 O(n); O(n²); O(1); O(n log n).
  • EX03 New array: O(n) time, O(n) space. In place with two indexes: O(n) time, O(1) space.
  • EX04 Best: target is first, 1 step. Worst: target last or missing, n steps. Average: about n/2 steps, still O(n).
  • EX05 Nested loops: O(n²) time, O(1) space. HashSet: O(n) time, O(n) space. Choose the HashSet for large inputs; nested loops only if memory is very tight or n is tiny.
  • EX06 O(n²): remove(0) shifts all remaining items (O(n)), done n times.
  • Outcome 3 is also covered by the lab (rewrite and measure) and the quiz.

الاختبار

Outcomes: O1 express Big O · O2 time vs space, best/average/worst · O3 compare solutions.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1int x = arr[10]; is:O1
  2. 2A single for loop from 0 to n doing constant work is:O1
  3. 3Two nested loops, each over n:O1
  4. 4for (int i = 1; i < n; i *= 2) is:O1
  5. 53n² + 5n + 9 simplifies to:O1
  6. 6A loop over a List of n items calling list.contains(x) inside is:O1
  7. 7Extra memory that grows with the input is called:O2
  8. 8Linear search's best case is:O2
  9. 9Why quote the worst case?O2
  10. 10Finding duplicates in a big list: which is faster for large n?O3

الجزء العملي

Six exercises BEG-039-EX01 to EX06 plus the lab. The assessment is the 10-snippet complexity quiz (the ten questions above plus the lab snippets): pass at 8 of 10, and a lab with correct analysis and a measured speed-up.

أخطاء شائعة

Counting only the outer loop; ignoring hidden loops (list.contains).

التقييم

Complexity quiz (10 snippets).

الصلة بالمسار الوظيفي

Capacity planning (SRC-004) and interview questions at every level.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.