search_algorithm 1.0.2 copy "search_algorithm: ^1.0.2" to clipboard
search_algorithm: ^1.0.2 copied to clipboard

A Dart package providing implementations of Binary Search, Linear Search, and Jump Search algorithms with performance measurement.

Search Algorithms #

A Dart package that provides implementations of various search algorithms, including Binary Search, Linear Search, and Jump Search. Each search function accepts a list of data (either String or int) and the data to search for, returning the index if the data is found, otherwise -1. The package also provides information about the time taken for each search.

Features #

  • Linear Search: A straightforward search algorithm that checks each element in the list until the desired element is found or the list ends.
  • Binary Search: A more efficient algorithm for sorted lists, dividing the search interval in half repeatedly.
  • Jump Search: An algorithm that searches in blocks of elements, reducing the time complexity for sorted lists.

Installation #

Add the following line to your pubspec.yaml dependencies section:

dependencies:
  search_algorithms: ^1.0.2

Usage #

import 'package:search_algorithms/search_algorithms.dart';

void main() {
  List<int> intList = List.generate(10000, (index) => index);
  List<String> stringList = List.generate(10000, (index) => 'Item$index');

  // Linear Search
  int index1 = SearchAlgorithms.linearSearch(intList, 5000);
  print('Linear Search Index: $index1');

  // Binary Search
  int index2 = SearchAlgorithms.binarySearch(intList, 5000);
  print('Binary Search Index: $index2');

  // Jump Search
  int index3 = SearchAlgorithms.jumpSearch(intList, 5000);
  print('Jump Search Index: $index3');

  // Measure time for Linear Search
  Duration duration1 = SearchAlgorithms.measureTime(() => SearchAlgorithms.linearSearch(intList, 5000));
  print('Linear Search time: ${duration1.inMicroseconds} µs');

  // Measure time for Binary Search
  Duration duration2 = SearchAlgorithms.measureTime(() => SearchAlgorithms.binarySearch(intList, 5000));
  print('Binary Search time: ${duration2.inMicroseconds} µs');

  // Measure time for Jump Search
  Duration duration3 = SearchAlgorithms.measureTime(() => SearchAlgorithms.jumpSearch(intList, 5000));
  print('Jump Search time: ${duration3.inMicroseconds} µs');
}

API #

int linearSearch

Parameters: #

list: The list to search. key: The element to search for. Returns: The index of the element if found, otherwise -1. int binarySearch

Parameters: #

list: The sorted list to search. key: The element to search for. Returns: The index of the element if found, otherwise -1. int jumpSearch

Parameters: #

list: The sorted list to search. key: The element to search for. Returns: The index of the element if found, otherwise -1. Duration measureTime(Function function) Measures the time taken to execute a function.

Parameters: #

function: The function to measure. Returns: The duration taken to execute the function.

1
likes
150
points
41
downloads

Publisher

unverified uploader

Weekly Downloads

A Dart package providing implementations of Binary Search, Linear Search, and Jump Search algorithms with performance measurement.

Homepage
Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on search_algorithm