Skip to content
Topic guide · Algorithms

Sorting Algorithms: Complexity, Traces and Which Sort to Use · সর্টিং অ্যালগরিদম: জটিলতা, ধাপে ধাপে ট্রেস ও কোন সর্ট কখন

Bubble, selection and insertion sort take O(n²) time in the average and worst case; merge sort and heap sort take O(n log n) in every case, and quick sort takes O(n log n) on average but O(n²) in the worst case (for example, already-sorted input with the first or last element as pivot). Merge, insertion, bubble, counting and radix sort are stable, while selection, quick and heap sort are not; merge sort needs O(n) extra memory, while heap sort sorts in place. No comparison sort can beat Ω(n log n) comparisons in the worst case, which is proved with a decision tree; counting and radix sort avoid this bound because they do not compare keys.

বাবল, সিলেকশন ও ইনসারশন সর্টের গড় ও সবচেয়ে খারাপ ক্ষেত্রে সময় লাগে O(n²); মার্জ সর্ট ও হিপ সর্টে সব ক্ষেত্রে O(n log n), আর কুইক সর্টে গড়ে O(n log n) কিন্তু সবচেয়ে খারাপ ক্ষেত্রে O(n²) (যেমন আগে থেকে সাজানো ইনপুটে প্রথম বা শেষ উপাদানকে পিভট নিলে)। মার্জ, ইনসারশন, বাবল, কাউন্টিং ও রেডিক্স সর্ট স্টেবল, কিন্তু সিলেকশন, কুইক ও হিপ সর্ট স্টেবল নয়; মার্জ সর্টে O(n) অতিরিক্ত মেমরি লাগে, আর হিপ সর্ট ইন-প্লেস। কোনো তুলনাভিত্তিক (কম্পারিজন) সর্ট সবচেয়ে খারাপ ক্ষেত্রে Ω(n log n)-এর চেয়ে কম তুলনায় সাজাতে পারে না, যা ডিসিশন ট্রি দিয়ে প্রমাণ করা হয়; কাউন্টিং ও রেডিক্স সর্ট কি তুলনা করে না বলে এই সীমার বাইরে।

Asked in past exams
60
Papers
51

1. What sorting is and the words exams use

Sorting puts items in order, usually ascending. Sorted data makes binary search possible (about 20 comparisons for 1 million items, since 2²⁰ ≈ 1.05 million), and makes duplicates and ranges easy to find. These properties classify every sort:

Property Meaning Examples
Comparison sort Decides order only by comparing two keys Bubble, selection, insertion, Shell, merge, quick, heap
Non-comparison sort Uses the key values or digits directly Counting, radix, bucket
Stable Equal keys keep their original order Bubble, insertion, merge, counting, radix
In-place Needs only O(1) extra memory (quick sort's O(log n) stack is usually accepted) Bubble, selection, insertion, heap, quick
Adaptive Runs faster on nearly sorted input Insertion, bubble with a "no swap" check

All traces below sort the same array, [5, 8, 3, 6, 2], into ascending order, so you can compare the algorithms directly.

2. Bubble sort

Compare each adjacent pair and swap it if it is out of order. After pass 1 the largest item has "bubbled" to the end; each later pass fixes one more item. Stop early if a pass makes no swap.

Worked example 1: how many swaps does bubble sort need for 5, 8, 3, 6, 2?

Pass Comparisons and swaps Array after the pass Swaps
1 (5,8) no; (8,3) swap; (8,6) swap; (8,2) swap [5, 3, 6, 2, 8] 3
2 (5,3) swap; (5,6) no; (6,2) swap [3, 5, 2, 6, 8] 2
3 (3,5) no; (5,2) swap [3, 2, 5, 6, 8] 1
4 (3,2) swap [2, 3, 5, 6, 8] 1

Total: 7 swaps and 4 + 3 + 2 + 1 = 10 comparisons in 4 passes; bubble sort never needs more than n − 1 passes. Shortcut: bubble sort makes exactly one swap per inversion (a pair out of order). The inversions here are (5,3), (5,2), (8,3), (8,6), (8,2), (3,2), (6,2): 7.

#include <stdio.h>

int main(void) {
    int a[] = {5, 8, 3, 6, 2};
    int n = sizeof a / sizeof a[0];
    int swaps = 0;

    for (int i = 0; i < n - 1; i++) {
        int swapped = 0;
        for (int j = 0; j < n - 1 - i; j++) {
            if (a[j] > a[j + 1]) {          /* out of order: swap */
                int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
                swaps++;
                swapped = 1;
            }
        }
        if (!swapped) break;                /* no swap: already sorted */
    }
    for (int i = 0; i < n; i++) printf("%d ", a[i]);
    printf("\nswaps = %d\n", swaps);
    return 0;
}

Output: 2 3 5 6 8 and swaps = 7. Comparisons in the worst case: (n − 1) + (n − 2) + … + 1 = n(n − 1)/2, so O(n²). With the early stop, an already sorted array needs one pass of n − 1 comparisons: best case O(n).

3. Selection sort

Find the minimum of the unsorted part and swap it to the front.

Pass Minimum of the unsorted part Array after the pass
1 2 (swap with 5) [2, 8, 3, 6, 5]
2 3 (swap with 8) [2, 3, 8, 6, 5]
3 5 (swap with 8) [2, 3, 5, 6, 8]
4 6 (already in place) [2, 3, 5, 6, 8]

Only 3 swaps (at most n − 1 ever), which helps when moving items is expensive. But it always scans the whole unsorted part, so it makes n(n − 1)/2 comparisons in every case: O(n²) best, average and worst. It is not stable: on [4ᵃ, 4ᵇ, 1], pass 1 swaps 1 with 4ᵃ, giving [1, 4ᵇ, 4ᵃ].

4. Insertion sort

Like sorting cards in your hand: take the next item (the key) and shift bigger items in the sorted left part one step right until the key fits.

Pass Key Array after inserting it
1 8 [5, 8, 3, 6, 2]
2 3 [3, 5, 8, 6, 2]
3 6 [3, 5, 6, 8, 2]
4 2 [2, 3, 5, 6, 8]

This took 7 shifts and 9 comparisons. Worst case (reverse order): n(n − 1)/2 comparisons, 10 for n = 5. Best case (already sorted): n − 1 comparisons, O(n). It is stable, in-place and adaptive, and the fastest simple sort for small or nearly sorted arrays.

Shell sort

Insertion sort moves an item only one place per shift. Shell sort (Donald Shell, 1959) first runs insertion sort on items a gap apart, then on smaller gaps, and ends with gap 1, which is ordinary insertion sort on an almost sorted array. Common gap sequences: Shell's n/2, n/4, …, 1 and Knuth's 1, 4, 13, 40, … (h = 3h + 1, used largest first).

Gap What happens Array after the pass
2 Sort positions 0, 2, 4 (5, 3, 2 → 2, 3, 5) and positions 1, 3 (8, 6 → 6, 8) [2, 6, 3, 8, 5]
1 Ordinary insertion sort: only 3 shifts, because 2 is already at the front [2, 3, 5, 6, 8]

Shell sort is in-place but not stable (gap 2 on [4ᵃ, 4ᵇ, 1] gives [1, 4ᵇ, 4ᵃ]). Its time depends on the gaps: worst case O(n²) with Shell's halving gaps, O(n√n) with Knuth's, and about O(n log n) at best on sorted input.

5. Merge sort (divide and conquer)

Split the array in half, sort each half recursively, then merge the two sorted halves by repeatedly taking the smaller front item. Merging two sorted lists of total length n takes at most n − 1 comparisons: O(n).

Merge sort splits [5, 8, 3, 6, 2] down to single items and merges them back in order: 5 and 8 give [5, 8], which with [3] gives [3, 5, 8]; 6 and 2 give [2, 6]; the last merge gives [2, 3, 5, 6, 8].

Where T(n) = 2T(n/2) + n comes from. Sorting n items means sorting two halves of n/2 items (2T(n/2)) and merging them (about n steps). Unrolling it, with n = 2ᵏ and T(1) = 0:

T(n) = 2T(n/2) + n = 4T(n/4) + 2n = 8T(n/8) + 3n = … = 2ᵏ·T(1) + k·n = n log₂ n.

There are log₂ n levels and each level does n work in total. Checked by computing the recurrence: T(8) = 24 = 8 × 3 and T(1024) = 10,240 = 1024 × 10. So merge sort is Θ(n log n) in every case, is stable, needs O(n) extra memory, and suits linked lists and external sorting (data larger than memory, such as 1 GB with 100 MB of RAM: sort chunks that fit, then merge them).

6. Quick sort and its O(n²) worst case

Choose a pivot, partition so smaller items go left and larger go right (the pivot lands in its final place), then quick sort each side. Average O(n log n) and usually the fastest in practice (in place, cache friendly); not stable.

Worked example 2: partition [5, 8, 3, 6, 2] with the first element as pivot.

  1. Pivot = 5. Index i moves right from 8 looking for an item > 5: stops at 8. Index j moves left from 2 looking for an item ≤ 5: stops at 2. Swap them: [5, 2, 3, 6, 8].
  2. i moves on: 3 ≤ 5, then stops at 6. j moves left: 8 and 6 are > 5, stops at 3. Now i has passed j, so stop.
  3. Swap the pivot with a[j] (the 3): [3, 2, 5, 6, 8]. 5 is in its final position, with [3, 2] on the left and [6, 8] on the right.
  4. Left part [3, 2], pivot 3 → [2, 3]. Right part [6, 8], pivot 6 → unchanged. Result [2, 3, 5, 6, 8].

Why the worst case is O(n²). Run the same code on the already sorted [2, 3, 5, 6, 8]: pivot 2 is the smallest, so the parts have sizes 0 and 4; then pivot 3 leaves 0 and 3, and so on. Each partition removes only the pivot, so the comparisons are 4 + 3 + 2 + 1 = 10 = n(n − 1)/2. In general T(n) = T(n − 1) + n = O(n²), with recursion depth n (so O(n) stack). Counted in a test: 1,000 already sorted numbers took 499,500 comparisons; 1,000 random numbers took about 11,000 on average (n log₂ n ≈ 9,966).

With balanced splits, T(n) = 2T(n/2) + n = O(n log n), the best and average case. A random or median-of-three pivot makes the worst case very unlikely but not impossible: randomized quick sort is still O(n²) in the worst case, with O(n log n) expected time.

Point Quick sort Merge sort
Method Divide and conquer; work done while partitioning Divide and conquer; work done while merging
Best / average O(n log n) O(n log n)
Worst O(n²) O(n log n)
Extra space O(log n) stack on average, O(n) worst O(n) array
Stable No Yes
Preferred for Arrays in memory (fast in practice) Guaranteed time, stability, linked lists, external sorting

7. Heap sort

Build a max-heap (every parent ≥ its children; stored in an array where the children of index i are 2i + 1 and 2i + 2). Then repeatedly swap the root (the maximum) with the last item of the heap, shrink the heap by one, and sift the new root down.

Step Heap part + sorted part
Build max-heap [8, 6, 3, 5, 2]
Move 8 to the end, sift down [6, 5, 3, 2] + [8]
Move 6 [5, 2, 3] + [6, 8]
Move 5 [3, 2] + [5, 6, 8]
Move 3 [2] + [3, 5, 6, 8]

Building the heap takes O(n); each of the n − 1 extractions takes O(log n), so heap sort is O(n log n) in every case with O(1) extra memory. It is not stable. With very little extra memory and many items, heap sort is the usual answer.

8. Counting sort and radix sort (non-comparison)

Counting sort (integer keys 0 … k): count each value, turn the counts into running totals (positions), then place items from right to left, which keeps it stable. For [4, 2, 2, 8, 3, 3, 1]: counts of 1, 2, 3, 4, 8 are 1, 2, 2, 1, 1; running totals 1, 3, 5, 6, 7; output [1, 2, 2, 3, 3, 4, 8]. Time and space O(n + k): excellent for a small range, useless for huge k.

Worked example 3: radix sort 608, 5, 768, 298, 576, 975, 90, 80. Sort by one digit at a time, least significant first, with a stable sort at each digit (005 has 0 tens and 0 hundreds).

Pass Digit Result
1 Units 90, 80, 5, 975, 576, 608, 768, 298
2 Tens 5, 608, 768, 975, 576, 80, 90, 298
3 Hundreds 5, 80, 90, 298, 576, 608, 768, 975

Time O(d(n + b)) for d digits in base b: linear for fixed-width keys. Stability at each pass is what keeps the earlier digit order.

Bucket sort spreads keys over k buckets by value range, sorts each bucket (usually with insertion sort) and joins the buckets in order: O(n + k) on average for uniformly distributed keys, O(n²) in the worst case when all keys land in one bucket.

9. The complexity table

Algorithm Best Average Worst Extra space Stable In-place
Bubble O(n) with early stop O(n²) O(n²) O(1) Yes Yes
Selection O(n²) O(n²) O(n²) O(1) No Yes
Insertion O(n) O(n²) O(n²) O(1) Yes Yes
Shell O(n log n) Depends on the gaps O(n²) Shell's gaps, O(n√n) Knuth's O(1) No Yes
Merge O(n log n) O(n log n) O(n log n) O(n) Yes No
Quick O(n log n) O(n log n) O(n²) O(log n) average No Yes
Heap O(n log n) O(n log n) O(n log n) O(1) No Yes
Counting O(n + k) O(n + k) O(n + k) O(n + k) Yes No
Radix O(d(n + b)) O(d(n + b)) O(d(n + b)) O(n + b) Yes No
Bucket O(n + k) O(n + k) uniform keys O(n²) O(n + k) Yes, with insertion sort in buckets No

10. Which sort for which case

Situation Best choice Why
Nearly sorted data, such as {1, 2, 3, 8, 15, 10} Insertion, or bubble with early stop O(n) when almost sorted: bubble needs 9 comparisons and 1 swap here
Small arrays (about 10–20 items) Insertion Low overhead; libraries use it inside merge and quick sort
General in-memory sorting, speed matters Quick sort Fastest on average
Guaranteed O(n log n) and stability Merge sort Same time in every case
Very little extra memory, guaranteed O(n log n) Heap sort In place, no O(n²) case
Linked list Merge sort Needs only sequential access, no extra array
Data bigger than RAM External merge sort Sorts chunks, then merges runs
Expensive moves (large records) Selection At most n − 1 swaps
Integers in a small range (marks 0–100) Counting O(n + k)
Fixed-length numbers or strings Radix Linear for fixed width
Time least dependent on input order Merge, selection (also heap) Same work on sorted, reversed and random input

Real libraries: Python's sorted() and list.sort() use Timsort (a merge–insertion hybrid, guaranteed stable); Java's Arrays.sort uses a dual-pivot quick sort for primitive arrays and a stable merge sort (Timsort) for objects; C's qsort() is not guaranteed to be stable (the C standard leaves the order of equal elements unspecified).

11. The Ω(n log n) lower bound for comparison sorts

Any comparison sort can be drawn as a decision tree: each inner node is a comparison, each leaf is one possible ordering of the input. To sort every input correctly, the tree needs at least n! leaves. A binary tree of height h has at most 2ʰ leaves, so 2ʰ ≥ n!, giving h ≥ log₂(n!) = Θ(n log n). The height is the worst-case number of comparisons, so every comparison sort needs Ω(n log n) comparisons in the worst case. Merge sort and heap sort are therefore asymptotically optimal.

A decision tree that sorts three items a, b, c needs 6 leaves, one per ordering, so its height is at least 3 comparisons.

Minimum worst-case comparisons by this bound: n = 3 → ⌈log₂ 6⌉ = 3; n = 5 → ⌈log₂ 120⌉ = 7; n = 10 → ⌈log₂ 3,628,800⌉ = 22. Counting and radix sort are faster than n log n without contradicting this, because they never compare two keys; they rely on keys being small integers or fixed-length digit strings.

12. Tricky exam cases

  • "Which sort uses divide and conquer?" Merge sort and quick sort (quick sort is not greedy and not dynamic programming).
  • "Which is fastest?" Quick sort on average among general sorts; for nearly sorted input, insertion (or bubble with early stop).
  • Insertion sort worst-case comparisons = n(n − 1)/2; bubble sort makes O(n²) comparisons.
  • Not comparison based: counting, radix, bucket. Method for proving sorting lower bounds: the decision tree.
  • Not stable in the typical form: selection, Shell, quick and heap sort.
  • Randomized quick sort has expected O(n log n) but worst case O(n²).
  • "Time taken by an NP-class (non-deterministic) sorting algorithm" is keyed O(n): it can guess each item's position and check the result in one pass.
  • Topological sort is not a sorting of values: it orders the vertices of a directed acyclic graph (DAG) so every edge u → v has u before v, in O(V + E). A graph with a cycle has no topological order.
  • Merging two sorted lists of sizes m and n takes O(m + n): each comparison outputs one item.

Practice · অনুশীলন

  1. How many swaps does bubble sort make to sort 4, 3, 2, 1 in ascending order?
    Answer
    1. Every pair is an inversion: (4,3), (4,2), (4,1), (3,2), (3,1), (2,1); bubble sort makes one swap per inversion. That is n(n − 1)/2 for n = 4, the worst case.
  2. Why is quick sort's worst case O(n²)? Give an input that causes it.
    Answer

    If the pivot is always the smallest or largest item, each partition removes only the pivot, so T(n) = T(n − 1) + n, giving n(n − 1)/2 comparisons = O(n²). Example: an already sorted array such as [1, 2, 3, 4, 5] with the first (or last) element as pivot.

  3. Derive the time complexity of merge sort from T(n) = 2T(n/2) + n.
    Answer

    Unroll: T(n) = 2ᵏT(n/2ᵏ) + kn. With n = 2ᵏ (k = log₂ n) and T(1) = 0, T(n) = n log₂ n, so merge sort is Θ(n log n) in the best, average and worst case.

  4. You must sort 1 million exam marks (integers 0–100) and keep students with equal marks in their original order. Which algorithm, and what is its time?
    Answer

    Counting sort: stable and O(n + k) with k = 101, so effectively linear. A comparison sort would need Ω(n log n).

  5. Name the algorithm: in-place, O(n log n) in the worst case, not stable.
    Answer

    Heap sort.

Asked in past exams · আগের পরীক্ষায় যেভাবে এসেছে

Our past papers have 60 questions on this topic (29 MCQ, 31 written) from 51 exams. Here are 12 of them; each links to its full paper.

  1. Why Quick sort worst complexity in O(n²)? Explain with example.

    Model answer on the paper page

  2. How many member of swapping is needed to sort the number sequence 5, 8, 3, 6, 2 in ascending order using bubble sort.

    Model answer on the paper page

  3. In which of the following graphs can we apply topological sort?

    • a)Undirected Cyclic graph
    • b)Directed Cyclic graph
    • c)Undirected Acyclic graph
    • d)Directed Acyclic graph (correct answer)
    Explanation · ব্যাখ্যা

    Correct answer: (d) Directed Acyclic graph

    A topological order lists the vertices so that for every directed edge u → v, u comes before v. Such an order exists if and only if the graph is a directed acyclic graph (DAG). If there were a cycle v₁ → v₂ → … → vₖ → v₁, then v₁ would have to come before itself, which is impossible. On a DAG, the order is found in O(V + E) time, either by reversing the DFS finishing order or with Kahn's in-degree method.

    Why the other options are wrong

    • (a) Undirected Cyclic graph: it has no edge directions to respect, and it contains a cycle.
    • (b) Directed Cyclic graph: any directed cycle makes a topological order impossible.
    • (c) Undirected Acyclic graph: an undirected forest has no "u before v" constraints, so topological sorting is not defined for it.

    Tip: Topological sort ⇔ DAG. Typical uses are task scheduling, build dependencies and course prerequisites.

    Source: MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 10: Depth-First Search

  4. Quick sort algorithm is an example of –

    • a)Greedy approach
    • b)Improved binary search
    • c)Dynamic programming
    • d)Divide and conquer (correct answer)
    Explanation · ব্যাখ্যা

    Correct answer: (d) Divide and conquer

    Quick sort is built on the three divide-and-conquer steps:

    1. Divide: pick a pivot and partition the array into elements smaller than the pivot and elements larger than it.
    2. Conquer: sort the two parts recursively.
    3. Combine: nothing left to do; the parts are already in place.

    Example: [5, 2, 8, 1, 9] with pivot 5 → [2, 1] 5 [8, 9] → sort each part → [1, 2, 5, 8, 9]. Average time O(n log n), worst case O(n²).

    Why the other options are wrong

    • (a) Greedy approach: a greedy algorithm commits to one locally best choice and never revisits it; quick sort's pivot is not a "best" choice, and both parts are solved recursively.
    • (b) Improved binary search: not a design paradigm. Binary search throws away one half, while quick sort must sort both partitions.
    • (c) Dynamic programming: stores and reuses answers of overlapping subproblems; quick sort's two partitions are disjoint, so nothing is reused.

    Tip: Quick sort and Merge sort are the two classic divide-and-conquer sorts.

    Source: divide and conquer — Dictionary of Algorithms and Data Structures (NIST)
    Source: quicksort — Dictionary of Algorithms and Data Structures (NIST)

  5. Which is correct for Merge sort-

    • a)Time complexity, O(n²)
    • b)Time complexity, O (n log n) (correct answer)
    • c)Time complexity, O (log n)
    • d)Not stable sort
    Explanation · ব্যাখ্যা

    Correct answer: (b) Time complexity, O(n log n)

    Merge sort splits the array into halves (log₂ n levels) and merges the sorted halves, which is linear work per level. Total: n × log₂ n, so O(n log n) in the best, average and worst cases.

    Why the other options are wrong

    • (a) Time complexity, O(n²): that is bubble/selection/insertion sort, or quicksort's worst case; merge sort never degrades to n².
    • (c) Time complexity, O(log n): a sort must at least read all n items, so it cannot run in O(log n).
    • (d) Not stable sort: standard merge sort is stable. When two keys are equal, the merge takes the one from the left half first, so equal items keep their original order. Java's library mergesort, for example, is documented as guaranteed stable.

    Tip: Merge sort: O(n log n) always, stable, O(n) extra space.

    Source: merge sort (NIST Dictionary of Algorithms and Data Structures); Collections (Java Platform SE 6 API, Oracle)

  6. কোন Algorithm-টি দ্রুত sorting করে?

    • a)Bubble sort
    • b)Selection sort
    • c)Quick sort (correct answer)
    • d)Insertion sort
    Explanation · ব্যাখ্যা

    সঠিক উত্তর: (c) Quick sort

    Quick sort একটি pivot বেছে নিয়ে array-কে pivot-এর চেয়ে ছোট ও বড়, এই দুই ভাগে partition করে, তারপর প্রতিটি ভাগকে আলাদাভাবে recursively sort করে। গড়ে (average case) এতে সময় লাগে O(n log n), আর আলাদা array ছাড়াই (in-place) কাজ করে বলে বাস্তবে এটি খুব দ্রুত চলে। বাকি তিনটি অপশনের average ও worst case দুটোই O(n²)। যেমন n = 10,000 হলে n² = 10⁸, কিন্তু n log₂ n ≈ 10,000 × 13.3 ≈ 1.33 × 10⁵, অর্থাৎ প্রায় ৭৫০ গুণ কম কাজ।

    অন্য অপশনগুলো কেন ভুল

    • (a) Bubble sort: পাশাপাশি দুটি element বারবার তুলনা করে দরকার হলে swap করে; average ও worst case-এ O(n²)।
    • (b) Selection sort: প্রতিটি pass-এ বাকি অংশ থেকে সবচেয়ে ছোট element খুঁজে বের করে; input যেমনই হোক, সবসময় n(n − 1)/2 টি তুলনা লাগে, অর্থাৎ O(n²)।
    • (d) Insertion sort: প্রায় sorted ডেটায় দ্রুত (O(n)), কিন্তু average ও worst case-এ O(n²)।

    পরীক্ষার নোট: এখানে "দ্রুত" বলতে average case বোঝানো হয়েছে। Worst case-এ (যেমন আগে থেকেই sorted array-তে প্রথম বা শেষ element-কে pivot নিলে) Quick sort-ও O(n²) হয়ে যায়, তবে সাধারণ ব্যবহারে এই চারটির মধ্যে এটিই সবচেয়ে দ্রুত।

    টিপ: O(n log n): Quick, Merge, Heap sort; O(n²): Bubble, Selection, Insertion sort।

    উৎস: quicksort — Dictionary of Algorithms and Data Structures (NIST)
    উৎস: Lecture 5: Linear Sorting, MIT 6.006 Introduction to Algorithms (Spring 2020), MIT OpenCourseWare
    উৎস: bubble sort — Dictionary of Algorithms and Data Structures (NIST)

  7. The complexity of Bubble sort algorithm is-

    • a)O(n)
    • b)O(log n)
    • c)O(n²) (correct answer)
    • d)O(n log n)
    Explanation · ব্যাখ্যা

    Correct answer: (c) O(n²)

    Bubble sort compares each adjacent pair and swaps it if it is out of order, repeating passes over the list. In the worst case (reverse-sorted input) pass 1 makes n−1 comparisons, pass 2 makes n−2, …, down to 1:
    (n−1) + (n−2) + … + 1 = n(n−1)/2 comparisons, e.g. 4,950 for n = 100. That is O(n²) for arbitrary data.

    Why the other options are wrong

    • (a) O(n): only the best case of the early-exit version on an already sorted list (n−1 comparisons, one pass); not the algorithm's complexity in general.
    • (b) O(log n): the cost of binary search; a sort must at least look at all n items.
    • (d) O(n log n): the cost of efficient sorts such as merge sort and heapsort, not bubble sort.

    Tip: Bubble, selection and insertion sort are O(n²); merge sort and heapsort are O(n log n).

    Source: bubble sort (NIST Dictionary of Algorithms and Data Structures)

  8. ____ sort uses a divide and conquer approach.

  9. ________ method is often used to prove lower bounds in sorting.

  10. In the worst case, the number of comparisons in the Insertion sort algorithm is:

  11. Which sorting algorithm below is not comparison based?

  12. Implementation of Insertion sort, Selection sort, Bubble sort.

Common mistakes · সাধারণ ভুল

  • Saying quick sort is always O(n log n): its worst case is O(n²), even with a random pivot.
  • Giving selection sort a best case of O(n): it always makes n(n − 1)/2 comparisons, so it is O(n²) in every case.
  • Calling merge sort in-place: the usual array version needs O(n) extra memory.
  • Giving bubble sort a best case of O(n) without the early-stop check: without it, every pass runs and it is O(n²).
  • Confusing stable with in-place: merge sort is stable but not in-place, heap sort is in-place but not stable.
  • Thinking counting or radix sort break the Ω(n log n) bound: the bound applies only to comparison sorts.

বাংলায় সংক্ষেপে

  • বাবল, সিলেকশন ও ইনসারশন সর্টের গড় ও সবচেয়ে খারাপ সময় O(n²); সিলেকশন সর্ট সব ক্ষেত্রেই O(n²)।
  • মার্জ সর্ট ও হিপ সর্ট সব ক্ষেত্রে O(n log n); কুইক সর্ট গড়ে O(n log n), সবচেয়ে খারাপ ক্ষেত্রে O(n²)।
  • মার্জ সর্ট ও কুইক সর্ট ডিভাইড অ্যান্ড কনকার পদ্ধতি; মার্জ সর্টের রিকারেন্স T(n) = 2T(n/2) + n, যার সমাধান n log n।
  • স্টেবল: বাবল, ইনসারশন, মার্জ, কাউন্টিং, রেডিক্স; স্টেবল নয়: সিলেকশন, কুইক, হিপ।
  • ৫, ৮, ৩, ৬, ২ সাজাতে বাবল সর্টে ৭টি সোয়াপ লাগে (ইনভার্সনের সংখ্যার সমান)।
  • প্রায় সাজানো ডেটায় ইনসারশন সর্ট, কম মেমরিতে হিপ সর্ট, লিংকড লিস্ট ও বড় এক্সটার্নাল ডেটায় মার্জ সর্ট উপযুক্ত।
  • তুলনাভিত্তিক সর্টের নিম্নসীমা Ω(n log n), যা ডিসিশন ট্রি দিয়ে প্রমাণিত; কাউন্টিং ও রেডিক্স সর্ট তুলনা করে না বলে লিনিয়ার সময়ে চলতে পারে।

Updated 2026-10-18. Written by ARBS Academy; the past exam questions are from public recruitment exams.