Skip to content

Latest commit

 

History

History
87 lines (60 loc) · 3.05 KB

File metadata and controls

87 lines (60 loc) · 3.05 KB

My_algorithms — Sorting & Searching Algorithms

Part of Project_zero.

A header-only static library providing classic sorting and searching algorithms built on top of a shared Fields<T> base class. Every algorithm works on std::vector<T> internally and can be fed a raw array, any compatible container, or an rvalue vector. Synchronous and asynchronous (std::future-based) variants are available for every operation.


Features

Searching

  • Linear_search<T> — scans the entire array; returns all matching indices
  • Binary_search<T> — uses std::equal_range; array must be sorted; returns all matching indices

Sorting

  • Selection_sort<T>
  • Bubble_sort<T>
  • Insertion_sort<T>
  • Quick_sort<T> — in-place, pivot at midpoint

Async variants
Every algorithm exposes a *_async() method that returns a std::future, so you can kick off a sort or search on a background thread and retrieve the result later.

Display helpers (Search<T> and Sort<T>)
Concrete classes that combine all algorithms and provide console-interactive methods (lsearch_console(), qsort_console(), etc.) with built-in timing (measure_time) reported in microseconds.

Thread safety
Sorting methods take an exclusive lock (std::unique_lock) and search methods take a shared lock (std::shared_lock) on an internal std::shared_mutex, so concurrent reads are safe.


Setup & Linking

1. Include the header

#include "My_algorithms.h"

My_algorithms.h includes pch.h — make sure precompiled headers are configured in your project, or remove that include if you don't use them.

2. Link the static library

In Visual Studio:

  • Linker → Input → Additional Dependencies → add My_algorithms.lib
  • Linker → General → Additional Library Directories → add the path to x64/Release/

Quick Start

#include "My_algorithms.h"
#include <iostream>

int main() {
    int data[] = {5, 3, 8, 1, 9, 2, 7};

    // --- Sorting ---
    Sort<int> sorter(data, 7);
    sorter.qsort_console();      // sorts in-place and prints elapsed time
    // data[] is now updated via sync_func

    // --- Searching ---
    Search<int> searcher(data, 7);
    searcher.lsearch_console();  // prompts for a key, prints matching indices + time

    // --- Async sort ---
    Sort<int> async_sorter(data, 7);
    auto future = async_sorter.isort_async();
    future.get();                // wait for completion

    // --- From a vector ---
    std::vector<int> vec = {4, 6, 2, 8, 1};
    Sort<int> vec_sorter(vec);
    vec_sorter.bsort_console();
    // vec is updated in place after sort
}

Notes

  • Binary_search throws std::runtime_error if the array is not sorted when find() is called.
  • measure_time is a protected helper in Fields<T>; it is called internally by the *_console() methods and prints timing to stdout.
  • The sync_func callback (set up in the constructor) writes the sorted internal vector back to the original data source — raw array pointer or container reference — after each sort.