wleci
AboutProjectsBlogContact
Contact
Back to blog
AlgorithmsC++Sorting

Stage 7: Basic Algorithms in C++ - Manual Implementation

A guide to copying, searching, sorting, and sequence generation algorithms without using the <algorithm> library.

February 11, 20263 min read
Share:

Basic Algorithms: The Foundation of Programming Logic

For your exam, you are prohibited from using ready-made solutions from the <algorithm> library (such as std::sort or std::copy). This means you must be able to implement these mechanisms yourself using loops and conditional statements.


1. Copying Arrays

Since arrays cannot be copied using the assignment operator (tab1 = tab2), we must iterate through each element individually.

cpp
void copyArray(int source[], int destination[], int size) { for (int i = 0; i < size; i++) { destination[i] = source[i]; } }

2. Linear Search

The simplest method to find an element in an unsorted array is linear search. The function should return the index of the found element or -1 if the element does not exist.

cpp
int findElement(int tab[], int size, int target) { for (int i = 0; i < size; i++) { if (tab[i] == target) return i; // Found! } return -1; // Not in the array }

3. Bubble Sort

This is the most frequently required algorithm in introductory exams due to its simplicity. It involves comparing adjacent elements and swapping them if they are in the wrong order.

cpp
void bubbleSort(int tab[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (tab[j] > tab[j + 1]) { // Swap int temp = tab[j]; tab[j] = tab[j + 1]; tab[j + 1] = temp; } } } }

4. Sequence Generation

Arithmetic Sequence

Each subsequent element differs by a constant value r.

cpp
void generateArithmetic(int tab[], int n, int start, int r) { for (int i = 0; i < n; i++) { tab[i] = start + i * r; } }

Random Sequence (using <cstdlib> and <ctime>)

Remember to initialize the random number generator using srand(time(NULL)) in the main function.

cpp
void generateRandom(int tab[], int n, int max_value) { for (int i = 0; i < n; i++) { tab[i] = rand() % (max_value + 1); } }

Key Exam Tips

  1. Complexity: Bubble sort has a complexity of O(n2)O(n^2)O(n2). For very large arrays (e.g., 100,000 elements), it will be very slow, but for exam purposes (arrays of 100-1000 elements), it is sufficient.
  2. Exit Condition: In linear search, always remember to return a value signaling an error (usually -1) if the loop finishes without finding a result.
  3. Loop Ranges: In bubble sort, the inner loop should end at n - i - 1 because after each full iteration, the largest element "bubbles up" to the end and no longer needs to be checked.

Control Task:

Try writing a function that generates a Fibonacci sequence into an array of size N. Remember: tab[i] = tab[i-1] + tab[i-2].

You might also like

MathC++

Stage 8: Basic Math Functions from the <cmath> Library

A detailed summary of the most important mathematical operations in C++ - an essential for every programming exam.

2 min read
ReferencesC++

Stage 5: References and References as Function Parameters

Understanding the reference mechanism in C++: how variable aliases work and why they are key to efficient argument passing.

3 min read
BasicsC++

Stage 1: Basic Data Types in C++ - A Comprehensive Guide

A detailed discussion of data types in C++, considering memory sizes and common exam mistakes.

2 min read
Back to blog
wleci.pl

Full-stack Developer

I build modern web applications with passion for clean code and good design.

[email protected]
Poland

Navigation

  • Home
  • About
  • Projects
  • Blog
  • Contact

Services

  • Web applications
  • Websites
  • API & Backend
  • Consulting

Technologies

  • React / Next.js
  • TypeScript
  • Node.js
  • PostgreSQL

Social

© 2026 wleci.pl. All rights reserved.

Privacy policy•Terms of service

Made with in Poland