MCS-211 · Block 1 Unit 1

Basics of an Algorithm and its Properties

Section 7 of 9

Block 1 · Unit 1Basics of an Algorithm and its Properties11 min · Section 7 of 9

1.7Types of Computational Problems

Covers IGNOU MCS-211 §1.6

Although there are endless computing problems, most of them belong to a few well-studied families. If you can recognise the family, you can often reuse a known algorithm instead of inventing one. This page describes the standard problem types.

Sorting

Sorting means rearranging items into order (ascending or descending) by a key, such as sorting numbers by value or names alphabetically. Sorting is everywhere: search results, leaderboards, and as a step that makes other problems (such as binary search or finding duplicates) easier.

No single sorting algorithm is best in every situation: some are simple but slow, some are faster but complex; some do well on random data, others on nearly sorted data; some work in main memory, others suit huge files on disk.

Two properties of sorting algorithms deserve special attention:

Python · stable_sort.py
# Students already in alphabetical order; now sort them by marks.
students = [("Asha", 3), ("Ravi", 3), ("Neha", 1)]

def selection_sort(items):
    a = list(items)
    for i in range(len(a) - 1):
        m = i
        for j in range(i + 1, len(a)):
            if a[j][1] < a[m][1]:
                m = j
        a[i], a[m] = a[m], a[i]          # long-distance swap
    return a

def insertion_sort(items):
    a = list(items)
    for i in range(1, len(a)):
        key = a[i]
        j = i - 1
        while j >= 0 and a[j][1] > key[1]:   # strict '>' keeps equal keys in order
            a[j + 1] = a[j]
            j -= 1
        a[j + 1] = key
    return a

print("selection sort:", selection_sort(students))
print("insertion sort:", insertion_sort(students))
Output
selection sort: [('Neha', 1), ('Ravi', 3), ('Asha', 3)]
insertion sort: [('Neha', 1), ('Asha', 3), ('Ravi', 3)]

Selection sort swapped Neha with Asha across the array, jumping Asha behind Ravi: it is not stable. Insertion sort only moves items past larger ones, so Asha stays before Ravi: it is stable. Both are in-place.

Searching

Searching means finding a given value, the search key, in a collection. Methods range from linear search to binary search (sorted data) to hashing and search trees. The best choice depends on whether the data is static or changes often (insertions and deletions), and on the memory available.

String processing

A string is a sequence of characters (text, DNA bases, bits). The best-known string problem is string matching: finding where a pattern occurs in a text, as in a search box, a plagiarism checker or a virus scanner. This course studies string matching algorithms (naive, Rabin–Karp, KMP) in Block 3.

Graph problems

A graph is a set of vertices (points) joined by edges (lines). Road maps, computer networks, social networks and task dependencies are all graphs. Typical problems:

  • Traversal — visit every vertex (e.g. broadcasting a message in a network).
  • Shortest path — the cheapest route between two vertices (e.g. routing, maps).
  • Travelling salesman problem — the shortest tour visiting every vertex once.
  • Graph colouring — colour the vertices with as few colours as possible so that adjacent vertices differ. Example: exam scheduling, where vertices are exams, an edge joins two exams with a common student, and colours are time slots.

Traversal and shortest paths have efficient algorithms; TSP and minimum graph colouring have no known efficient algorithm.

Combinatorial problems

A combinatorial problem asks for a permutation, combination or subset that satisfies some conditions and is often the best one (maximum or minimum). TSP, graph colouring and independent set are examples. They are hard because the number of candidate objects grows extremely fast with the input size (as in 2n2^n and n!n!), and for most of them no polynomial-time algorithm is known — most computer scientists believe none exists. Shortest path is a fortunate exception: it looks combinatorial but has fast algorithms.

Geometric problems

Geometric algorithms work with points, lines and polygons, and are used in computer graphics, robotics, maps and medical imaging (tomography). Two classic problems:

  • Closest pair — given nn points, find the two nearest to each other.
  • Convex hull — find the smallest convex polygon containing all the points (like a rubber band stretched around nails).

Numerical problems

Numerical problems involve continuous mathematics: solving systems of linear equations, computing definite integrals, solving differential equations, evaluating functions and statistics. Many can only be solved approximately, because real numbers are stored with limited precision. The main danger is round-off error: small errors in each step can accumulate over many iterations and spoil the result.

Key takeaways

  • Standard types: sorting, searching, string processing, graph, combinatorial, geometric, numerical.
  • Comparison sorts need Ω(nlog⁡n)\Omega(n \log n) comparisons; non-comparison sorts can be linear for suitable keys.
  • Stable = equal keys keep their order; in-place = only constant extra memory.
  • Combinatorial problems explode in size (2n2^n, n!n!) and usually have no known efficient algorithm.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

A sorting algorithm keeps students with equal marks in their original alphabetical order. It is:

Show answer

Answer: A. stable

Keeping the relative order of equal keys is the definition of stability. In-place is about extra memory, a different property.

Question 2 · multiple choice

Which problem type does “find the smallest polygon enclosing all given points” belong to?

Show answer

Answer: D. Geometric

This is the convex hull problem, a classic geometric problem. It involves numbers, but its objects are points and polygons.

Question 3 · multiple choice

How many pairs does brute-force closest pair check for 10 points?

Show answer

Answer: B. 45

C(10, 2) = 10 × 9 / 2 = 45. 90 counts every pair twice (in both orders).

Question 4 · multiple choice

Which statement about sorting is correct?

Show answer

Answer: C. Comparison-based sorts need Ω(nlog⁡n)\Omega(n \log n) comparisons in the worst case.

The lower bound applies only to comparison sorts; counting and radix sort can be linear for suitable keys. Selection sort is not stable, as the program shows.

Question 5 · multiple choice

Scheduling exams so that no student has two exams at the same time is naturally modelled as:

Show answer

Answer: C. graph colouring

Exams are vertices, a shared student is an edge, and time slots are colours; adjacent vertices must get different colours.

Question 6 · short answer

Run selection sort on [(P, 2), (Q, 2), (R, 1)] (sort by the number) and state whether the result shows stability.

Show worked answer

Pass 1: the minimum key is R(1) at position 3; swap with position 1 → [(R,1), (Q,2), (P,2)]. Pass 2: among positions 2–3, Q(2) and P(2) are equal, so no smaller key is found and nothing changes. Result: [(R,1), (Q,2), (P,2)]. P came before Q in the input but after it in the output, so this run shows that selection sort is not stable.

↑ ↓ move↵ openTab switch type