EKAlgorithms contains some well known CS algorithms & data structures.

Overview

Build Status

EKAlgorithms

EKAlgorithms is a set of computer exercises implemented in Objective-C. Data structures, well known algorithms, CS curiosities, you name it!

Don't forget to watch the repository; Its content will be expanded and updated frequently.

Arrays and Lists

  1. Index of maximum element in array.
  2. Indexes of maximum and minimum elements simultaneously.
  3. Find longest string in array of strings.
  4. Find shortest string in array of strings.
  5. Array reverse.
  6. Intersection of two arrays.
  7. Union of two arrays (with remove duplicates).
  8. Union of two arrays (with remove duplicates) for some key.
  9. Find duplicates.
  10. Array with N unique/not unique random objects.
  11. Check if array is sorted.
  12. Array shuffle (Fisher-Yates).
  13. Sum of array elements.
  14. N of occurences of each element in array.

Search Algorithms

  1. Linear search.
  2. Binary search.

Sorting Algorithms

  1. Bubble sort.
  2. Shell sort.
  3. Merge sort.
  4. Quick sort.
  5. Insertion sort.
  6. Selection sort.
  7. Radix Sort.
  8. Partial selection sort.
  9. Heap sort.

Selection Algorithms

  1. Quickselect.

Strings

  1. Palindrome or not.
  2. String reverse.
  3. Words count.
  4. Permutations of string.
  5. Occurrences of each character (a - z).
  6. Count "needles" in a "haystack".
  7. Random string.
  8. Concatenation of two strings.
  9. Find 1st occurrence of "needle" in a "haystack".
  10. Last occurrence of "needle" in a "haystack".
  11. Longest common subsequence.
  12. Levenshtein distance.
  13. KMP (Knuth–Morris–Pratt).
  14. Boyer–Moore string search algorithm.

Numeric Algorithms

  1. Sieve of Eratosthenes.
  2. Great common divisor (GCD).
  3. Least common multiple (LCM).
  4. Factorial.
  5. Fibonacci numbers (5 algos).
  6. Sum of digits.
  7. Binary to decimal conversion.
  8. Decimal to binary conversion.
  9. Fast exponentiation.
  10. Number reverse.
  11. Even/odd check.
  12. Leap year check.
  13. Armstrong number check.
  14. Prime number check.
  15. Find Nth prime.
  16. Swap the value of two NSInteger pointers.
  17. Square root using Newton-Raphson method.
  18. Convert integer to another numeral system (2, 8, 12, 16).
  19. Fast inverse square root.

Data Structures

  1. Stack (LIFO).
  2. Queue (FIFO).
  3. Deque.
  4. Linked list.
  5. Graph:
    • DFS (depth-first search);
    • BFS (breadth-first search);
    • MST (minimum spanning tree - Prim's algorithm);
    • MST (minimum spanning tree - Kruskal's algorithm);
    • Shortest path (Dijkstra's algorithm);
    • Topsort.
  6. Tree.
  7. Binary tree:
    • Pre-order traversal;
    • In-order traversal;
    • Post-order traversal.
  8. Binary search tree (BST).
  9. AVL tree.
  10. Binary heap.

Problems

  1. Josephus Problem.
  2. Modulo bias.

Geometry

  1. Array of sorted locations according to a distance to a given location.
  2. Cartesian quadrant selection algorithms

Recursion

  1. Tower of Hanoi.

Contributions

Pull requests are welcome! But if you want to do a contribution, open an issue first.

Originally, the compiled exercises are for educational purposes only and have no intention of being the ultimate solution complexity-wise, but they do intend to be used by you as a starting point of a deeper study on algorithms and their optimization.

Important Note

The Foundation framework already includes tools and methods for most of the exercises contained here. Kudos to Apple on that! But... this. is. SPARTA! So lets get our hands dirty and try to implement cool CS stuff with minimal use of existing APIs.

Thanks!

Similar repositories

algorithms playground for common questions (Ruby language)

Examples of commonly used data structures and algorithms in Swift. (Swift)

Algorithms and data structures in Swift, with explanations!

Contributors

Special thanks to these guys for their contributions to the project's development:

Comments
  • To create Unit Tests for the algorithms

    To create Unit Tests for the algorithms

    I'm quite a fan of unit testing (not necessarily TDD), and while refactoring methods of EKSearchStuff, EKSortStuff and EKArrayStuff I found myself in the position of having to check the console (for the output of the main file) to see if I broke something or not. I would like to create a unit tests suite (if you don't mind I would use Kiwi framework) to make it easier in the future to refactor things and also for new people to add new algorithms. Of course I won't be able to write the whole suite by myself, so I will need some help. What do you think?

    opened by vittoriom 22
  • Implement algorithms both using highest Cocoa API and our

    Implement algorithms both using highest Cocoa API and our "white-boxed" implementations.

    The changed proposed #14 is good but only partially - it is good to know that Apple's reverseObjectEnumerator does its job - and as of Apple recommends to "use highest API possible" - it would be a better choice than to implement it by hands, but I think it would still be great to have both variant of methods to be kept in EKAlgorithms.

    What I suggest is to keep both methods side by side, the only trick is again to invent a good convention to handle this:

    My initial thought was something like:

    + (NSArray *)reversedArrayWithArray:(NSArray *)arrayToReverse
    + (NSArray *)native_reversedArrayWithArray:(NSArray *)arrayToReverse
    

    or

    + (NSArray *)reversedArrayWithArray:(NSArray *)arrayToReverse
    + (NSArray *)CocoaImplementation_reversedArrayWithArray:(NSArray *)arrayToReverse
    

    But I personally dislike using underscores in method names that is why I also thought about something like

    + (NSArray *)reversedArrayWithArray:(NSArray *)arrayToReverse
    + (NSArray *)reversedArrayWithArrayImplementedByCocoa:(NSArray *)arrayToReverse
    

    For now I think that "CocoaImplementation_reversedArrayWithArray" is the best naming convention to be used for methods showing usage of highest-level of API suggested by Cocoa.

    Let me know what you think.


    Here are more examples we could write using Cocoa API:

    Index of maximum element: [array valueForKeyPath:@"@max.integerValue"] Binary search from awesome objc.io Issue n7, Binary Search.

    opened by stanislaw 6
  • Add a section about friendly/similar repositories?

    Add a section about friendly/similar repositories?

    Hello, @EvgenyKarkan.

    How about adding section with similar repositories? I think this growing one is a good candiate to start section.

    I haven't been doing anything on this algorithms topic during these latest months because of almost entire focus on patterns/architecture area so no contributions :smile: Anyway I like it a lot that we created and now have this basic base of algorithms just in case if a need in some of them raises! Good work!

    Thanks,

    Stanislaw

    opened by stanislaw 5
  • EKAlgorithmsExamples bug.

    EKAlgorithmsExamples bug.

    Please fix:

    EKAlgorithmsExamples crashes (XCode 5.1.1, OS 10.9): main.m, line 72, message: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSDictionary dictionaryWithObjectsAndKeys:]: second object of each pair must be non-nil. Or, did you forget to nil-terminate your parameter list?'

    [oneArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"EKAlgorithms100", nil]];
    

    should be:

    [oneArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"EKAlgorithms100", someKey, nil]];
    
    opened by sdrpa 5
  • To move most of the methods in appropriate categories

    To move most of the methods in appropriate categories

    After taking a look at the classes, I found that most of the methods could be simplified if moved to categories instead of their own class.

    Example: + (NSUInteger)indexOfMaximumElementInArray:(NSArray *)array; could become - (NSUInteger)indexOfMaximumElement;

    + (NSInteger)indexOfObjectViaLinearSearch:(id)object inArray:(NSArray *)arrayToSearch; could become - (NSInteger)indexOfObjectViaLinearSearch:(id)object;

    + (NSMutableArray *)bubbleSortedArrayWithUnsortedArray:(NSMutableArray *)unsortedArray; could become - (NSArray *)bubbleSort;

    + (BOOL)isGivenStringPalindrome:(NSString *)string; could become - (BOOL)isPalindrome;

    and so on.. Do you agree with this? If yes, I will move methods to categories so that their interface will be easier to use.

    opened by vittoriom 5
  • Is more efficient implementation of reversedArrayWithArray: possible?

    Is more efficient implementation of reversedArrayWithArray: possible?

    Following my previous issue #34:

    I think that your original implementation of reversedArrayWithArray can be improved by using the technique similar to what is used in #30 and #31:

    Only first n/2 elements should be traversed exchanging i and count-i on each step - the middle element (if it exists - stays one the same position - hm, nice!).

    Let me know, what do you think.

    opened by stanislaw 4
  • LCS and LD for NSString

    LCS and LD for NSString

    hi i once wrote longest common subsequence and Levenshtein distanc algorithm as an NSString category in this tiny reop but when i came across this repo of yours i feel that maybe i could add the two methods into your project. is it okay for me to make a pull request then?

    opened by aceisScope 4
  • iOS Support

    iOS Support

    Ported to iOS. Seems to work. #87

    iOS doesn't implement NSComparisonMethods protocol since the one was made for MacOSX's scripting support. iOS doesn't deal with scripts.

    Those methods, leaving scripting behind, are identical to the usual compare: and isEqual:.

    Also had to change a class of weight property of EKEdge from NSObject to NSNumber. Why NSObject in the first place?

    Made preprocessor's statements around imports in addition to ones in .h and .m files of iOS-only categories to protect them from IDE's import optimisations and suppression of warnings.

    opened by aspcartman 3
  • Thank you for your great collection.

    Thank you for your great collection.

    Hi, I'm new to programming, and I chose Objective-C as my first programming language. I've been search for a while but I couldn't find any book about Data Structure and Algorithms in Objective-C. So can you give me some source for learning these things.

    Thanks.

    opened by chatnever 3
  • EKAlgorithms is on Travis

    EKAlgorithms is on Travis

    All works fine now.

    There were problems with MapKit on OS X - it is supported only in 10.9, while Travis machines still have 10.8.5. I mocked all the geometry needed for now, so EKAlgorithms does not depend on any external frameworks: nor CoreLocation, nor MapKit.

    opened by stanislaw 3
  • Replace mutableCopy with [NSMutableArray arrayWithArray:] everywhere.

    Replace mutableCopy with [NSMutableArray arrayWithArray:] everywhere.

    Before today I wasn't aware that besides creating mutable array -[NSArray mutableCopy] also makes copy of each element.

    Now, having this in mind, I am pretty sure, that for general consistency of the whole algorithms base we need to eliminate all these mutableCopy strings and replace them with corresponding [NSMutableArray arrayWithArray:] strings.

    opened by stanislaw 3
  • To optimize each loop.

    To optimize each loop.

    Every single loop will be faster without any additional computations and methods call. E.g "[array count]" in loop such as "for (NSUinteger i = 0; i < [array count]; i++)" should be extracted to independent local variable NSUinteger counter = [array count]. So the loop will be faster & look like "for (NSUinteger i = 0; i < counter; i++)".

    opened by EvgenyKarkan 4
Owner
Evgeny Karkan
Evgeny Karkan
Algorithms and data structures in Swift, with explanations!

Welcome to the Swift Algorithm Club! Here you'll find implementations of popular algorithms and data structures in everyone's favorite new language Sw

raywenderlich 27.3k Jan 8, 2023
Commonly used data structures for Swift

Swift Collections is an open-source package of data structure implementations for the Swift programming language.

Apple 2.7k Jan 5, 2023
Simple implementations of various dynamic data structures in Swift.

SwiftDataStructures Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements Installatio

Hector Delgado 0 Oct 21, 2021
Mendel - Swift miliframework for implementing evolutionary/genetic algorithms

Mendel Mendel - Swift miliframework for implementing evolutionary/genetic algorithms. Or, you know, Gregor Mendel. This started out as an exercise in

saniul 139 Oct 26, 2022
Swivl - A set of BLAS-accelerated linerar algebra structures and functions

Swivl - Swift Vector Library A set of BLAS-accelerated linerar algebra structure

null 0 Jan 19, 2022
A Graph Data Structure in Pure Swift

SwiftGraph SwiftGraph is a pure Swift (no Cocoa) implementation of a graph data structure, appropriate for use on all platforms Swift supports (iOS, m

David Kopec 700 Dec 16, 2022
KeyPathKit is a library that provides the standard functions to manipulate data along with a call-syntax that relies on typed keypaths to make the call sites as short and clean as possible.

KeyPathKit Context Swift 4 has introduced a new type called KeyPath, with allows to access the properties of an object with a very nice syntax. For in

Vincent Pradeilles 406 Dec 25, 2022
The simplest abstraction to synchronize local data with remote source. For iOS, wirtten in swift.

Purpose The simplest abstraction to synchronize local data with remote source. For iOS, written in swift. Overview Many applications uses remote serve

Siarhei Ladzeika 7 Mar 17, 2022
Arena is an implementation of the generational arena data structure in Swift.

Arena This package is very much work in progress. Arena is an implementation of the generational arena data structure in Swift. An Arena is useful for

null 7 Dec 7, 2021
NFC Forum Well Known Type Data Parser for iOS11 and Core NFC

NFCNDEFParse NFC Forum Well Known Type Data Parser for iOS11 and Core NFC. Supports parsing of types: Text - NFCForum-TS-RTD_Text_1.0 2006-07-24 Uri -

Jari Kalinainen 14 Oct 21, 2022
Algorithms and data structures in Swift, with explanations!

Welcome to the Swift Algorithm Club! Here you'll find implementations of popular algorithms and data structures in everyone's favorite new language Sw

raywenderlich 27.2k Dec 30, 2022
Examples of commonly used data structures and algorithms in Swift.

Swift Structures This project provides a framework for commonly used data structures and algorithms written in a new iOS development language called S

Wayne Bishop 2.1k Dec 28, 2022
Algorithms and data structures in Swift, with explanations!

Welcome to the Swift Algorithm Club! Here you'll find implementations of popular algorithms and data structures in everyone's favorite new language Sw

raywenderlich 27.3k Jan 8, 2023
Artificial intelligence/machine learning data structures and Swift algorithms for future iOS development. bayes theorem, neural networks, and more AI.

Swift Brain The first neural network / machine learning library written in Swift. This is a project for AI algorithms in Swift for iOS and OS X develo

Vishal 331 Oct 14, 2022
List tree data souce to display hierachical data structures in lists-like way. It's UI agnostic, just like view-model and doesn't depend on UI framework

SwiftListTreeDataSource List tree data souce to display hierachical data structures in lists-like way. It's UI agnostic, just like view-model, so can

Dzmitry Antonenka 26 Nov 26, 2022
Mathias Köhnke 1.1k Dec 16, 2022
WKZombie is a Swift framework for iOS/OSX to navigate within websites and collect data without the need of User Interface or API, also known as Headless browser.

WKZombie WKZombie is an iOS/OSX web-browser without a graphical user interface. It was developed as an experiment in order to familiarize myself with

Mathias Köhnke 1.1k Dec 16, 2022
STDevRxExt contains some extension functions for RxSwift and RxCocoa which makes our live easy.

STDevRxExt Example To run the Example.playground, clone the repo, and run pod install from the Example directory first. Requirements iOS 9.0+ tvOS 9.0

STDev 6 Mar 26, 2021
AsyncTaskKit - contains some additions to async/await Task

AsyncTaskKit This repo contains some additions to async/await Task. In general i

Michał Zaborowski 0 Jan 2, 2022
LemonadeDeclarativeUI framework contains some new functions for UIKit

LemonadeDeclarativeUI framework contains some new functions for UIKit. This library has been developing. If you want to contribute reach me!

Özgür Elmaslı 0 Jan 2, 2022