Giter VIP home page Giter VIP logo

ekalgorithms's Introduction

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:

ekalgorithms's People

Contributors

aceisscope avatar ahanmal avatar aspcartman avatar bitdeli-chef avatar chocochipset avatar cuongtv51 avatar evgenykarkan avatar kiwivandi avatar klmitchell2 avatar shrtlist avatar squarezw avatar stanislaw avatar vittoriom avatar xhzengaib avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ekalgorithms's Issues

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.

Array Shuffle

I would like to add the Fisher–Yates method to shuffle an array.

Cool?

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?

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.

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.

[Question]Some code style? function question?

Hi,

In EKGraph.m, I see code like this

NSMutableArray *parent = [@[] mutableCopy];
NSMutableDictionary *dist     = [@{} mutableCopy];

why not

NSMutableArray *parent = [NSMutableArray array];
NSMutableDictionary *dist     = [NSMutableDictionary dictionary];

Is this kind of code relative to special function or just a way of style?

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?

No visible interface for

Error:(33, 53) no visible @interface for 'NSNumber' declares the selector 'isLessThan:'
Error:(52, 26) no visible @interface for 'NSNumber' declares the selector 'isGreaterThan:'

etc. It seems podspec has some dependency missing. Strange enough, no invalid imports detected.

EDIT:
Well, NSComparisonMethods is only available on macosx. Well, I guess I make a fix and a pull request.

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.

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.

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]];

Incorrect isPrime Method

for (int i = 2; i <= (int)sqrt(givenNumber); i++) {
         if (givenNumber % i == 0) {
           return YES;
         }
     }

should be:

for (int i = 2; i <= (int)sqrt(givenNumber); i++) {
         if (givenNumber % i == 0) {
           return NO;
         }
     }

I'll open a PR soon.

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 😄 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

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.

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++)".

Use NSTreeController instead

I found AppKit framework have already implement tree structure, and I will rewrite tree related code if possible.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.