Searching and Sorting: Sequential Search and Bubble Sort – Grade 13 A/L ICT Notes 9.13

© 2026 schoolict.net (Ruwan Mangala Suraweera)

9.13 Searching and Sorting: Sequential Search and Bubble Sortසෙවීම සහ වර්ග කිරීම: අනුක්‍රමික සෙවීම සහ Bubble Sort

Grade 13 A/L ICT · Lesson 913 ශ්‍රේණිය A/L ICT · පාඩම 9

Prints all tabs in the language you are reading. Use “Save as PDF” in the print window to keep a copy.ඔබ කියවන භාෂාවෙන් සියලු tabs මුද්‍රණය වේ. පිටපතක් තියාගන්න print window එකේ “Save as PDF” තෝරන්න.
🎬Video coming soon in this language. Switch language to watch the other video.මේ භාෂාවෙන් වීඩියෝව ඉක්මනින් එනවා. අනෙක් වීඩියෝව බලන්න භාෂාව මාරු කරන්න.

In this videoමේ වීඩියෝවේ

  • Sequential search checks items in order.සෙවීම පිළිවෙළට පරීක්ෂා කරයි.
  • It returns the index or −1.index හෝ −1 දෙයි.
  • Bubble sort swaps neighbours in the wrong order.Bubble sort යාබද හුවමාරු කරයි.
  • Each pass puts the largest left value at the end.සෑම pass එකක්ම විශාලතම අගට.
  • A pass with no swaps ends the sort.හුවමාරු නැති pass නවත්වයි.
SearchingSequential searchTargetComparisonSortingBubble sortPass

Notesසටහන්

Syllabus 9.13. Sequential (linear) search checks items one by one from the start until the target is found or the list ends. Bubble sort compares neighbouring items and swaps them if they are in the wrong order; after each pass the largest remaining value “bubbles” to the end.විෂය නිර්දේශය 9.13. අනුක්‍රමික (රේඛීය) සෙවීම ඉලක්කය හමු වන තුරු හෝ ලැයිස්තුව අවසන් වන තුරු මුල සිට එකින් එක පරීක්ෂා කරයි. Bubble sort යාබද අයිතම සසඳා වැරදි පිළිවෙළ නම් හුවමාරු කරයි; සෑම pass එකකටම ඉතිරි විශාලතම අගය අගට “bubble” වේ.

1. Sequential search1. අනුක්‍රමික සෙවීම

🐍 Sequential searchඅනුක්‍රමික සෙවීම

def linear_search(items, target):
    for i in range(len(items)):
        if items[i] == target:
            return i
    return -1

data = [34, 7, 23, 32, 5, 62]
print(linear_search(data, 23))
print(linear_search(data, 99))

▶ Outputප්‍රතිදානය

2
-1
Targetඉලක්කයResult indexප්‍රතිඵල indexComparisonsසැසඳීම්
23232233
9999−1−166
🧮 Worked exampleවිසඳූ උදාහරණය How many comparisons to find 23 and 99 in [34, 7, 23, 32, 5, 62]?[34, 7, 23, 32, 5, 62] හි 23 සහ 99 සෙවීමට සැසඳීම් කීයද?
23 is checked against 34, 7, 23 – found at index 2 after 3 comparisons. 99 is compared with all 6 items and the function returns −1.23: 34, 7, 23 – index 2, සැසඳීම් 3. 99: අයිතම 6 සමඟම සසඳා −1 දෙයි.

2. Bubble sort2. Bubble sort

Bubble sort: largest value bubbles to the end each passStart51428Pass 114258Pass 212458Pass 312458Pass 3 makes no swap, so the algorithm stops early.
Pass 1 of bubble sortBubble sort හි pass 1

🐍 Bubble sort showing each passසෑම pass එකක්ම පෙන්වන bubble sort

def bubble_sort(a):
    n = len(a)
    for p in range(n - 1):
        swapped = False
        for j in range(n - 1 - p):
            if a[j] > a[j + 1]:
                a[j], a[j + 1] = a[j + 1], a[j]
                swapped = True
        print("Pass", p + 1, a)
        if not swapped:
            break
    return a

bubble_sort([5, 1, 4, 2, 8])

▶ Outputප්‍රතිදානය

Pass 1 [1, 4, 2, 5, 8]
Pass 2 [1, 2, 4, 5, 8]
Pass 3 [1, 2, 4, 5, 8]
PassPassList after the passpass එකට පසු ලැයිස්තුව
11[1, 4, 2, 5, 8][1, 4, 2, 5, 8]
22[1, 2, 4, 5, 8][1, 2, 4, 5, 8]
33[1, 2, 4, 5, 8][1, 2, 4, 5, 8]
🧮 Worked exampleවිසඳූ උදාහරණය Sort [5, 1, 4, 2, 8] with bubble sort.[5, 1, 4, 2, 8] bubble sort කරන්න.
Pass 1 swaps 5↔1, 5↔4, 5↔2 → [1, 4, 2, 5, 8]. Pass 2 swaps 4↔2 → [1, 2, 4, 5, 8]. Pass 3 makes no swap, so the swapped flag stops the sort early. Total swaps: 4.Pass 1: 5↔1, 5↔4, 5↔2 → [1, 4, 2, 5, 8]. Pass 2: 4↔2 → [1, 2, 4, 5, 8]. Pass 3 හුවමාරු නැත; නවතී. මුළු හුවමාරු 4.
💡 For n items bubble sort needs at most n − 1 passes; sequential search needs at most n comparisons.අයිතම n සඳහා bubble sort උපරිම pass n − 1; සෙවීම උපරිම සැසඳීම් n.
🚀 Beyond the syllabus (2026 update)🚀 විෂය නිර්දේශයෙන් ඔබ්බට (2026 යාවත්කාලීනය) Not required for the exam – read for interest.විභාගයට අවශ්‍ය නැත – දැනුම සඳහා කියවන්න. Python’s built-in sorted() uses Timsort, much faster than bubble sort on large data – but bubble sort is the classic for learning how sorting works.Python sorted() Timsort භාවිත කරයි; bubble sort ඉගෙනීමට සම්භාව්‍යයි.

Key Wordsප්‍රධාන පද

Searchingසෙවීම
Finding whether and where a value is in a list.අගයක් ලැයිස්තුවේ කොතැනද යන්න සොයා ගැනීම.
Sequential searchඅනුක්‍රමික සෙවීම
Checking each item in turn from the start.මුල සිට එකින් එක පරීක්ෂා කිරීම.
Targetඉලක්කය
Value being searched for.සොයන අගය.
Comparisonසැසඳීම
Checking one item against the target or a neighbour.අයිතමයක් ඉලක්කය හෝ අසල්වැසියා සමඟ පරීක්ෂාව.
Sortingවර්ග කිරීම
Arranging items in ascending or descending order.ආරෝහණ හෝ අවරෝහණ පිළිවෙළට සැකසීම.
Bubble sortBubble sort
Sort that swaps neighbouring items in the wrong order.වැරදි පිළිවෙළේ යාබද අයිතම හුවමාරු කරන sort.
PassPass
One full run through the list in bubble sort.bubble sort හි ලැයිස්තුව හරහා එක් සම්පූර්ණ ගමන.
Swapහුවමාරුව
Exchanging the positions of two items.අයිතම දෙකක ස්ථාන මාරු කිරීම.
Swapped flagSwapped flag
Boolean that stops the sort when a pass has no swaps.හුවමාරු නැති pass එකකදී නවත්වන Boolean.
Ascending orderආරෝහණ පිළිවෙළ
Smallest to largest.කුඩාම සිට විශාලතම.
Return −1−1 ආපසු
Signal that the target was not found.ඉලක්කය හමු නොවූ බවට සංඥාව.
Algorithm efficiencyඇල්ගොරිතම කාර්යක්ෂමතාව
How the work grows as the data grows.දත්ත වැඩි වන විට වැඩ වැඩි වන ආකාරය.

Sample Q&Aආදර්ශ ප්‍රශ්න සහ පිළිතුරු

1How does sequential search work?අනුක්‍රමික සෙවීම?

Answerපිළිතුර: Checks items one by one until found or the end.එකින් එක පරීක්ෂාව.

2Index of 23?23 index?

Answerපිළිතුර: 2.2.

3Comparisons for 99?99 සැසඳීම්?

Answerපිළිතුර: 6 (all items), returns −1.6, −1.

4List after pass 1?pass 1 පසු?

Answerපිළිතුර: [1, 4, 2, 5, 8].[1, 4, 2, 5, 8].

5Why does it stop after pass 3?pass 3 පසු ඇයි?

Answerපිළිතුර: No swaps, so swapped stays False.හුවමාරු නැත.

6Maximum passes for 5 items?අයිතම 5 උපරිම pass?

Answerපිළිතුර: 4 (n − 1).4.

MCQබහුවරණ ප්‍රශ්න

Choose the correct answer for each question.සෑම ප්‍රශ්නයකටම නිවැරදි පිළිතුර තෝරන්න.

Scoreලකුණු: 0 / 20

1Sequential search for 23 in [34, 7, 23, 32, 5, 62] returns23

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

Index from 0.0 සිට.

2Comparisons needed to find 23 are23

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

34, 7, 23.34, 7, 23.

3Searching for 99 returns99

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

Not found.හමු නොවේ.

4After pass 1, [5, 1, 4, 2, 8] becomespass 1

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

5 bubbles to its place.5 ගමන් කරයි.

5Total swaps sorting [5, 1, 4, 2, 8] areහුවමාරු

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

3 + 1.3 + 1.

6Bubble sort comparesසසඳයි

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

Adjacent.යාබද.

7The swapped flag lets bubble sortflag

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

Early exit.කලින් නවතී.

8The maximum passes for n items isඋපරිම

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

n − 1.n − 1.

9Which term means: “Finding whether and where a value is in a list”?“අගයක් ලැයිස්තුවේ කොතැනද යන්න සොයා ගැනීම” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

Searching – Finding whether and where a value is in a list.සෙවීම – අගයක් ලැයිස්තුවේ කොතැනද යන්න සොයා ගැනීම.

10Which term means: “Checking each item in turn from the start”?“මුල සිට එකින් එක පරීක්ෂා කිරීම” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

Sequential search – Checking each item in turn from the start.අනුක්‍රමික සෙවීම – මුල සිට එකින් එක පරීක්ෂා කිරීම.

11Which term means: “Value being searched for”?“සොයන අගය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

Target – Value being searched for.ඉලක්කය – සොයන අගය.

12Which term means: “Checking one item against the target or a neighbour”?“අයිතමයක් ඉලක්කය හෝ අසල්වැසියා සමඟ පරීක්ෂාව” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

Comparison – Checking one item against the target or a neighbour.සැසඳීම – අයිතමයක් ඉලක්කය හෝ අසල්වැසියා සමඟ පරීක්ෂාව.

13Which term means: “Arranging items in ascending or descending order”?“ආරෝහණ හෝ අවරෝහණ පිළිවෙළට සැකසීම” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

Sorting – Arranging items in ascending or descending order.වර්ග කිරීම – ආරෝහණ හෝ අවරෝහණ පිළිවෙළට සැකසීම.

14Which term means: “Sort that swaps neighbouring items in the wrong order”?“වැරදි පිළිවෙළේ යාබද අයිතම හුවමාරු කරන sort” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: B

Bubble sort – Sort that swaps neighbouring items in the wrong order.Bubble sort – වැරදි පිළිවෙළේ යාබද අයිතම හුවමාරු කරන sort.

15Which term means: “One full run through the list in bubble sort”?“bubble sort හි ලැයිස්තුව හරහා එක් සම්පූර්ණ ගමන” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: C

Pass – One full run through the list in bubble sort.Pass – bubble sort හි ලැයිස්තුව හරහා එක් සම්පූර්ණ ගමන.

16Which term means: “Exchanging the positions of two items”?“අයිතම දෙකක ස්ථාන මාරු කිරීම” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: A

Swap – Exchanging the positions of two items.හුවමාරුව – අයිතම දෙකක ස්ථාන මාරු කිරීම.

17Which term means: “Boolean that stops the sort when a pass has no swaps”?“හුවමාරු නැති pass එකකදී නවත්වන Boolean” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

Swapped flag – Boolean that stops the sort when a pass has no swaps.Swapped flag – හුවමාරු නැති pass එකකදී නවත්වන Boolean.

18Which term means: “Smallest to largest”?“කුඩාම සිට විශාලතම” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

Ascending order – Smallest to largest.ආරෝහණ පිළිවෙළ – කුඩාම සිට විශාලතම.

19Which term means: “Signal that the target was not found”?“ඉලක්කය හමු නොවූ බවට සංඥාව” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: E

Return −1 – Signal that the target was not found.−1 ආපසු – ඉලක්කය හමු නොවූ බවට සංඥාව.

20Which term means: “How the work grows as the data grows”?“දත්ත වැඩි වන විට වැඩ වැඩි වන ආකාරය” යන්නෙන් අදහස් වන පදය කුමක්ද?

✔ Correct!නිවැරදියි!✘ Not quite.වැරදියි. Correct answerනිවැරදි පිළිතුර: D

Algorithm efficiency – How the work grows as the data grows.ඇල්ගොරිතම කාර්යක්ෂමතාව – දත්ත වැඩි වන විට වැඩ වැඩි වන ආකාරය.

MCQ answer keyMCQ පිළිතුරු

  1. A – Index from 0.0 සිට.
  2. D – 34, 7, 23.34, 7, 23.
  3. C – Not found.හමු නොවේ.
  4. B – 5 bubbles to its place.5 ගමන් කරයි.
  5. A – 3 + 1.3 + 1.
  6. E – Adjacent.යාබද.
  7. D – Early exit.කලින් නවතී.
  8. C – n − 1.n − 1.
  9. E – Searching – Finding whether and where a value is in a list.සෙවීම – අගයක් ලැයිස්තුවේ කොතැනද යන්න සොයා ගැනීම.
  10. B – Sequential search – Checking each item in turn from the start.අනුක්‍රමික සෙවීම – මුල සිට එකින් එක පරීක්ෂා කිරීම.
  11. A – Target – Value being searched for.ඉලක්කය – සොයන අගය.
  12. C – Comparison – Checking one item against the target or a neighbour.සැසඳීම – අයිතමයක් ඉලක්කය හෝ අසල්වැසියා සමඟ පරීක්ෂාව.
  13. B – Sorting – Arranging items in ascending or descending order.වර්ග කිරීම – ආරෝහණ හෝ අවරෝහණ පිළිවෙළට සැකසීම.
  14. B – Bubble sort – Sort that swaps neighbouring items in the wrong order.Bubble sort – වැරදි පිළිවෙළේ යාබද අයිතම හුවමාරු කරන sort.
  15. C – Pass – One full run through the list in bubble sort.Pass – bubble sort හි ලැයිස්තුව හරහා එක් සම්පූර්ණ ගමන.
  16. A – Swap – Exchanging the positions of two items.හුවමාරුව – අයිතම දෙකක ස්ථාන මාරු කිරීම.
  17. E – Swapped flag – Boolean that stops the sort when a pass has no swaps.Swapped flag – හුවමාරු නැති pass එකකදී නවත්වන Boolean.
  18. D – Ascending order – Smallest to largest.ආරෝහණ පිළිවෙළ – කුඩාම සිට විශාලතම.
  19. E – Return −1 – Signal that the target was not found.−1 ආපසු – ඉලක්කය හමු නොවූ බවට සංඥාව.
  20. D – Algorithm efficiency – How the work grows as the data grows.ඇල්ගොරිතම කාර්යක්ෂමතාව – දත්ත වැඩි වන විට වැඩ වැඩි වන ආකාරය.

Structured Questionsව්‍යුහගත ප්‍රශ්න

1Write a Python function for sequential search.සෙවීමේ ශ්‍රිතය. (3 marksලකුණු 3)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
for i in range(len); if equal return i; return −1.for; if; return.

2Find 23 and 99 in [34, 7, 23, 32, 5, 62] with comparison counts.23 සහ 99. (2 marksලකුණු 2)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
23: index 2, 3 comparisons; 99: −1, 6 comparisons.2, 3; −1, 6.

3Show each pass of bubble sort on [5, 1, 4, 2, 8].pass පෙන්වන්න. (3 marksලකුණු 3)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
[1, 4, 2, 5, 8]; [1, 2, 4, 5, 8]; no swaps, stop.pass 3.

4Explain the purpose of the swapped flag.swapped flag. (3 marksලකුණු 3)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
Detects a pass with no swaps; ends early; saves time.කලින් නවතී.

5Compare the best and worst case of sequential search.හොඳම / නරකම. (2 marksලකුණු 2)

Show model answerආදර්ශ පිළිතුර පෙන්වන්න
Best 1 comparison (first item); worst n (last or missing).1; n.

🎮 Game🎮 ක්‍රීඩා

Pick a game. Each one uses words from this lesson.ක්‍රීඩාවක් තෝරන්න. හැම එකක්ම මේ පාඩමේ වචන පාවිච්චි කරනවා.

Summary & Downloadsසාරාංශය සහ බාගත කිරීම්

Key pointsප්‍රධාන කරුණු

  • Sequential search checks items in order.සෙවීම පිළිවෙළට පරීක්ෂා කරයි.
  • It returns the index or −1.index හෝ −1 දෙයි.
  • Bubble sort swaps neighbours in the wrong order.Bubble sort යාබද හුවමාරු කරයි.
  • Each pass puts the largest left value at the end.සෑම pass එකක්ම විශාලතම අගට.
  • A pass with no swaps ends the sort.හුවමාරු නැති pass නවත්වයි.

Downloadsබාගත කිරීම්

Full Lesson 9 PDFසම්පූර්ණ පාඩම 9 PDF: English · සිංහල

Next: Lesson 9 A/L Exam Practiceඊළඟට: පාඩම 9 A/L විභාග පුහුණුව →

© 2026 schoolict.net (Ruwan Mangala Suraweera). All rights reserved.සියලු හිමිකම් ඇවිරිණි.

Resource Book Lesson Note Download Questions Syllabus Sinhala LMS Simulator Tool