Unit 1 of 4 · B.Sc IT Sem 3

Unit 1: Introduction, searching and sorting

Data Structures notes · PTU syllabus (BSIT302/BSBC302)

3 min read5 topics10 exam questions
On this page
  1. Unit summary
  2. Data, data structures and their types
  3. Algorithm complexity and Big O notation
  4. Time–space trade-off
  5. Linear and binary search
  6. Bubble, insertion and selection sort
  7. Key terms
  8. Quick revision
  9. Important questions

Unit summary

Data structures organise data so that programs run efficiently. This unit covers the basic concept of data, problem analysis, algorithm complexity, Big O notation, the time–space trade-off, types of data structures, linear and binary search, and bubble, insertion and selection sort.

After this unit you can

  • Explain data, data structures and their types
  • Analyse algorithm complexity using Big O notation and the time–space trade-off
  • Implement linear and binary search
  • Implement bubble, insertion and selection sort

PTU syllabus topics

  • Basic concept of data
  • problem analysis
  • algorithm complexity
  • Big O notation
  • time-space trade-off
  • types of data structures (arrays, records, pointers, stack, queue, trees, linked list)
  • linear and binary search
  • bubble/insertion/selection sort
ComparisonSorting and searching complexity
Best case
Worst case

Linear search

O(1)

O(n)

Binary search (sorted)

O(1)

O(log n)

Bubble sort

O(n)

O(n²)

Insertion sort

O(n)

O(n²)

Selection sort

O(n²)

O(n²)

1

Topic 1

Data, data structures and their types

A data structure is a logical or mathematical model for organising data so it can be stored and processed efficiently.

ClassificationClassification of data structures
Data structures
  • Primitive

    int, float, char, pointer

  • Linear

    Array, linked list, stack, queue

  • Non-linear

    Tree, graph

  • Static vs dynamic

    Fixed size (array) vs grows at run time (linked list)

Common operations: traversing (visiting each element), searching, inserting, deleting, sorting and merging.

  • Problem analysis: understand inputs, outputs and constraints, choose a suitable data structure and algorithm, then estimate time and space requirements.
2

Topic 2

Algorithm complexity and Big O notation

The complexity of an algorithm is the amount of time (time complexity) and memory (space complexity) it needs as a function of the input size n.

NotationMeaningDescribes
Big-O, O(f(n))Upper boundWorst case
Omega, Ω(f(n))Lower boundBest case
Theta, Θ(f(n))Tight boundExact growth rate

Common growth rates from fastest to slowest: O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ).

Example

Linear search checks up to n elements, so it is O(n); binary search halves the range each step, so it is O(log n).

Time-space trade-off: we can often make a program faster by using more memory (for example a lookup table), or save memory at the cost of speed.

3

Topic 3

Time–space trade-off

  • Using more memory can reduce running time, and vice versa — e.g., a lookup table (hash table) answers queries in O(1) time using extra space; recomputing values saves space but takes more time; storing sorted copies speeds searching.
4

Topic 4

Linear and binary search

  • Linear search: check each element one by one until the key is found or the list ends. Works on unsorted data. Best O(1), worst O(n).
  • Binary search: works only on sorted data. Compare the key with the middle element; search the left or right half; repeat. Worst case O(log n).
cint binarySearch(int a[], int n, int key) {
    int low = 0, high = n - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (a[mid] == key) return mid;
        else if (key < a[mid]) high = mid - 1;
        else low = mid + 1;
    }
    return -1;
}
5

Topic 5

Bubble, insertion and selection sort

cvoid bubble(int a[], int n) {
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - 1 - i; j++)
            if (a[j] > a[j + 1]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; }
}
void insertion(int a[], int n) {
    for (int i = 1; i < n; i++) {
        int key = a[i], j = i - 1;
        while (j >= 0 && a[j] > key) { a[j + 1] = a[j]; j--; }
        a[j + 1] = key;
    }
}
void selection(int a[], int n) {
    for (int i = 0; i < n - 1; i++) {
        int min = i;
        for (int j = i + 1; j < n; j++) if (a[j] < a[min]) min = j;
        int t = a[i]; a[i] = a[min]; a[min] = t;
    }
}
ComparisonSimple sorting algorithms
Idea
Time complexity (best, worst)

Bubble sort

Repeatedly swap adjacent out-of-order pairs

O(n) with early exit, O(n²)

Insertion sort

Insert each element into the sorted part

O(n), O(n²) — good for nearly sorted data

Selection sort

Select the minimum and place it in position

O(n²), O(n²) — fewest swaps

Example

Insertion sort on 5, 2, 4, 1: after passes → 2 5 4 1 → 2 4 5 1 → 1 2 4 5.

Key terms

Data structure
Way of organising data for efficient use
Big O notation
Upper bound on growth of running time
Time–space trade-off
Exchanging memory for speed or vice versa
Binary search
Search halving a sorted list each step
Stable sort
Sort keeping equal elements in original order

Quick revision

  • Primitive vs non-primitive; linear vs non-linear; operations.
  • Big O, Ω, Θ; common orders O(1) to O(2ⁿ).
  • Time–space trade-off.
  • Linear O(n) vs binary O(log n) search.
  • Bubble, insertion, selection sort and complexities.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.Classify data structures.
  2. Q2.What is Big O notation?
  3. Q3.Explain the time–space trade-off.
  4. Q4.What is the precondition for binary search?
  5. Q5.Which simple sort is best for nearly sorted data?
  6. Q6.State the worst-case complexity of bubble sort.

Long-answer questions

  1. Q1.Explain data structures and their types.
  2. Q2.Explain algorithm complexity and asymptotic notations.
  3. Q3.Explain linear and binary search with algorithms.
  4. Q4.Explain bubble, insertion and selection sort with examples.

Stuck on this unit?

Message SBS on WhatsApp for help with Data Structures, or to ask about studying B.Sc IT at Synetic.

WhatsApp us