CodeIEB
Theory Notes/🗄️ Topic 4: Data & Information Management, Solution Development/11.4.8
11.4.8Grade 11

Array & String Manipulation Algorithms

The algorithmic core of the entire practical syllabus — search, sort, and string manipulation algorithms you must be able to code from memory, not just describe.

Array searching algorithms:

AlgorithmHow it worksRequirement
Sequential (linear) searchChecks each element one by one from the start until found or the array endsWorks on any array, sorted or not
Binary searchRepeatedly checks the middle element and eliminates half the remaining array each time based on whether the target is higher or lowerRequires the array to already be SORTED

Array sorting algorithms:

AlgorithmHow it works
Selection sortRepeatedly finds the smallest (or largest) remaining unsorted element and swaps it into its correct position
Improved selection sortSame idea, but avoids unnecessary swaps when the element is already in the correct position
Bubble sortRepeatedly compares adjacent elements and swaps them if out of order, 'bubbling' the largest values to the end each pass
Bubble sort with a flagAdds a Boolean flag that tracks whether any swap occurred in a pass — if no swaps happened, the array is already sorted and the algorithm can stop early (an execution efficiency improvement)

Example

// Bubble sort with a flag boolean swapped; do { swapped = false; for (int i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) { int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; swapped = true; } } } while (swapped);

Other essential array operations: inserting an element (shifting later elements to make room), deleting an element (shifting later elements to close the gap), and removing duplicates — for both simple data types and arrays of objects (comparing relevant object fields instead of the whole object).

String manipulation algorithms:

  • Counting words in a string — typically by counting spaces (or splitting on spaces) and adjusting for edge cases.
  • Isolating individual words in a string.
  • Removing vowels from a string.
  • Encoding/encrypting a string — see 11.2.8 for the security context (e.g. a simple substitution or shift cipher).
  • Changing a full name to initials + surname, e.g. "Fred John Smith" → "FJ Smith" (take the first letter of each first/middle name, keep the surname in full).
  • Validating input data such as an ID number, including calculating check digits (see 11.2.7, 11.4.14).

Example

// Converting "Fred John Smith" to "FJ Smith" String[] parts = fullName.split(" "); String initials = ""; for (int i = 0; i < parts.length - 1; i++) { initials += parts[i].charAt(0); } String result = initials + " " + parts[parts.length - 1];

A key exam skill: identifying patterns in duplicated code to recognise where a reusable method with parameters would be more efficient than repeating similar logic multiple times (linking back to 11.4.1 and 11.4.6).

💡 Exam Tip

Binary search is the single most common 'explain why this fails' trap: it ONLY works correctly on a sorted array — if asked to debug a broken binary search, always check whether the array was sorted first.