Pacific-Design.com

    
Home Index

1. Algorithms

+ Anagram

+ ArrayList

+ Binary Search

+ Bitwise

+ Find 1 Part Distance

+ Find Frequent Number

+ Find Smallest Difference

+ Group By Key

+ Hash Map

+ Hash Set

+ Hash Table

+ Largest Number

+ Linked Hash Map

+ Linked List

+ Mean Median Variance

+ Palindrome

+ Permutation

+ Permutation Hex

+ Pretty Map

+ Regex

+ Search Sorted Array

+ Smallest Difference

+ Stack

+ String Builder

+ Subsequences

+ Sum Of Two

+ Tree Map

Algorithms /

Big-O notation guarantees for the cost of insertion and deletion


Sorting Algorithms


1. Merge Sort: O(n log n)

Sort each pair of elements then, sort every four elements by merging every two pairs Then, sort every 8 elements, etc O(n log n) expected and worst case

2. Quick Sort: O(n log n) worst: O(n^2)

Pick a random element and partition the array, such that all numbers that are less than it come before all elements that are greater than it Then do that for each half, then each quar- ter, etc O(n log n) expected, O(n^2) worst case

3. Bucket Sort: O(n + m)

Partition the array into a finite number of buckets, and then sort each bucket individually. This gives a time of O(n + m), where n is the number of items and m is the number of distinct items

4. Selection Sort: O(n^2)

Find the smallest element using a linear scan and move it to the front Then, find the second smallest and move it, again doing a linear scan Continue doing this until all the elements are in place O(n^2)

5. Bubble Sort: O(n^2)

Start at the beginning of an array and swap the first two elements if the first is bigger than the second Go to the next pair, etc, continuously making sweeps of the array until sorted O(n^2)


Trees

Binary Search Tree: O(log n)

Binary search trees keep their keys in sorted order, so that lookup and other operations can use the principle of binary search
https://en.wikipedia.org/wiki/Binary_search_tree

Breadth First Search

Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the tree root (or some arbitrary node of a graph, sometimes referred to as a 'search key') and explores the neighbor nodes first, before moving to the next level neig
https://en.wikipedia.org/wiki/Breadth-first_search

ArrayList - dynamicaly resized

        ArrayList<String> array = new ArrayList<>();
        array.add("one");
        array.add("two");
        System.out.println(array.get(0));

        // Java 1.8 using lambda
        array.forEach(string -> {
            System.out.println(string);
        });

        for (String str : array) {
            System.out.println(str);
        }

Vector - forEach / synchornized

        Vector<String> vector = new Vector<>();
        vector.add("one");   vector.add("two");
        vector.add("three"); vector.add("four");
        System.out.println(vector.get(0));

        // Java 1.8 using lambda
        vector.forEach(string -> {
            System.out.println(string);
        });

        for ( String str : vector) {
            System.out.println(str);
        }

HashSet - forEach / lambda

        HashSet<String> hashSet = new HashSet();
        hashSet.add("A"); hashSet.add("B");
        hashSet.add("D"); hashSet.add("E");
        hashSet.add("C"); hashSet.add("F");

        // Java 1.8 with lambda
        final long[] integers = {0};
        hashSet.forEach(str -> {
            System.out.print(str + " = ");
            System.out.println(integers[0]++);
        });

        for (String str : hashSet) {
            System.out.println(str);
        }

HashMap - increment value for a key

        HashMap<String, Integer> map = new HashMap<>();
        map.put("one", 1);
        map.put("two", 2);

        if(map.containsKey("two")) {
            int tmp = map.get("two");
            map.put("two", tmp + 10);
        }

        for(Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(" Key   = " + entry.getKey()
                             + " Value = " + entry.getValue());
        }

LinkList - forEach / lambda

        LinkedList<String> list = new LinkedList<>();
        list.add("one");
        list.add("two");

        //--- using for loop ---//
        for(String str : list) {
            System.out.println(str);
        }

        //--- using lambda ---//
        final Integer[] integers = {0};
        final String[] strings = {"Kevin","Thomas"};

        list.forEach(string -> {
            System.out.println(string);
            integers[0]++;
            strings[0] = "Agoura";
        });


        //--- using iterator ---//
        Iterator iterator = list.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }


Collection Framework

A collection is just a group of objects. Collection can be of different types. Collection of Strings, numbers etc.


                 +-----------------------+
                 |       Interfaces      |
                 +-----------------------+
                   /                   \                
        +------------+              +------------+    +-------------+
        |    List    |              |    Set     |    |    Queue    |
        | (Ordered)  |              | (No Order) |    |    Deque    |
        +------------+              +------------+    +-------------+
         /         \                      |
        /           \                     V
+-----------+  +------------+       +------------+       
| ArrayList |  | LinkedList |       | Sorted Set | 
+-----------+  +------------+       +------------+       


Map represent a dictionary or hash table kind of data structure where each record has key and value. Each key is unique in the map. We look for a record using a key, and this search is constant time, and search is very fast.
                +--------------+
                |     Map      |
                +--------------+
                | Key  | Value | 
                +--------------+
                       |
                       V
                +--------------+
                |  Sorted Map  |
                +--------------+


http://hilite.me


The C++ Programming Language by Bjarne Stroustrup
cplusplus.com - General information about the C++ programming
astyle - Indentation and reformatting filters for C, C++
Cracking the Coding Interview