├── README.md ├── algorithms.html ├── big-O-notation.html ├── css ├── style.css └── tables-styles.css ├── data-structures.html ├── images ├── graphs │ ├── comparison.svg │ ├── constant.svg │ ├── linear.svg │ ├── linearithmic.svg │ ├── log.svg │ └── quadratic.svg ├── icons │ ├── FB-share.svg │ ├── algo-icon.svg │ ├── big-o-icon.svg │ ├── cheat-sheets.svg │ ├── data-structures-icon.svg │ ├── favicon.ico │ ├── github.svg │ ├── google-plus-share.svg │ ├── intro-icon.svg │ ├── sort-table.gif │ ├── title-icon.png │ └── twitter-share.svg └── logo-algos.svg ├── index.html ├── intro.html └── js ├── jquery-1.11.2.min.js ├── jquery.tablesorter.js └── script.js /README.md: -------------------------------------------------------------------------------- 1 | live demo: http://cooervo.github.io/Algorithms-DataStructures-BigONotation/index.html 2 | 3 | # Algorithms-DataStructures-BigONotation 4 | 5 | Is simple website I made as a fun project to help me understand better: algorithms, data structures and big O notation. And also to have some practice in: Java, JavaScript, CSS, HTML and Responsive Web Design(RWD). If you discover some errors in the code or typos I haven't noticed please just let me know or feel free to contribute with Github, fork the project and make a pull request :) 6 | 7 | Sections are divided into: 8 | 9 | - Big o cheat sheets 10 | - intro 11 | - big o notation 12 | - data structures 13 | - algorithms 14 | 15 | ### Technologies or guidelines 16 | This website uses the following technologies to work properly: 17 | 18 | * JS & jQuery for interactivity 19 | * jQuery tablesorte unoficial fork http://mottie.github.io/tablesorter/docs/ 20 | * CSS + HTML for structure and styling 21 | * Responsive Web Design (RWD) to adjust to desktop and smartphones 22 | * Git for VCS 23 | 24 | 25 | -------------------------------------------------------------------------------- /algorithms.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | Algorithms 12 | 13 | 14 | 15 | 16 | 17 | 18 | 79 | 80 | 81 | 82 | 83 | 84 |
85 | 86 | 87 |
88 |

SEARCHING AND SORTING

89 | 90 |

In this section we are going to review searching and 91 | sorting algorithms.

92 |

Why is sorting so important

93 | 105 | 106 |

Considerations:

107 | 120 |

Java uses Comparable interface for sorting and returns: 121 | +1 if compared object is greater, -1 if compared object is 122 | less and 0 if compared objects are equal.

123 |

Sorting becomes more ubiquitous when we think on all the 124 | things we do daily that are previously sorted for us to 125 | understand and have better access to them:

126 | 137 | 138 |

Searching

139 |

There are two types of searching algorithms: Those that 140 | need a previously ordered data structure in order to work 141 | properly, and those that don’t need an ordered list.

142 |

Searching is also very important for many computing 143 | applications: searching through a search engine, finding a 144 | bank account balance for some client, searching in a large 145 | data set for a particular value, searching in your 146 | directories for some needed file. Many applications rely on 147 | effective search, if your application is complete but takes 148 | long too perform a search and retrieve data it will be 149 | discarded as useless.

150 |
151 |
152 | 153 |
154 |

SELECTION SORT

155 | 156 |

It is called selection sort because it repeatedly 157 | selects the smallest remaining item:

158 |
    159 |
  1. Find the smallest element. Swap it with the first 160 | element.
  2. 161 |
  3. Find the second smallest element. Swap it with the 162 | second element
  4. 163 |
  5. Find the third smallest element. Swap it with the 164 | third element
  6. 165 |
  7. Repeat finding the smallest element and swapping in 166 | the correct position until the list is sorted
  8. 167 |
168 | 169 |

Implementation in java for selection sort:

170 |
171 |
172 | 					
173 |     public class SelectionSort {
174 |     
175 |        public static void selectionSort(Comparable[] array) {
176 |       
177 |         for (int i = 0; i < array.length; i++) {
178 |          int min = i;
179 |          for (int j = i + 1; j < array.length; j++) {
180 |           if (isLess(array[j], array[min])) {
181 |            min = j;
182 |           }
183 |          }
184 |          swap(array, i, min);
185 |         }
186 |        }
187 |        
188 |        //returns true if Comparable j is less than min
189 |        private static boolean isLess(Comparable j, Comparable min) {
190 |         int comparison = j.compareTo(min);
191 |         return  comparison< 0;
192 |        }
193 |       
194 |        private static void swap(Comparable[] array, int i, int j) {
195 |         Comparable temp = array[i];
196 |         array[i] = array[j];
197 |         array[j] = temp;
198 |        }
199 |       
200 |        public static <E> void printArray(E[] array) {
201 |         for (int i = 0; i < array.length; i++) {
202 |          System.out.print(array[i] + " ");
203 |         }
204 |        }
205 |        
206 |        // Check if array is sorted
207 |        public static boolean isSorted(Comparable[] array) {
208 |         for (int i = 1; i < array.length; i++) {
209 |          if (isLess(array[i], array[i - 1])) {
210 |           return false;
211 |          }
212 |         }
213 |         return true;
214 |        }
215 |        
216 |         public static void main(String[] args) {
217 |       	  Integer[] intArray = { 34, 17, 23, 35, 45, 9, 1 };
218 |       	  System.out.println("Unsorted Array: ");
219 |       	  printArray(intArray);
220 |       	  System.out.println("\nIs intArray sorted? "
221 |             + isSorted(intArray));
222 |       	
223 |       	  selectionSort(intArray);
224 |       	  System.out.println("\nSelection sort:");
225 |       	  printArray(intArray);
226 |       	  System.out.println("\nIs intArray sorted? "
227 |             + isSorted(intArray));
228 |       	
229 |       	  String[] stringArray = { "z", "g", "c", "o", "a",
230 |             "@", "b", "A", "0", "." };
231 |       	  System.out.println("\n\nUnsorted Array: ");
232 |       	  printArray(stringArray);
233 |       	
234 |       	  System.out.println("\n\nSelection sort:");
235 |       	  selectionSort(stringArray);
236 |       	  printArray(stringArray);
237 |        }
238 |    }					
239 | 					
240 | 				
241 |
242 | 243 |

Output is:

244 |
245 |
246 |                         
247 |    Unsorted Array: 
248 |    34 17 23 35 45 9 1 
249 |    Is intArray sorted? false
250 |    
251 |    Selection sort:
252 |    1 9 17 23 34 35 45 
253 |    Is intArray sorted? true
254 |    
255 |    
256 |    Unsorted Array: 
257 |    z g c o a @ b A 0 . 
258 |    
259 |    Selection sort:
260 |    . 0 @ A a b c g o z 
261 |          
262 |        
263 |
264 |
265 |
266 | 267 |
268 |

SHELLSORT

269 | 270 |

Shell sort is perfect good for embedded systems as it 271 | doesn’t require a lot of extra memory allocation; it is also 272 | useful for small to medium size arrays. Lets see the 273 | following implementation in java code:

274 | 275 |
276 |
277 |                
278 |    public class ShellSort {
279 |    
280 |     public static void shellSort(Comparable[] array) {
281 |      int N = array.length;
282 |      int h = 1;
283 |      while (h < N/3) h = 3*h + 1;
284 |      while (h >= 1){
285 |       for (int i=h; i<N ; i++) {
286 |        for(int j=i; j>=h && isLess(array[j], array[j-h]); j-=h){
287 |         swap(array, j, j-h);
288 |        }
289 |       }
290 |       h = h/3;
291 |      }
292 |     }
293 |    
294 |     // returns true if Comparable j is less than min
295 |     private static boolean isLess(Comparable j, Comparable min) {
296 |     
297 |      int comparison = j.compareTo(min);
298 |      return comparison< 0;
299 |      
300 |     }
301 |    
302 |     private static void swap(Comparable[] array, int i, int j) {
303 |     
304 |      Comparable temp = array[i];
305 |      array[i] = array[j];
306 |      array[j] = temp;
307 |      
308 |     }
309 |    
310 |     public static <E> void printArray(E[] array) {
311 |     
312 |      for (int i = 0; i < array.length; i++) {
313 |       System.out.print(array[i] + " ");
314 |      }
315 |      
316 |     }
317 |    
318 |     // Check if array is sorted
319 |     public static boolean isSorted(Comparable[] array) {
320 |     
321 |      for (int i = 1; i < array.length; i++) {
322 |       if (isLess(array[i], array[i - 1])) {
323 |        return false;
324 |       }
325 |      }
326 |      return true;
327 |     }
328 |    
329 |     public static void main(String[] args) {
330 |     
331 |      Integer[] intArray = { 34, 1000, 23, 2, 35, 0, 9, 1 };
332 |      System.out.println("Unsorted Array: ");
333 |      printArray(intArray);
334 |      System.out.println("\nIs intArray sorted? " + isSorted(intArray));
335 |    
336 |      shellSort(intArray);
337 |      System.out.println("\nSelection sort:");
338 |      printArray(intArray);
339 |      System.out.println("\nIs intArray sorted? " + isSorted(intArray));
340 |    
341 |      String[] stringArray = { "z", "Z","999", "g", "c", "o",
342 |          "a", "@", "b", "A","0", "." };
343 |      System.out.println("\n\nUnsorted Array: ");
344 |      printArray(stringArray);
345 |    
346 |      System.out.println("\n\nSelection sort:");
347 |      shellSort(stringArray);
348 |      printArray(stringArray);
349 |     }
350 |    }
351 |                
352 |                
353 |                
354 |                
355 |             
356 |
357 | 358 |

Output:

359 |
360 |
361 |                
362 |    Unsorted Array: 
363 |    34 1000 23 2 35 0 9 1 
364 |    Is intArray sorted? false
365 |    
366 |    Selection sort:
367 |    0 1 2 9 23 34 35 1000 
368 |    Is intArray sorted? true
369 |    
370 |    
371 |    Unsorted Array: 
372 |    z Z 999 g c o a @ b A 0 . 
373 |    
374 |    Selection sort:
375 |    . 0 999 @ A Z a b c g o z  
376 |                
377 |             
378 |
379 | 380 | 381 |
382 |
383 |
384 |

MERGESORT

385 | 386 |

Mergesort is also called divide and conquer algorithm, 387 | because it divides the original data into smaller pieces of 388 | data to solve the problem. Merge sort works in the following 389 | way:

390 |
    391 |
  1. Divide into 2 collections. Mergesort will take the 392 | middle index in the collection and split it into the left 393 | and right parts based on this middle index.
  2. 394 |
  3. Resulting collections are again recursively splited 395 | and sorted
  4. 396 |
  5. Once the sorting of the two collections is 397 | finished, the results are merged
  6. 398 |
  7. Now Mergesort it picks the item which is smaller 399 | and inserts this item into the new collection.
  8. 400 |
  9. Then selects the next elements and sorts the 401 | smaller element from both collections
  10. 402 |
403 |
404 |
405 |                
406 |    public class MergeSort {
407 |    
408 |        public int[] sort(int [] array){
409 |         mergeSort(array, 0, array.length-1);
410 |         return array;
411 |         
412 |        }
413 |        
414 |        private void mergeSort(int[] array, int first, int last) {
415 |        
416 |         // We need mid to divide problem into smaller size pieces
417 |         int mid = (first + last) / 2;
418 |         
419 |         //If first < last the array must be recursively sorted
420 |         if (first < last) {
421 |          mergeSort(array, first, mid);
422 |          mergeSort(array, mid + 1, last);
423 |         }
424 |         
425 |         //merge solved pieces to get solution to original problem
426 |         int a = 0, f = first, l = mid + 1;
427 |         int[] temp = new int[last - first + 1];
428 |       
429 |         while (f <= mid && l <= last) {
430 |          temp[a++] = array[f] < array[l] ? array[f++] : array[l++];
431 |         }
432 |         
433 |         while (f <= mid) {
434 |          temp[a++] = array[f++];
435 |         }
436 |         
437 |         while (l <= last) {
438 |          temp[a++] = array[l++];
439 |         }
440 |         
441 |         a = 0;
442 |         while (first <= last) {
443 |          array[first++] = temp[a++];
444 |         }
445 |        }
446 |        
447 |        public static void printArray(int[] array) {
448 |         for (int i = 0; i < array.length; i++) {
449 |          System.out.print(array[i] + " ");
450 |         }
451 |         
452 |        }
453 |       
454 |        public static void main(String[] args) {
455 |        
456 |         System.out.println("Original array:");
457 |         int[] example = { 100, 30, 55, 62, 2, 42, 20, 9, 394, 1, 0 };
458 |         printArray(example);
459 |         
460 |         System.out.println("\nMergesort");
461 |         MergeSort merge = new MergeSort();
462 |         merge.sort(example);
463 |         printArray(example);
464 |         
465 |        }
466 |    }  
467 |                   
468 |             
469 |
470 | 471 |

Output:

472 | 473 |
474 |
475 |                
476 |    Original array:
477 |    100 30 55 62 2 42 20 9 394 1 0 
478 |    Mergesort
479 |    0 1 2 9 20 30 42 55 62 100 394   
480 |                
481 |             
482 |
483 | 484 | 485 |
486 |
487 | 488 |
489 |

QUICKSORT

490 | 491 |

Quick sort is better than merge sort from a memory usage 492 | comparison. Because quick sort doesn’t require additional 493 | storage to work. It only uses a small auxiliary stack.

494 |

Skiena discovered via experimentation that a properly 495 | implemented quicksort is typically 2-3 times faster than 496 | mergesort or heapsort

497 |

The pseudo-code for quicksort is:

498 |
    499 |
  1. If the array contains only 1 element or 0 elements 500 | then the array is sorted.
  2. 501 |
  3. If the array contains more than 1 element:
  4. 502 |
  5. Select randomly an element from the array. This is 503 | the "pivot element".
  6. 504 |
  7. Split into 2 arrays based on pivot element: smaller 505 | elements than pivot go to the first array, the ones above 506 | the pivot go into the second array
  8. 507 |
  9. Sort both arrays by recursively applying the 508 | Quicksort algorithm.
  10. 509 |
  11. Merge the arrays
  12. 510 |
511 |

512 | Quicksort may take quadratic time O(n2) to 513 | complete but this is highly improbable to occur. This 514 | happens when the partitions are unbalanced. For example, the 515 | first partition is on the smallest item, the second 516 | partition of the next smallest item, so each time the 517 | program just remove one item for each call, leading to a 518 | large number of partitions of large sub arrays .If the 519 | collection of elements is previously randomized the most 520 | probable performance time for quicksort is Θ (n log n). So 521 | if you’re having problems with performance of quicksort, you 522 | can rearrange randomly elements in the collection this will 523 | increase the probability of quicksort to perform in Θ (n log 524 | n). 525 |

526 | 527 |

Implementation of quicksort:

528 |
529 |
530 |                
531 |    import java.util.Random;
532 |    
533 |    public class QuickSort {
534 |    
535 |        public void quicksort(int[] array) {
536 |         quicksort(array, 0, array.length - 1);
537 |        }
538 |        
539 |        private void quicksort(int[] array, int first, int last) {
540 |         if (first < last) {
541 |          int pivot = partition(array, first, last);
542 |          
543 |          // Recursive call
544 |          quicksort(array, first, pivot - 1);
545 |          quicksort(array, pivot + 1, last);
546 |         }
547 |        }
548 |        
549 |        private int partition(int[] input, int first, int last) {
550 |        
551 |         /*
552 |         *notice how pivot is randomly selected this makes O(n^2) 
553 |         *very low probability
554 |         */
555 |         int pivot = first + new Random().nextInt(last - first + 1); 
556 |         
557 |         swap(input, pivot, last);
558 |         for (int i = first; i < last; i++) {
559 |          if (input[i] <= input[last]) {
560 |           swap(input, i, first);
561 |           first++;
562 |          }
563 |         }
564 |         
565 |         swap(input, first, last);
566 |         return first;
567 |        }
568 |        
569 |        private void swap(int[] input, int a, int b) {
570 |         int temp = input[a];
571 |         input[a] = input[b];
572 |         input[b] = temp;
573 |        }
574 |        
575 |        public static void printArray(int[] array) {
576 |         for (int i = 0; i < array.length; i++) {
577 |          System.out.print(array[i] + " ");
578 |         }
579 |        }
580 |        
581 |        public static void main(String[] args) {
582 |        
583 |         int[] example = { 90, 6456, 20, 34, 65, -1, 54, -15, 
584 |         1, -999, 55, 529, 0 }; 
585 |         System.out.println("Original Array");
586 |         printArray(example);
587 |         System.out.println("\n\nQuickSort");
588 |         QuickSort sort = new QuickSort();
589 |         sort.quicksort(example);
590 |         printArray(example);
591 |         
592 |        }
593 |    }
594 |                
595 |             
596 |
597 | 598 |

Output:

599 | 600 |
601 |
602 |                
603 |    Original Array
604 |    90 6456 20 34 65 -1 54 -15 1 -999 55 529 0 
605 |    
606 |    QuickSort
607 |    -999 -15 -1 0 1 20 34 54 55 65 90 529 6456
608 |                
609 |             
610 |
611 | 612 | 613 |
614 |
615 | 616 |
617 |

LINEAR SEARCH

618 | 619 |

This is the most basic type of search, easy to implement 620 | and understand but inefficient for extremely large data 621 | sets. Let’s look at the following implementation:

622 | 623 |
624 |
625 |                
626 |    public class LinearSearch {
627 |    
628 |       public static void main(String[] args) {
629 |          int[] array = { 0, 2, 9, 10, 100, -2, -3, 
630 |                         902, 504, -1000, 20, 9923 };
631 |          
632 |          System.out.println(linearSearch(array, 100)); 
633 |          //prints: 100 found at position index: 4
634 |          
635 |          System.out.println(linearSearch(array, -1)); 
636 |          //prints: not found
637 |          
638 |          System.out.println(linearSearch(array, 9923));
639 |          //prints: 9923 found at position index: 11
640 |       }
641 |       
642 |       private static String linearSearch(int[] array, int toSearch){
643 |          String result ="not found";
644 |          
645 |          for (int i=0; i< array.length; i++){
646 |             if(array[i]==toSearch){
647 |                result = array[i] +" found at position index: " + i;
648 |                break;
649 |             }
650 |          }
651 |          return result;
652 |       }
653 |    }  
654 |                
655 |             
656 |
657 | 658 |
659 |
660 | 661 |
662 |

BINARY SEARCH

663 | 664 |

Binary search is also easy to implement and easy to 665 | understand. We actually make use of binary search without 666 | knowing when searching in a dictionary for a specific word 667 | or in a phone book. Technically it’s not the same but it is 668 | a very similar process.

669 |

We open a dictionary more or less by the middle if the 670 | word we are searching for starts in a letter over the 671 | middle, we discard the first half of the dictionary and now 672 | focus more or less by the middle of the second half. If the 673 | word is below the middle of the second half, we discard the 674 | top half and keep applying this same technique until we find 675 | our word

676 |

Let’s look at the pseudocode to make this idea clearer:

677 |
    678 |
  1. Receive a sorted array of n elements
  2. 679 |
  3. Compute middle point
  4. 680 |
  5. If index toSearch is less than array[mid] then 681 | highestIndex = mid -1 (This changes mid)
  6. 682 |
  7. If index toSearch is greater than array[mid] then 683 | lowestIndex = mid +1
  8. 684 |
  9. else if index isn't greater nor less than 685 | array[mid], this means it's equal so return 0
  10. 686 |
  11. If not greater, less nor equal, then it's not in 687 | the array so return -1
  12. 688 |
689 | 690 |
691 |
692 |                
693 |    public class BinarySearch {
694 |    
695 |     private BinarySearch(){ } //Constructor
696 |    
697 |     public static int binarySearch(int toSearch, int[]array){
698 |         int lowestIndex = 0;
699 |         int highestIndex = array.length -1;
700 |      
701 |        while (lowestIndex <= highestIndex){
702 |                   int mid = lowestIndex + 
703 |                      (highestIndex - lowestIndex)/2;
704 |    
705 |                    if  ( toSearch < array[mid]) 
706 |                          highestIndex = mid -1;
707 |       
708 |                   else if ( toSearch > array[mid]) 
709 |                               lowestIndex = mid + 1;
710 |       
711 |                    else if (toSearch==array[mid])
712 |                                return mid;
713 |        }
714 |        return -1;
715 |     }
716 |     
717 |     public static void main(String[] args) {
718 |      int [] array = { -100, 1 , 2, 3, 4, 5, 100, 999, 10203};
719 |      
720 |      System.out.println(binarySearch(999, array));  //returns index 6
722 |      System.out.println(binarySearch(-100, array)); //returns index 0
724 |      System.out.println(binarySearch(6, array));    //returns index -1
726 |     }
727 |     
728 |    }  
729 |                
730 |             
731 |
732 |

Java implements its own .binarySearch() static methods 733 | in the classes Arrays and Collections in the standard 734 | java.util package for performing binary searches on Java 735 | arrays and on Lists, respectively. They must be arrays of 736 | primitives, or the arrays or Lists must be of a type that 737 | implements the Comparable interface, or you must specify a 738 | custom Comparator object.

739 |
740 |
741 | 742 | 743 | 744 |
745 | 746 | 747 | 748 | 749 | 750 | -------------------------------------------------------------------------------- /big-O-notation.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | Big O Notation 12 | 13 | 14 | 15 | 16 | 17 | 18 | 79 | 80 | 81 | 82 |
83 | 84 |
85 |

HOW TO DETERMINE COMPLEXITIES

86 | 87 |

We can determine complexity based on the type of 88 | statements used by a program. The following examples are in 89 | java but can be easily followed if you have basic 90 | programming experience and use big O notation we will 91 | explain later why big O notation is commonly used:

92 | 93 |

Constant time: O(1)

94 | 95 |

96 | The following operations take constant time: 97 |

98 | 106 |

They take constant time because they are "simple" 107 | statements. In this case we say the statement time is O(1)

108 | 109 |
110 | 111 |
112 | 					
113 | 	int example = 1;
114 | 				
115 |
116 | 117 |

As you can see in the graph below constant time is 118 | indifferent of input size. Declaring a variable, inserting 119 | an element in a stack, inserting an element into an unsorted 120 | linked list all these statements take constant time.

121 | 122 | 124 | 125 |

Linear time: O(n)

126 |

127 | The next loop executes N times, if we assume the statement 128 | inside the loop is O(1), then the total time for the loop is 129 | N*O(1), which equals O(N) also known as linear time: 130 |

131 | 132 |
133 |
134 | 					
135 | 	for (int i = 0; i < N; i++) {
136 | 		    //do something in constant time...
137 | 	}
138 | 				
139 | 				
140 |
141 | 142 |

In the following graph we can see how running time 143 | increases linearly in relation to the number of elements n:

144 | 146 | 147 |

More examples of linear time are:

148 | 153 | 154 |

155 | Quadratic time: O(n2) 156 |

157 |

158 | In this example the first loop executes N times. For each 159 | time the outer loop executes, the inner loop executes N 160 | times. Therefore, the statement in the nested loop executes 161 | a total of N * N times. Here the complexity is O(N*N) which 162 | equals O(N2). This should be avoided as this 163 | complexity grows in quadratic time 164 |

165 |
166 |
167 | 					
168 | 	for (int i=0; i < N; i++) {
169 |          		for(int j=0; j< N; j++){
170 |          			   //do something in constant time...
171 |          		}
172 | 	}
173 | 				
174 | 				
175 |
176 | 177 |

Some extra examples of quadratic time are:

178 | 187 | 188 |

Algorithms that scale in quadratic time are better to be 189 | avoided. Once the input size reaches n=100,000 element it 190 | can take 10 seconds to complete. For an input size of 191 | n=1’000,000 it can take ~16 min to complete; and for an 192 | input size of n=10’000,000 it could take ~1.1 days to 193 | complete...you get the idea.

194 | 196 | 197 | 198 | 199 | 200 |

Logarithmic time: O(Log n)

201 |

202 | Logarithmic time grows slower as N grows. An easy way to 203 | check if a loop is log n is to see if the counting variable 204 | (in this case: i) doubles instead of incrementing by 1. In 205 | the following example 206 | int i 207 | doesn’t increase by 1 (i++), it doubles with each run thus 208 | traversing the loop in log(n) time: 209 |

210 | 211 |
212 | 213 |
214 | 					
215 | 	for(int i=0; i < n; i *= 2) {
216 |        	  	//do something in constant time...
217 |     	}						
218 | 	
219 | 				
220 |
221 |

Some common examples of logarithmic time are:

222 | 226 | 227 |

Don't feel intimidated by logarithms. Just remember that 228 | logarithms are the inverse operation of exponentiating 229 | something. Logarithms appear when things are constantly 230 | halved or doubled.

231 | 232 |

Logarithmic algorithms have excellent performance in 233 | large data sets:

234 | 235 | 237 | 238 |

Linearithmic time: O(n*Log n)

239 | 240 |

Linearithmic algorithms are capable of good performance 241 | with very large data sets. Some examples of linearithmic 242 | algorithms are:

243 | 248 | 249 |

250 | We'll see a custom implementation of Merge and Quicksort in 251 | the algorithms section. But 252 | for now the following example helps us illustrate our point: 253 |

254 | 255 |
256 | 257 |
258 | 					
259 | 	for(int i= 0; i< n; i++) { // linear loop  O(n) * ...
260 |           		for(int j= 1; j< n; j *= 2){ // ...log (n)
261 |               		  //do something in constant time...
262 |           		}
263 | 	}					
264 | 					
265 | 				
266 |
267 | 268 | 270 | 271 | 272 |

Conclusion

273 |

274 | As you might have noticed, Big O notation describes the 275 | worst case possible. When you loop through an array in order 276 | to find if it contains X item the worst case is 277 | that it’s at the end or that it’s not even present on the 278 | list. Making you iterate through all n items, thus O(n). The 279 | best case would be for the item we search to be at the 280 | beginning so every time we loop it takes constant time to 281 | search but this is highly uncommon and becomes more 282 | improbable as the list of items increases. In the next 283 | section we'll look deeper into why big O focuses on worst 284 | case analysis. 285 |

286 | 287 |

A comparison of the first four complexities, might let 288 | you understand why for large data sets we should avoid 289 | quadratic time and strive towards logarithmic or 290 | linearithmic time:

291 | 292 | 294 | 295 | 296 |
297 |
298 | 299 |
300 |

BIG O NOTATION AND WORST CASE ANALYSIS

301 |

Big O notation is simply a measure of how well an 302 | algorithm scales (or its rate of growth). This way we can 303 | describe the performance or complexity of an algorithm. Big 304 | O notation focuses on the worst-case scenario.

305 |

306 | Why focus on worst case performance? At first look it might 307 | seem counter-intuitive why not focus on best case or at 308 | least in average case performance? I like a lot the answer 309 | given in The Algorithms Design Manual by S. Skiena: 310 |

311 |

Imagine you go to a casino what will happen if you bring 312 | n dollars?

313 | 338 |

Now think of this in a context of a program with 339 | .search() method which takes linear time to execute:

340 |

The worst case is O(n), this is when the key is at the 341 | end or never present in the list. Which might happen.

342 |

The best case is O(1), this happens if and only if the 343 | key is at the beginning of the list. Which becomes even more 344 | unlikely as n grows.

345 |
346 | 347 | 348 |
349 |
350 | 351 | 352 | 353 | 354 | 355 | -------------------------------------------------------------------------------- /css/style.css: -------------------------------------------------------------------------------- 1 | @CHARSET "UTF-8"; 2 | 3 | 4 | html { 5 | position: relative; 6 | min-height: 100%; 7 | } 8 | 9 | body { 10 | bottom: 0; 11 | top: 0; 12 | left: 0; 13 | right: 0; 14 | margin: 0; 15 | font-family: sans-serif, Verdana, serif; 16 | font-size: 0.9em; 17 | } 18 | 19 | p{ 20 | line-height:22px; 21 | } 22 | 23 | li{ 24 | font-size:1.2em; 25 | margin-bottom: 0.2em; 26 | font-weight: 500; 27 | font-family: "Titillium Web",sans-serif, Verdana, serif; 28 | color:rgba(0,0,0, 0.8); 29 | } 30 | 31 | a{ 32 | color: blue; 33 | } 34 | 35 | #main a:visited{ 36 | color:blue; 37 | } 38 | 39 | /* ============== HEADER ============== */ 40 | aside { 41 | position: fixed; 42 | top: 0; 43 | bottom: 0; 44 | left: 0; 45 | min-width: 20%; 46 | height: 100%; 47 | left: 0; 48 | background-color: rgba(0, 0, 100, 0.1); 49 | border-right: 1px solid rgba(0, 0, 0, 0.1); 50 | opacity: 0.8; 51 | } 52 | 53 | aside:hover{ 54 | opacity: 1; 55 | } 56 | 57 | #menu-button{ 58 | display: none; 59 | 60 | } 61 | 62 | #logo { 63 | margin-top: 3.6em; 64 | display: block; 65 | margin-left: -0.3em; 66 | margin-right: auto; 67 | min-height: 13%; 68 | height: 13%; 69 | } 70 | 71 | 72 | aside ul:not(#share-list) { 73 | margin: 5em auto; 74 | vertical-align: middle; 75 | } 76 | 77 | aside ul:not(#share-list) li { 78 | width: 100%; 79 | margin: 0 auto 0px -2.8em; 80 | list-style: none; 81 | vertical-align: middle; 82 | font-size: 1em; 83 | padding-bottom: 1em; 84 | padding-top: 0.5em; 85 | } 86 | 87 | aside a { 88 | text-decoration: none; 89 | color: black; 90 | } 91 | 92 | .aside-icons { 93 | width: 1.5em; 94 | margin-right: 0.6em; 95 | margin-left: 3.6em; 96 | margin-bottom: -6px; 97 | } 98 | 99 | #share-div { 100 | margin-top: 7%; 101 | } 102 | 103 | #share-list { 104 | width: 70%; 105 | margin: 0 auto; 106 | margin-left: -1em; 107 | } 108 | 109 | #share-list li { 110 | display: inline; 111 | margin: 0 auto; 112 | } 113 | 114 | .share-buttons { 115 | width: 2em; 116 | margin-left: 1.4em; 117 | } 118 | 119 | /* ============== MAIN ============== */ 120 | #main { 121 | margin: 3% auto auto 250px; 122 | min-width: 30%; 123 | max-width: 70%; 124 | min-height: 30em; 125 | color: rgba(0, 0, 0, 0.8); 126 | text-align: justify; 127 | 128 | } 129 | 130 | #main a { 131 | text-decoration: none; 132 | color: rgb(25, 25, 112); 133 | } 134 | 135 | 136 | #main a:hover { 137 | text-decoration: underline; 138 | color: rgb(70, 70, 222); 139 | } 140 | 141 | 142 | article { 143 | width: 70%; 144 | min-height: 100px; 145 | margin: 0px auto 1px 12em; 146 | padding-bottom: 1em; 147 | font-size: 1.1em; 148 | } 149 | 150 | .titles { 151 | letter-spacing: -3px; 152 | font-family: "Open Sans", sans-serif, Verdana, serif; 153 | font-size: 2em; 154 | color: rgba(0, 4, 10, 0.93); 155 | text-align: left; 156 | } 157 | 158 | .inner-titles{ 159 | letter-spacing: -1.4px; 160 | font-weight: bolder; 161 | font-size: 1.5em; 162 | color: rgba(0, 4, 10, 0.73); 163 | margin-bottom: -10px; 164 | margin-top: 1em; 165 | } 166 | 167 | .code-div{ 168 | line-height: 20px; 169 | width: 78%; 170 | margin: 2em auto; 171 | background-color: black; 172 | border-radius: 1em; 173 | padding-right: 2em; 174 | 175 | } 176 | .Jcode{ 177 | color: white; 178 | } 179 | 180 | .code-comment{ 181 | color: grey; 182 | } 183 | 184 | .graphs{ 185 | display: block; 186 | border-radius: 1em; 187 | width: 50%; 188 | margin: 0 auto 3em; 189 | } 190 | 191 | .division { 192 | height: 0px; 193 | margin: 3em auto; 194 | width: 60%; 195 | margin-bottom: 2em; 196 | border-top: 1px solid rgba(0, 0, 0, 0.2); 197 | border-bottom: 1px solid rgba(0, 0, 0, 0.3); 198 | } 199 | 200 | #github-icon{ 201 | width: 1.5em; 202 | margin-bottom: -5px; 203 | margin-left: 0.6em; 204 | } 205 | 206 | /* ========Media query============ */ 207 | 208 | /* ==== Small devices ==== */ 209 | @media(max-width: 768px){ 210 | 211 | #greetings{ 212 | display:none; 213 | } 214 | 215 | aside{ 216 | position: relative; 217 | top:-40px; 218 | right: 0; 219 | left: 0; 220 | width: 100%; 221 | height: 100px; 222 | background-color: rgba(0, 0, 0, 0.05); 223 | border-bottom: 1px solid rgba(0, 0, 0, 0.15); 224 | margin-bottom: -170px; 225 | margin-right: -200px; 226 | 227 | } 228 | 229 | #logo{ 230 | margin-top: 2em; 231 | margin-left: -0.2em; 232 | height: 4em; 233 | float: left; 234 | } 235 | 236 | #menu-button{ 237 | margin-top: 3em; 238 | margin-right: 0.8em; 239 | padding: 0.8em; 240 | border: 1px solid grey; 241 | border-radius: 9px; 242 | float: right; 243 | display: inline-table; 244 | } 245 | 246 | #responsive-nav{ 247 | display:none; 248 | padding-top: 2em; 249 | } 250 | 251 | article { 252 | width: 100%; 253 | min-height: 100px; 254 | margin: 2% auto 1px 0; 255 | padding-bottom: 2em; 256 | font-size: 0.9em; 257 | text-align: justify; 258 | display: 259 | } 260 | 261 | .titles{ 262 | font-size: 1.6em; 263 | letter-spacing: -2px; 264 | 265 | } 266 | 267 | .inner-titles{ 268 | font-size: 1.3em; 269 | } 270 | 271 | #main { 272 | margin: 45% 10% auto 10%; 273 | } 274 | 275 | .graphs{ 276 | width: 100%; 277 | } 278 | 279 | .code-div{ 280 | width: 115%; 281 | margin-left: -2em; 282 | } 283 | 284 | code{ 285 | font-size: 11px; 286 | } 287 | 288 | 289 | #share-list{ 290 | display:none; 291 | } 292 | 293 | h1{ 294 | text-align: justify; 295 | } 296 | 297 | } 298 | 299 | @media(min-width: 390px) and (max-width: 750px){ 300 | aside{ 301 | display: block; 302 | } 303 | 304 | nav{ 305 | display:table; 306 | margin-top:30px; 307 | } 308 | } 309 | 310 | @media(max-width: 990px){ 311 | #greetings{ 312 | display: none; 313 | } 314 | 315 | } 316 | -------------------------------------------------------------------------------- /css/tables-styles.css: -------------------------------------------------------------------------------- 1 | @CHARSET "UTF-8"; 2 | 3 | #greetings{ 4 | font-size: 12px; 5 | line-height: 15px; 6 | width:65%; 7 | margin-left: 23%; 8 | } 9 | 10 | 11 | #greetings a{ 12 | text-decoration: none; 13 | } 14 | #top-github{ 15 | height: 1.3em; 16 | } 17 | 18 | table { 19 | border-collapse: separate; 20 | font-size: 100%; 21 | line-height: 10%; 22 | margin: 3em auto 3em 20%; 23 | text-align: left; 24 | width: 90%; 25 | } 26 | 27 | thead { 28 | background: grey; 29 | cursor: pointer; 30 | } 31 | 32 | .big-header-cells{ 33 | text-align: center; 34 | cursor: none; 35 | } 36 | 37 | th { 38 | color: white; 39 | font-weight: bolder; 40 | padding: 1em 1.5em; 41 | position: relative; 42 | font-size: 1em; 43 | } 44 | 45 | td:not(.first-column){ 46 | text-align: center; 47 | padding: 1em; 48 | } 49 | 50 | .first-column{ 51 | background-color: rgba(0,0,0,0.2); 52 | font-weight: bolder; 53 | font-size-adjust: 1px; 54 | } 55 | 56 | #growth-table th{ 57 | text-align: center; 58 | } 59 | 60 | #growth-table tbody tr td:FIRST-CHILD{ 61 | text-align: left; 62 | background-color: rgba(0,0,0, 0.1); 63 | font-weight: bolder; 64 | 65 | } 66 | 67 | .greenest{ 68 | background-color: rgba(0,150,0, 0.6); 69 | color: #004000; 70 | } 71 | 72 | .green{ 73 | background-color: rgba(130, 240, 10, 0.6); 74 | color: #004000; 75 | } 76 | 77 | .yellow{ 78 | background-color: rgba(255,255,10, 0.6); 79 | color: #804000; 80 | } 81 | 82 | .orange{ 83 | background-color: rgba(255,100,0, 0.6); 84 | color: #804000; 85 | } 86 | 87 | .red{ 88 | background-color: rgba(255,0,0, 0.6); 89 | color: #800000; 90 | } 91 | 92 | tbody:hover td{ 93 | color: transparent; 94 | background-color: transparent; 95 | border:0px; 96 | } 97 | 98 | tbody:hover tr:hover td { 99 | color: black; 100 | } 101 | 102 | td { 103 | padding: 1%; 104 | position: relative; 105 | transition: all 300ms; 106 | } 107 | 108 | .table-titles{ 109 | letter-spacing: -3px; 110 | font-family: "Open Sans", sans-serif, Verdana, serif; 111 | font-size: 2em; 112 | color: rgba(0, 4, 10, 0.93); 113 | text-align: left; 114 | margin-bottom: 1em; 115 | } 116 | 117 | 118 | 119 | /* ========Media query============ */ 120 | @media ( max-width : 768px) { 121 | table { 122 | font-size: 70%; 123 | line-height: 10px; 124 | margin: auto; 125 | margin-left: -13%; 126 | width: 90%; 127 | } 128 | 129 | thead{ 130 | font-size: 8px; 131 | 132 | } 133 | 134 | th { 135 | color: white; 136 | font-weight: bold; 137 | position: relative; 138 | font-size: 1em; 139 | } 140 | 141 | article{ 142 | margin-top: 10em; 143 | } 144 | 145 | } 146 | 147 | -------------------------------------------------------------------------------- /data-structures.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | Data Structures 13 | 14 | 15 | 16 | 17 | 18 | 19 | 80 | 81 | 82 |
83 | 84 | 85 |
86 |

DATA STRUCTURES

87 |

Data structures are fundamental constructs around which 88 | you build your application. A data structure determines the 89 | way data is stored, and organized in your computer. Whenever 90 | data exists it must have some kind of data structure in 91 | order to be stored in a computer.

92 | 93 |

Contiguous or linked data 94 | structures

95 | 96 | 97 |

Data structures can be classified as either contiguous 98 | or linked, depending whether they are based on arrays or 99 | pointers(references):

100 |

101 | Contiguous-allocated structures, are made of single 102 | slabs of memory, some of these data structures are arrays, 103 | matrices, heaps, and hash tables. 104 |

105 |

106 | Linked data structures, are composed as distinct 107 | chunks of memory linked together by pointers (references). 108 | Some of this data structures are lists, trees, and graph 109 | adjacency lists. 110 |

111 | 112 |

Comparison

113 |

Some advantages of linked lists over static arrays are:

114 | 123 |

Advantages of arrays:

124 | 129 |
130 |
131 | 132 |
133 |

ARRAY

134 | 135 |

Arrays are the fundamental contiguously allocated data 136 | structure. They have a fixed size and each element can be 137 | efficiently located by its index. Imagine an array is like a 138 | street full of houses, one right next to each other, each 139 | house can be easily located by its address (index).

140 |

The following is an example of usage of Java's 141 | implementation of ArrayList, which is an Array that is 142 | resized when needed

143 | 144 |
145 |
146 | 					
147 |                
148 |     /*
149 |     * We can determine at the moment of instantiation the capacity
150 |     * of the	ArrayList this gives a little boost in performance, 
151 |     * instead of making the array resize constantly
152 |     */
153 |       List<String> exampleList = new ArrayList<>(100);
154 |        
155 |     /*
156 |      * Don't confuse capacity with size, the following 
157 |      * statement outputs 0, as currently the ArrayList 
158 |      * size is 0, but it's capacity is 100
159 |      */
160 |       System.out.println(exampleList.size());
161 |    
162 |       exampleList.add("first");
163 |       exampleList.add("second");
164 |       exampleList.add("third");
165 |       
166 |       System.out.println(exampleList.size());//prints 3
167 | 					
168 | 					
169 |
170 |
171 |
172 | 173 |
174 |

SET

175 | 176 |

A Set is a Collection that cannot contain duplicate 177 | elements.

178 | 179 |

In Java the Set interface contains methods inherited 180 | from Collection and adds the restriction that duplicate 181 | elements are prohibited. Java also adds a stronger contract 182 | on the behavior of the equals() and hashCode() methods, 183 | allowing Set instances to be compared meaningfully even if 184 | their implementation types differ. Some methods declared by 185 | Set are: 186 |

200 |

Let's use an example of Java's implementation of set to 201 | understand how it works. First, we need to define the 202 | DataType and override the equals() and hashCode() methods:

203 | 204 |
205 |
206 | 					
207 |    public class DataType {
208 |    	private String name;
209 |    	private int number;
210 |    	
211 |    	public DataType(String name, int number){
212 |    		this.name = name;
213 |    		this.number = number;
214 |    	}
215 |    
216 |    	@Override
217 |    	public int hashCode() {
218 |    		final int prime = 31;
219 |    		int result = 1;
220 |    		result = prime * result + ((name == null) ? 0 :
221 |    		 name.hashCode());
222 |    		return result;
223 |    	}
224 |    
225 |    	    
226 |       /*
227 |       * We override method equals so that objects 
228 |       * with same name can't be added to the set, 
229 |       * but objects with same numbers can be added
230 |       */
231 |    	@Override
232 |    	public boolean equals(Object obj) {
233 |    		if (obj == null)
234 |    			return false;
235 |    		
236 |    		if (this.getClass() != obj.getClass())
237 |    			return false;
238 |    		
239 |    		DataType other = (DataType) obj;
240 |    		if (other.name == null) 
241 |    				return false;
242 |    				
243 |    		return this.name.equals(other.name);
244 |    	}
245 |    
246 |    	@Override
247 |    	public String toString() {
248 |    		return "DataType: name=" + name + ", number=" + number;
249 |    	}
250 |    }
251 | 					
252 | 				
253 |
254 | 255 |

Now we can proceed to use the class

256 |
257 |
258 | 					
259 |    Set<DataType> example = new HashSet<>();
260 |    	
261 |    DataType data1 = new DataType("first", 1);
262 |        	
263 |    //notice name repeated it's not valid
264 |    DataType data2 = new DataType("first", 1);
265 |        
266 |    //notice different name but same number it's valid
267 |    DataType data3 = new DataType("second", 1);
268 |    	
269 |    example.add(data1);
270 |    example.add(data2);
271 |    example.add(data3);
272 |    
273 |    for (DataType x : example){
274 |    	System.out.println(x);
275 |    }				
276 | 					
277 | 				
278 |
279 | 280 |

the output is:

281 | 282 |
283 |
284 | 					
285 |    DataType: name=first, number=1
286 |    DataType: name=second, number=1
287 | 					
288 | 				
289 |
290 | 291 |

Multiset

292 | 293 |

A multiset is similar to a set but allows repeated 294 | values. For Java, third-party libraries provide multiset 295 | functionality

296 | 304 |

This data structure is perfect for when we need to 305 | perform statistical data that needs no sorting, for example 306 | calculating the average or Standard Deviation of a multiset.

307 | 308 |
309 |
310 | 311 |
312 |

STACKS AND QUEUES

313 |

314 | Arrays, Linked list, Trees are best use to represent real 315 | objects, Stacks & Queues are best to complete tasks, 316 | they are like a tool to complete and then discard. 317 |

318 |

They are useful to manage data in more a particular way 319 | than arrays and lists.

320 | 324 |

When to use stacks and queues:

325 | 334 | 335 |

336 | The following is an implementation of Queues in Java, based 337 | on the course 338 | by R.Sedgewick in coursera 339 |

340 | 341 |
342 |
343 | 					
344 |        
345 |    /*This implementation uses a singly-linked list 
346 |     * with a static nested class for linked-list nodes. 
347 |     * All operations take constant O(1) time in the worst case.
348 |     */
349 |    public class Queue<Generic> implements Iterable<Generic> {
350 |       	private Node<Generic> firstNode; // beginning of queue
351 |       	private Node<Generic> lastNode; // end of queue
352 |       	private int size; // number of elements on queue
353 |       
354 |       	private static class Node<Item> {
355 |       		private Item item;
356 |       		private Node<Item> next;
357 |       	}
358 |       
359 |       	public Queue() {
360 |       		firstNode = null;
361 |       		lastNode = null;
362 |       		size = 0;
363 |       	}
364 |       
365 |       	public boolean isEmpty() {
366 |       		return firstNode == null;
367 |       	}
368 |       
369 |       	public int size() {
370 |       		return size;
371 |       	}
372 |       
373 |       	public Generic peek() {
374 |       		if (isEmpty())
375 |       			throw new NoSuchElementException("Queue underflow");
376 |       		return firstNode.item;
377 |       	}
378 |       
379 |       	public void enqueue(Generic item) {
380 |       		Node<Generic> oldlast = lastNode;
381 |       		lastNode = new Node<Generic>();
382 |       		lastNode.item = item;
383 |       		lastNode.next = null;
384 |       		if (isEmpty())
385 |       			firstNode = lastNode;
386 |       		else
387 |       			oldlast.next = lastNode;
388 |       		size++;
389 |       	}
390 |       
391 |       	public Generic dequeue() {
392 |       		if (isEmpty())
393 |       			throw new NoSuchElementException("Queue underflow");
394 |       		Generic item = firstNode.item;
395 |       		firstNode = firstNode.next;
396 |       
397 |       		if (isEmpty())
398 |       			lastNode = null;// to avoid loitering
399 |       		size--;
400 |       		return item;
401 |       	}
402 |       
403 |       	public String toString() {
404 |       		StringBuilder s = new StringBuilder();
405 |       		for (Generic item : this)
406 |       			s.append(item + " ");
407 |       		return s.toString();
408 |       	}
409 |       
410 |       	public Iterator<Generic> iterator() {
411 |       		return new ListIterator<Generic>(firstNode);
412 |       	}
413 |       
414 |       	private class ListIterator<Item> implements Iterator <Item> {
415 |       		private Node <Item> current;
416 |       
417 |       		public ListIterator(Node<Item> first) {
418 |       			current = first;
419 |       		}
420 |       
421 |       		public boolean hasNext() {
422 |       			return current != null;
423 |       		}
424 |       
425 |       		public void remove() {
426 |       			throw new UnsupportedOperationException();
427 |       		}
428 |       
429 |       		public Item next() {
430 |       			if (!hasNext())
431 |       				throw new NoSuchElementException();
432 |       			Item item = current.item;
433 |       			current = current.next;
434 |       			return item;
435 |       		}
436 |       	}
437 |       
438 |       	public static void main(String[] args) {
439 |       		Queue<String> q = new Queue<>();
440 |       		q.enqueue("FIRST IN");
441 |       		q.enqueue(" 2nd ");
442 |       		q.enqueue(" 3rd ");
443 |       
444 |       		System.out.println(q.dequeue() + " first out ==> FIFO");
445 |       	}
446 |    }
447 | 					
448 | 				
449 |
450 |
451 |
452 | 453 |
454 |

DICTIONARIES

455 | 456 |

457 | A Dictionary is a data structure that maps a key to a 458 | value.This is useful in cases where you want to be 459 | able to access data via a particular key rather than an 460 | integer index. 461 |

In Java, Dictionaries are implemented as a Map: The Map 462 | interface maps unique keys to values. A key is an object 463 | that you use to retrieve a value at a later date.

464 |

Given a key and a value, you can store the value in a 465 | Map object. After the value is stored, you can retrieve it 466 | by using its key.

467 |

Following is a simple Map Implementation as an array. 468 | Firstly, we create a class to help store the key and it's 469 | value in an object:

470 | 471 |
472 |
473 | 					
474 |    public class Entry<K, V> {
475 |       private final K key;
476 |       private V value;
477 |       
478 |       public Entry(K key, V value) {
479 |       	this.key = key;
480 |       	this.value = value;
481 |       }
482 |       
483 |       public K getKey() {
484 |       	return key;
485 |       }
486 |       
487 |       public V getValue() {
488 |       	return value;
489 |       }
490 |       
491 |       public void setValue(V value) {
492 |       	this.value = value;
493 |       }
494 |    }
495 | 					
496 | 				
497 |
498 | 499 |

Then the implementation of map:

500 |
501 |
502 | 					
503 |    public class Map<K, V> {
504 |       private int size;
505 |       private int CAPACITY = 16;
506 |       private Entry<K, V>[] entriesArray = new Entry[CAPACITY];
507 |       
508 |       public void put(K key, V value) {
509 |       	boolean insert = true;
510 |       	for (int i = 0; i < size; i++) {
511 |       		if (entriesArray[i].getKey().equals(key)) {
512 |       			entriesArray[i].setValue(value);
513 |       			insert = false;
514 |       		}
515 |       	}
516 |       	if (insert) {
517 |       		growArray();
518 |       		entriesArray[size++] = new Entry<K, V>(key, value);
519 |       	}
520 |       }
521 |       
522 |       private void growArray() {
523 |       	if (size == entriesArray.length) {
524 |       		int newSize = entriesArray.length * 2;
525 |       		entriesArray = Arrays.copyOf(entriesArray, newSize);
526 |       	}
527 |       }
528 |       
529 |       public V get(K key) {
530 |       	for (int i = 0; i < size; i++) {
531 |       		if (entriesArray[i] != null) {
532 |       			if (entriesArray[i].getKey().equals(key)) {
533 |       				return entriesArray[i].getValue();
534 |       			}
535 |       		}
536 |       	}
537 |       	return null;
538 |       }
539 |       
540 |       public void remove(K key) {
541 |       	for (int i = 0; i < size; i++) {
542 |       		if (entriesArray[i].getKey().equals(key)) {
543 |       			entriesArray[i] = null;
544 |       			size--;
545 |       			condenseArrayElements(i);
546 |       		}
547 |       	}
548 |       }
549 |           
550 |        //Moves backwards elements from start arg
551 |        private void condenseArrayElements(int start){ 
552 |         for (int i = start; i < size; i++) {
553 |       	  entriesArray[i] = entriesArray[i+1];
554 |         }
555 |        }
556 |        
557 |        public int size(){ return size; }
558 |       
559 |         public Set<K> keySet(){
560 |         Set<K> set = new HashSet<K>();
561 |         for (int i = 0; i < size; i++) {
562 |          set.add(entriesArray[i].getKey());
563 |         }
564 |         return set;
565 |        }
566 |       
567 |         public static void main(String[] args) {
568 |       	  Map<String, Integer> mapExample = new Map<>();
569 |       	   mapExample.put("Key 1", 100);
570 |       	  System.out.println(mapExample.get("Key 1"));
571 |       	  
572 |       	  mapExample.put("Key 2", 200);
573 |       	  mapExample.put("Woaah", 100000);
574 |       	  System.out.println(mapExample.get("Key 2"));
575 |       	    
576 |       	  System.out.println("keySet: " + mapExample.keySet());
577 |       	 
578 |       	  System.out.print("Values: ");
579 |       	  for (String key : mapExample.keySet()){
580 |       	   System.out.print(mapExample.get(key) + " ");
581 |       	  }
582 |       	    mapExample.remove("Key 2");
583 |       	  System.out.println("\nkeySet: " + mapExample.keySet());
584 |       	 }
585 |    }
586 | 					
587 | 				
588 |
589 |
590 |
591 | 592 |
593 | 594 | 595 | 596 | 597 | 598 | -------------------------------------------------------------------------------- /images/graphs/comparison.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 17 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | t 73 | n 74 | O(n) 75 | O(1) 76 | O(n^2) 77 | O(Log n) 78 | 80 | O(n log n) 81 | 82 | -------------------------------------------------------------------------------- /images/graphs/constant.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 0 32 | 1 33 | 100 34 | 1,000 35 | 10,000 36 | 100,000 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | t 80 | n 81 | O(1) 82 | 83 | 84 | -------------------------------------------------------------------------------- /images/graphs/linear.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 0 33 | 1 34 | 100 35 | 1,000 36 | 10,000 37 | 100,000 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | t 81 | n 82 | O(n) 83 | 84 | 85 | -------------------------------------------------------------------------------- /images/graphs/linearithmic.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 0 28 | 1 29 | 100 30 | 1,000 31 | 10,000 32 | 100,000 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | t 72 | n 73 | 75 | O(n log n) 76 | 77 | -------------------------------------------------------------------------------- /images/graphs/log.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 0 33 | 1 34 | 100 35 | 1,000 36 | 10,000 37 | 100,000 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | t 81 | n 82 | O(Log n) 83 | 84 | 85 | -------------------------------------------------------------------------------- /images/graphs/quadratic.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 0 33 | 1 34 | 100 35 | 1,000 36 | 10,000 37 | 100,000 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | t 81 | n 82 | O(n^2) 83 | 84 | 85 | -------------------------------------------------------------------------------- /images/icons/FB-share.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 13 | 14 | -------------------------------------------------------------------------------- /images/icons/algo-icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /images/icons/big-o-icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /images/icons/cheat-sheets.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /images/icons/data-structures-icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /images/icons/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cooervo/Algorithms-DataStructures-BigONotation/dd120294fb512749c9a2482f153edfbb2262b3ca/images/icons/favicon.ico -------------------------------------------------------------------------------- /images/icons/github.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 20 | 21 | -------------------------------------------------------------------------------- /images/icons/google-plus-share.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 9 | 11 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /images/icons/intro-icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 9 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /images/icons/sort-table.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cooervo/Algorithms-DataStructures-BigONotation/dd120294fb512749c9a2482f153edfbb2262b3ca/images/icons/sort-table.gif -------------------------------------------------------------------------------- /images/icons/title-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cooervo/Algorithms-DataStructures-BigONotation/dd120294fb512749c9a2482f153edfbb2262b3ca/images/icons/title-icon.png -------------------------------------------------------------------------------- /images/icons/twitter-share.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 16 | 17 | -------------------------------------------------------------------------------- /images/logo-algos.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 18 | 21 | 25 | 26 | 27 | 29 | 31 | 35 | 47 | 50 | 52 | 53 | 55 | 59 | 60 | 64 | 66 | 69 | 70 | 72 | 76 | 77 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | Big O cheat sheets 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 82 | 83 |

84 | About: I made this website as a fun project to help me 85 | understand better: algorithms, data structures and big 86 | O notation. And also to have some practice in: Java, JavaScript, 87 | CSS, HTML and Responsive Web Design (RWD). If 88 | you discover errors in the code or typos I haven't noticed please let me know or 90 | feel free to contribute by clicking this Github 92 | link 94 | , fork the project and make a pull request :) 95 |

96 | 97 | 98 |
99 |
100 | 101 | 102 |

Instructions

103 | 110 |
111 | 112 | 113 | 114 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 138 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 161 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 171 | 173 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 194 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 205 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 |
Sorting Algorithms
Sorting 136 | Algorithms 137 | Space complexityTime 139 | complexity
Worst caseBest caseAverage caseWorst case
Insertion SortO(1)O(n)O(n2) 160 | O(n2) 162 |
Selection SortO(1)O(n2) 170 | O(n2) 172 | O(n2) 174 |
Smooth SortO(1)O(n)O(n log n)O(n log n)
Bubble SortO(1)O(n)O(n2) 193 | O(n2) 195 |
Shell SortO(1)O(n)O(n log n2) 204 | O(n log n2) 206 |
MergesortO(n)O(n log n)O(n log n)O(n log n)
QuicksortO(log n)O(n log n)O(n log n)O(n2)
HeapsortO(1)O(n log n)O(n log n)O(n log n)
244 | 245 | 246 | 247 | 249 | 250 | 251 | 254 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 282 | 283 | 284 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 329 | 332 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 342 | 344 | 346 | 348 | 350 | 352 | 353 | 354 | 355 | 357 | 359 | 361 | 363 | 365 | 367 | 368 | 369 | 370 | 372 | 374 | 376 | 378 | 380 | 382 | 383 | 384 | 385 |
Data Structures 248 | Comparison
Data 252 | Structures 253 | Average 255 | CaseWorst Case
SearchInsertDeleteSearchInsertDelete
ArrayN/AN/AN/AN/A
Sorted ArrayO(n)O(n)O(n)O(n)
Linked ListO(1)O(1)O(1)O(1)
Doubly Linked ListO(1)O(1)O(1)O(1)
StackO(1)O(1)O(1)O(1)
Hash tableO(1)O(1)O(n)O(n)
Binary Search TreeO(log 330 | 331 | n)O(log 333 | n)O(n)O(n)
B-TreeO(log 343 | n)O(log 345 | n)O(log 349 | n)O(log 351 | n)
Red-Black treeO(log 358 | n)O(log 360 | n)O(log 364 | n)O(log 366 | n)
AVL TreeO(log 373 | n)O(log 375 | n)O(log 379 | n)O(log 381 | n)
386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 398 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 |
Growth Rates
n f(n)log nnn log nn2 397 | 2n 399 | n!
100.003ns0.01ns0.033ns0.1ns1ns3.65ms
200.004ns0.02ns0.086ns0.4ns1ms77years
300.005ns0.03ns0.147ns0.9ns1sec8.4x1015yrs 432 |
400.005ns0.04ns0.213ns1.6ns18.3min--
500.006ns0.05ns0.282ns2.5ns13days--
1000.070.1ns0.644ns0.10ns4x1013yrs 460 | --
1,0000.010ns1.00ns9.966ns1ms----
10,0000.013ns10ns130ns100ms----
100,0000.017ns0.10ms1.67ms10sec----
1'000,0000.020ns1ms19.93ms16.7min----
10'000,0000.023ns0.01sec0.23ms1.16days----
100'000,0000.027ns0.10sec2.66sec115.7days----
1,000'000,0000.030ns1sec29.90sec31.7 years----
528 | 529 | 530 | 531 | 532 |
533 |

Comparison graph

534 | 535 | 537 | 538 | 539 |
540 |
541 | 542 | 543 |
544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | -------------------------------------------------------------------------------- /intro.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | Algorithms and Data Structures 13 | 14 | 15 | 16 | 17 | 18 | 19 | 80 | 81 | 82 |
83 | 84 |
85 |

WHAT IS AN ALGORITHM?

86 | 87 |

The best definition of algorithms that I have come 88 | across is the following:

89 |

90 | Algorithms are a series of steps or rules for solving 91 | a computational problem. 92 |

93 | 94 |

95 | A computational problem is a collection of questions 96 | that computers might be able to solve. 97 |

98 | 99 |

An example of a simple algorithm could be one used by a 100 | coffee maker, it might look something like this:

101 | 102 |
103 | 104 |
105 | 					
106 | 	if (clock.getTime() == 7am){
107 | 		coffeMaker.boilWater();
108 | 		
109 | 		if(water.isBoiled()){
110 | 			coffeMaker.addCoffee();
111 | 			coffeMaker.pourCoffee();
112 | 		}
113 | 		
114 | 	} else{
115 | 		coffeMaker.doNothing();
116 | 	}					
117 | 					
118 | 				
119 |
120 | 121 | 122 |
123 |
124 |
125 |

ALGORITHM EFFICIENCY

126 |

Algorithm efficiency is the study of the amount of 127 | resources used by an algorithm. The less resources used by 128 | the algorithm the more efficient it is. Nevertheless, to 129 | have a good comparison between different algorithms we can 130 | compare based on the resources it uses: how much time it 131 | needs to complete, how much memory it uses to solve a 132 | problem or how many operations it must do in order to solve 133 | the problem

134 | 135 |

136 | Time efficiency: a measure of amount of time an 137 | algorithm takes to solve a problem. 138 |

139 | 140 |

141 | Space efficiency: a measure of the amount of memory 142 | an algorithm needs to solve a problem. 143 |

144 |

145 | Complexity theory: a study of algorithm performance 146 | based on cost functions of statement counts. 147 |

148 |
149 |
150 |
151 | 152 | 153 | 154 | 155 | -------------------------------------------------------------------------------- /js/script.js: -------------------------------------------------------------------------------- 1 | //=======Drop down menu ================ 2 | $("#menu-button").on("click", function(e){ 3 | 4 | e.stopImmediatePropagation(); 5 | var $aside = $("aside"); 6 | $aside.css("height", "25em"); 7 | $aside.css("top", "-9em"); 8 | $aside.css("z-index", "10em"); 9 | $aside.css("margin-bottom", "-8em"); 10 | 11 | var $logo = $("#logo"); 12 | $logo.css("margin-bottom","1em"); 13 | 14 | var $nav = $("#responsive-nav"); 15 | $nav.css("display", "block"); 16 | $nav.css("margin-top", "8em");; 17 | 18 | }); 19 | 20 | //========Table sorter================== 21 | $(function() { 22 | $("#DS-table").tablesorter(); 23 | }); 24 | 25 | $(function() { 26 | $("#sorting-table").tablesorter(); 27 | }); 28 | 29 | $(".sort-table").tablesorter({ 30 | textExtraction : function(node) { 31 | var attr = $(node).attr('data-sort-value'); 32 | if (typeof attr !== 'undefined' && attr !== false) { 33 | return attr; 34 | } 35 | return $(node).text(); 36 | } 37 | }); --------------------------------------------------------------------------------