├── .gitignore ├── README.md ├── ShortCuts.md ├── TODO.txt ├── lib ├── apiguardian-api-1.1.0-javadoc.jar ├── apiguardian-api-1.1.0-sources.jar ├── apiguardian-api-1.1.0.jar ├── hamcrest-core-1.3-javadoc.jar ├── hamcrest-core-1.3-sources.jar ├── hamcrest-core-1.3.jar ├── junit-4.12-javadoc.jar ├── junit-4.12-sources.jar ├── junit-4.12.jar ├── opentest4j-1.2.0-javadoc.jar ├── opentest4j-1.2.0-sources.jar └── opentest4j-1.2.0.jar ├── notes ├── Big O Common-Runtimes.pdf ├── algo development techniques.png ├── essential knowledge for facebook interview.png ├── hashing │ └── hashmap.png ├── sorting │ ├── bubble-sort-in-c.png │ ├── in order traversal algo.png │ ├── level order traversal Breadth first.png │ └── mergeSort.gif └── trees │ ├── Onumber of tree with array and linked list.png │ ├── in order traversal algo.png │ ├── level order traversal Breadth first.png │ ├── tree representation as linke list.png │ ├── tree represented in array.png │ ├── tree terminology.png │ └── types of trees.png ├── src ├── algoBootcamp │ ├── README.md │ ├── algo │ │ ├── binarysearch │ │ │ ├── App.java │ │ │ ├── TestApp.java │ │ │ └── TestInsertionSort.java │ │ ├── insertionsort │ │ │ └── App.java │ │ ├── linearsearch │ │ │ └── App.java │ │ ├── mergesort │ │ │ ├── App.java │ │ │ └── MergeSort.java │ │ ├── quicksort │ │ │ └── App.java │ │ ├── recursion │ │ │ └── App.java │ │ └── selectionsort │ │ │ └── App.java │ └── ds │ │ ├── binarysearchtree │ │ ├── Application.java │ │ ├── BST.java │ │ └── Node.java │ │ ├── circularlinkedlist │ │ ├── App.java │ │ ├── CircularLinkedList.java │ │ └── Node.java │ │ ├── doublylinkedlist │ │ ├── App.java │ │ ├── DoublyLinkedList.java │ │ └── Node.java │ │ ├── graph │ │ ├── App.java │ │ ├── BetterGraph.java │ │ └── Graph.java │ │ ├── heapimplementation │ │ ├── Application.java │ │ ├── Heap.java │ │ └── Node.java │ │ ├── queue │ │ ├── App.java │ │ └── Queue.java │ │ ├── singlylinkedlist │ │ ├── App.java │ │ ├── Node.java │ │ └── SinglyLinkedList.java │ │ └── stack │ │ ├── App.java │ │ └── Stack.java ├── codechallenge │ ├── easy │ │ ├── AddWithoutCarryOver.java │ │ ├── AlphabetSoup.java │ │ ├── BubbleSort.java │ │ ├── CapatilizeFirstLetterOfWords.java │ │ ├── CheckNums.java │ │ ├── ConvertLinkedListToArray.java │ │ ├── CryptString.java │ │ ├── LogN.java │ │ ├── LongestWord.java │ │ ├── NonRecurringCharacter.java │ │ ├── Palindrome.java │ │ ├── PermutationWord.java │ │ ├── PowerOf.java │ │ ├── RemoveAdjacentDuplicate.java │ │ ├── ReverseStringAppTest.java │ │ ├── SimpleAddition.java │ │ ├── SimpleSymbols.java │ │ ├── Sort.java │ │ ├── SumAndProduct.java │ │ ├── TimeConverter.java │ │ ├── TwoSum.java │ │ ├── URLConverter.java │ │ ├── UniqueChar.java │ │ └── array │ │ │ ├── IsArrayInSequence.java │ │ │ ├── IsArrayPartitioned.java │ │ │ ├── Merge2ArraysInsort.java │ │ │ └── MergeSortChallenge.java │ ├── hard │ │ ├── ChessboardTraveling.java │ │ └── KaprekarsConstant.java │ ├── linkedlist │ │ ├── ConvertLinkedListToNumber.java │ │ ├── InsertNodeInSortedLL.java │ │ ├── MergeTwoSortedLinkedList.java │ │ ├── ReverseDoublyLinkedList.java │ │ └── ReverseSinglyLinkedList.java │ └── medium │ │ ├── BinaryNumbers.java │ │ ├── LinkedList.java │ │ ├── Pentagon.java │ │ ├── QuestionMark.java │ │ └── RobotMovement.java ├── crackingTheCodingInterview │ ├── chap1 │ │ ├── 1 QUESTIONS.md │ │ ├── CheckPermutation.java │ │ ├── IsUnique.java │ │ ├── OneAway.java │ │ ├── Palindrome.java │ │ ├── StringCompressor.java │ │ ├── StringRotation.java │ │ ├── URLify.java │ │ └── ZeroMatrix.java │ ├── chap2 │ │ ├── 2 QUESTIONS.md │ │ ├── DeleteMiddleNode.java │ │ ├── PalindromeDoubleLinkedList.java │ │ ├── PalindromeLinkedList.java │ │ ├── Partition.java │ │ ├── RemoveDuplicates.java │ │ ├── RemoveDuplicatesFollowup.java │ │ ├── ReturnKToLast.java │ │ └── SumLists.java │ ├── chap3 │ │ └── 3 QUESTIONS.md │ └── chap4 │ │ └── 4 QUESTIONS.md ├── dsGuy │ ├── AVLTree │ │ ├── AVLTree.java │ │ └── Main.java │ ├── README.md │ ├── bfs │ │ ├── BFSByAdjacencyMatrix.java │ │ ├── BFSByAdjacencyMatrixMain.java │ │ ├── BFSByLinkedList.java │ │ ├── BFSByLinkedListMain.java │ │ ├── PathFindingByBFS.java │ │ └── PathFindingByBFSMain.java │ ├── binarySearchTree │ │ ├── BinarySearchTreeByLinkedList.java │ │ └── BinarySearchTreeByLinkedListMain.java │ ├── binaryTree │ │ ├── BinaryTreeByArray.java │ │ ├── BinaryTreeByArrayMain.java │ │ ├── BinaryTreeByLinkedList.java │ │ └── BinaryTreeByLinkedListMain.java │ ├── dfs │ │ ├── DFSIterative.java │ │ ├── DFSIterativeMain.java │ │ ├── DFSRecursive.java │ │ └── DFSRecursiveMain.java │ ├── hashing │ │ ├── DirectChaining.java │ │ ├── DirectChainingMain.java │ │ ├── DoubleHashing.java │ │ ├── DoubleHashingMain.java │ │ ├── LinearProbing.java │ │ ├── LinearProbingMain.java │ │ ├── QuadraticProbing.java │ │ └── QuadraticProbingMain.java │ ├── heap │ │ ├── HeapByArray.java │ │ └── HeapByArrayMain.java │ ├── node │ │ ├── BinaryNode.java │ │ ├── BinaryNodeWithParent.java │ │ ├── DoubleNode.java │ │ ├── GraphNode.java │ │ ├── NumberOfPathsNode.java │ │ └── SingleNode.java │ ├── sorting │ │ ├── bubble │ │ │ ├── BubbleSort.java │ │ │ └── BubbleSortMain.java │ │ ├── bucket │ │ │ ├── BucketSort.java │ │ │ └── BucketSortMain.java │ │ ├── heap │ │ │ ├── HeapByArray.java │ │ │ ├── HeapSort.java │ │ │ └── HeapSortMain.java │ │ ├── insertion │ │ │ ├── InsertionSort.java │ │ │ └── InsertionSortMain.java │ │ ├── merge │ │ │ ├── MergeSort.java │ │ │ └── MergeSortMain.java │ │ ├── quick │ │ │ ├── QuickSort.java │ │ │ └── QuickSortMain.java │ │ ├── selection │ │ │ ├── SelectionSort.java │ │ │ └── SelectionSortMain.java │ │ └── topologicalSort │ │ │ └── TopologicalSort.java │ └── trie │ │ ├── Trie.java │ │ └── TrieMain.java ├── dynamicProgramming │ ├── backtracking │ │ ├── Anagrams.java │ │ ├── Backtracking-PDF.pdf │ │ ├── Combination.java │ │ ├── CombinationSum.java │ │ ├── NQueens.java │ │ ├── Permutation.java │ │ └── WordBreak.java │ ├── dynamicIntro │ │ ├── BinomialCoefficient.java │ │ ├── Dynamic-Programming-PDF.pdf │ │ ├── Fibonacci.java │ │ ├── Knapsack.java │ │ ├── Optimization.pdf │ │ └── PaintHouse.java │ ├── oneDimmensionalDynamic │ │ ├── LongestIncreasingSubsequence.java │ │ ├── MinCostPath.java │ │ ├── RobHouses.java │ │ ├── RodCutting.java │ │ ├── WordBreak.java │ │ └── pdfs │ │ │ ├── 2.Rod-Cutting-Problem.pdf │ │ │ ├── 3.Longest-Increasing-Subsequence.pdf │ │ │ ├── Exercise-Solutions.pdf │ │ │ └── Introduction.pdf │ ├── recursion │ │ ├── CheckSequence.java │ │ ├── DigitsSum.java │ │ ├── FindMaximum.java │ │ ├── Palindrome.java │ │ └── PrintStar.java │ └── twoDimmensionalDynamic │ │ ├── EditDistance.java │ │ ├── LongestCommonSubsequence.java │ │ ├── MakePalindrome.java │ │ ├── MinimumPathSum.java │ │ ├── RegularExpressionMatching.java │ │ ├── StopWatch.java │ │ └── pdf │ │ ├── 3.Minimum-deletions-for-Palindrome.pdf │ │ ├── 4.Edit-Distance.pdf │ │ ├── 5.Regular-Expression-Matching.pdf │ │ ├── Introduction.pdf │ │ └── Longest-Common-Subsequence.pdf ├── eliminateCodeFear │ ├── README.md │ ├── algorithms │ │ ├── BigO.java │ │ ├── BinarySearchTree.java │ │ ├── BubbleSort.java │ │ ├── BubbleSorter.java │ │ ├── FibonacciMemoized.java │ │ ├── FibonacciNaive.java │ │ ├── MaxIntHeap.java │ │ ├── MergeSort.java │ │ ├── MinIntHeap.java │ │ ├── NodeBST.java │ │ ├── Person.java │ │ ├── QuickSort.java │ │ ├── Trie.java │ │ └── graphs │ │ │ ├── BFS │ │ │ └── Graph.java │ │ │ ├── DFS │ │ │ ├── Graph.java │ │ │ └── PathFinder.java │ │ │ └── dijsktra │ │ │ └── ShortestPath.java │ ├── challenges │ │ ├── arraysAndStrings │ │ │ ├── Compressor.java │ │ │ ├── OneAwayDetector.java │ │ │ ├── PalindromeDetector.java │ │ │ ├── PermutationDetector.java │ │ │ ├── URLConverter.java │ │ │ ├── UniqueCharacterDetector.java │ │ │ ├── Zeroer1.java │ │ │ └── Zeroer2.java │ │ ├── binaryTrees │ │ │ ├── CustomBinaryTree.java │ │ │ ├── CustomBinaryTreeNode.java │ │ │ ├── SimpleBinarySearchTree.java │ │ │ └── SubtreeChecker.java │ │ ├── classics │ │ │ ├── Anagram.java │ │ │ ├── CaesarCipherAdvanced.java │ │ │ ├── CaesarCipherSimple.java │ │ │ ├── CharacterCount.java │ │ │ ├── FizzBuzz.java │ │ │ ├── IntReverser.java │ │ │ ├── RansomNote.java │ │ │ ├── SieveOfEratosthenes.java │ │ │ └── StringReverser.java │ │ ├── interviews │ │ │ ├── AmazonArrayMerger.java │ │ │ ├── CreditCard.java │ │ │ ├── CreditCardProcessor.java │ │ │ └── FaceBookArrayIntersector.java │ │ ├── linkedLists │ │ │ ├── LinkedListAdder.java │ │ │ ├── LinkedListLoopDetector.java │ │ │ ├── LinkedListRemoveDuplicates.java │ │ │ ├── Node.java │ │ │ └── Palindrome.java │ │ └── stacksAndQueues │ │ │ ├── MinStack.java │ │ │ └── StackOfPlates.java │ └── datastructures │ │ ├── DoublyLinkedList.java │ │ ├── DynamicArray.java │ │ ├── HashTable.java │ │ ├── LinkedList.java │ │ ├── LinkedListWithIndex.java │ │ ├── PrettyPrinter.java │ │ ├── Queue.java │ │ ├── SingleLinkedList.java │ │ └── Stack.java ├── geeksForGeeks │ └── binarySearchTree │ │ ├── BinarySearchTree.java │ │ ├── Graph.java │ │ └── MinHeap.java ├── javaConcepts │ ├── UserInputConcept │ │ └── Client.java │ ├── collection │ │ ├── Enum.java │ │ ├── LinkedListDemo.java │ │ ├── MyArrayList.java │ │ ├── MyMaps.java │ │ ├── MyTreeMap.java │ │ ├── PriroityQueue.java │ │ ├── comparable │ │ │ └── Client.java │ │ └── comparator │ │ │ └── Client.java │ ├── equals │ │ └── EqualsMain.java │ ├── exceptions │ │ └── TryMe.java │ ├── failSafe │ │ ├── FailFastIteratorExample.java │ │ └── FailSafeIteratorExample.java │ ├── garbagecollection │ │ └── GC.java │ ├── human │ │ ├── Client.java │ │ ├── GenderFemale.java │ │ ├── GenderMale.java │ │ └── Person.java │ ├── innerclass │ │ ├── AnonClass.java │ │ ├── MethodLocalInnerClass.java │ │ ├── NestedInnerClass.java │ │ └── SimpleNestedClass.java │ ├── lists │ │ ├── Collections1.java │ │ ├── LinkedListDemo.java │ │ └── ListExamples.java │ ├── serialization │ │ ├── Depersist.java │ │ ├── Persist.java │ │ └── Student.java │ ├── set │ │ └── SetEample.java │ ├── sortImp │ │ ├── Hasing.java │ │ └── MySort.java │ ├── threadpool │ │ └── SimpleThreadPool.java │ ├── threads │ │ ├── MyMultiThread.java │ │ ├── MyThread.java │ │ ├── MyThreadUsingInterface.java │ │ ├── TestThreadPool.java │ │ ├── ThreadGroupDemo.java │ │ └── TreadJoin.java │ └── variableShadowing │ │ ├── Bike.java │ │ ├── Car.java │ │ ├── Client.java │ │ ├── Truck.java │ │ └── Vehicle.java └── me │ └── premaseem │ ├── MyUtils.java │ ├── algorithm │ ├── binarySearch │ │ ├── SearchApp.java │ │ └── Test.java │ ├── bubblesort │ │ ├── BubbleSort.java │ │ └── bubble-sort.png │ ├── insersionSort │ │ ├── InsertionSort.java │ │ └── insertion sort.jpg │ ├── mergesort │ │ └── MergeSort.java │ ├── quicksort │ │ └── QuickSort.java │ ├── recursion │ │ ├── RecursiveAlgo.java │ │ └── Tests.java │ └── selectionsort │ │ └── Test.java │ ├── datastructure │ ├── binarySearchTree │ │ └── BinarySearchTree.java │ ├── binaryTree │ │ ├── BST.java │ │ ├── BST_2.java │ │ ├── BSTrec.java │ │ ├── binaryTreeArray │ │ │ ├── BinaryTreeArr.java │ │ │ └── BinaryTreeArrMain.java │ │ └── binaryTreeLinkedList │ │ │ ├── BinaryTreeLinkedList.java │ │ │ └── BinaryTreeLinkedListMain.java │ ├── circularQueue │ │ ├── CircularQueueWithArrays.java │ │ └── CircularQueueWithLinkedList.java │ ├── doublelinkedlist │ │ ├── DoubleLinkedList.java │ │ ├── Node.java │ │ └── Tests.java │ ├── graph │ │ ├── AirlineGraph.java │ │ ├── Graph.java │ │ └── busRoute │ │ │ ├── BusRoute.java │ │ │ ├── Graph.java │ │ │ └── Test.java │ ├── hashtable │ │ ├── MyHashTable_1.java │ │ ├── MyHashTable_2.java │ │ └── hashmap.png │ ├── heap │ │ ├── MinHeap.java │ │ └── imp1 │ │ │ ├── Heap.java │ │ │ ├── Node.java │ │ │ └── Test.java │ ├── simplequeue │ │ ├── CircularQueueTests.java │ │ ├── CustomCircularQueue.java │ │ ├── CustomQueue.java │ │ └── Tests.java │ ├── singlelinkedlist │ │ ├── Node.java │ │ ├── SingleLinkedList.java │ │ └── Test.java │ └── stacks │ │ ├── StackWithArray.java │ │ ├── StackWithArrayList.java │ │ ├── StackWithCustomizedLinkedList.java │ │ ├── StackWithLinkedList.java │ │ └── simplestack │ │ ├── App.java │ │ └── CustomStack.java │ ├── myLib │ ├── MyDLLNode.java │ └── MySLLNode.java │ └── practiceRepeatation │ ├── BubbleSort.java │ ├── Credit_card_validator.java │ ├── Median_filter_signal.java │ ├── MergeSort.java │ └── Quicksort.java └── test ├── .DS_Store └── eliminateCodeFear ├── algorithms ├── BinarySearchTreeTest.java ├── BubbleSortTest.java ├── FibonacciTest.java ├── MaxIntHeapTest.java ├── MergeSortTest.java ├── MinIntHeapTest.java ├── NodeBSTTest.java ├── QuickSortTest.java ├── TrieTest.java └── graphs │ ├── BFS │ └── GraphTest.java │ ├── DFS │ ├── GraphTest.java │ └── PathFinderTest.java │ └── dijsktra │ └── ShortestPathTest.java ├── challenges ├── arraysAndStrings │ └── ArraysAndStringsTest.java ├── binaryTrees │ └── BinaryTreeTest.java ├── classics │ └── ClassicsTest.java ├── interviews │ ├── AmazonInterviewTest.java │ ├── CreditCardProcessorTest.java │ └── FaceBookInterviewTest.java ├── linkedLists │ └── LinkedListsTest.java └── stacksAndQueues │ └── StacksAndQueuesTest.java └── datastructures ├── DynamicArrayTest.java ├── HashTableTest.java ├── LinkedListTest.java ├── LinkedListWithIndexTest.java └── SingleLinkedListTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | bin 2 | .idea/* 3 | -------------------------------------------------------------------------------- /TODO.txt: -------------------------------------------------------------------------------- 1 | List of todo difficult or important questions: 2 | 3 | ### Reverse a doubly linked list 4 | https://www.hackerrank.com/challenges/reverse-a-doubly-linked-list/problem?filter=java8&filter_on=language&h_l=interview&page=1&playlist_slugs%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D=linked-lists 5 | 6 | 7 | ### Inserting a Node Into a Sorted Doubly Linked List 8 | https://www.hackerrank.com/challenges/insert-a-node-into-a-sorted-doubly-linked-list/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D=linked-lists 9 | 1. Empty list 10 | 2. At the beginning of the list 11 | 3. Somewhere at the middle of the list 12 | 4. At the end of the list 13 | 14 | -------------------------------------------------------------------------------- /lib/apiguardian-api-1.1.0-javadoc.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/lib/apiguardian-api-1.1.0-javadoc.jar -------------------------------------------------------------------------------- /lib/apiguardian-api-1.1.0-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/lib/apiguardian-api-1.1.0-sources.jar -------------------------------------------------------------------------------- /lib/apiguardian-api-1.1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/lib/apiguardian-api-1.1.0.jar -------------------------------------------------------------------------------- /lib/hamcrest-core-1.3-javadoc.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/lib/hamcrest-core-1.3-javadoc.jar -------------------------------------------------------------------------------- /lib/hamcrest-core-1.3-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/lib/hamcrest-core-1.3-sources.jar -------------------------------------------------------------------------------- /lib/hamcrest-core-1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/lib/hamcrest-core-1.3.jar -------------------------------------------------------------------------------- /lib/junit-4.12-javadoc.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/lib/junit-4.12-javadoc.jar -------------------------------------------------------------------------------- /lib/junit-4.12-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/lib/junit-4.12-sources.jar -------------------------------------------------------------------------------- /lib/junit-4.12.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/lib/junit-4.12.jar -------------------------------------------------------------------------------- /lib/opentest4j-1.2.0-javadoc.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/lib/opentest4j-1.2.0-javadoc.jar -------------------------------------------------------------------------------- /lib/opentest4j-1.2.0-sources.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/lib/opentest4j-1.2.0-sources.jar -------------------------------------------------------------------------------- /lib/opentest4j-1.2.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/lib/opentest4j-1.2.0.jar -------------------------------------------------------------------------------- /notes/Big O Common-Runtimes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/Big O Common-Runtimes.pdf -------------------------------------------------------------------------------- /notes/algo development techniques.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/algo development techniques.png -------------------------------------------------------------------------------- /notes/essential knowledge for facebook interview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/essential knowledge for facebook interview.png -------------------------------------------------------------------------------- /notes/hashing/hashmap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/hashing/hashmap.png -------------------------------------------------------------------------------- /notes/sorting/bubble-sort-in-c.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/sorting/bubble-sort-in-c.png -------------------------------------------------------------------------------- /notes/sorting/in order traversal algo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/sorting/in order traversal algo.png -------------------------------------------------------------------------------- /notes/sorting/level order traversal Breadth first.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/sorting/level order traversal Breadth first.png -------------------------------------------------------------------------------- /notes/sorting/mergeSort.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/sorting/mergeSort.gif -------------------------------------------------------------------------------- /notes/trees/Onumber of tree with array and linked list.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/trees/Onumber of tree with array and linked list.png -------------------------------------------------------------------------------- /notes/trees/in order traversal algo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/trees/in order traversal algo.png -------------------------------------------------------------------------------- /notes/trees/level order traversal Breadth first.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/trees/level order traversal Breadth first.png -------------------------------------------------------------------------------- /notes/trees/tree representation as linke list.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/trees/tree representation as linke list.png -------------------------------------------------------------------------------- /notes/trees/tree represented in array.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/trees/tree represented in array.png -------------------------------------------------------------------------------- /notes/trees/tree terminology.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/trees/tree terminology.png -------------------------------------------------------------------------------- /notes/trees/types of trees.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/notes/trees/types of trees.png -------------------------------------------------------------------------------- /src/algoBootcamp/README.md: -------------------------------------------------------------------------------- 1 | All credit of the code example in this folder goes to Jonathan Rasmusson (Instructor). 2 | 3 | He has explained the code in detain in his video tutorial on Udemy. 4 | https://www.udemy.com/data-structures-and-algorithms-bootcamp -------------------------------------------------------------------------------- /src/algoBootcamp/algo/binarysearch/App.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.algo.binarysearch; 2 | 3 | public class App { 4 | 5 | public static void main(String[] args) { 6 | System.out.println(recursiveBinarySearch(new int[] {1,2,3,4,7,9,12,18}, 0, 7, 99)); 7 | } 8 | 9 | public static int binarySearch(int [] a, int x){ 10 | int bp = 0; 11 | int ep = a.length-1; 12 | 13 | while(bp <= ep){ 14 | int mp = (bp+ep)/2; 15 | if(x < a[mp]) ep = mp-1; 16 | else if (x > a[mp]) bp = mp+1; 17 | else return mp; 18 | } 19 | return -1; 20 | } 21 | 22 | public static int recursiveBinarySearch(int [] a, int p, int r, int x){ 23 | System.out.println("[ "+ p + "..." + r + " ]"); 24 | if( p > r){ 25 | return -1; 26 | } else{ 27 | int q = (p+r)/2; 28 | if(a[q] == x){ 29 | return q; 30 | } else if (a[q] > x){ 31 | return recursiveBinarySearch(a, p, q-1, x); 32 | } else{ 33 | return recursiveBinarySearch(a, q+1, r, x); 34 | } 35 | } 36 | } 37 | 38 | public int getElementFrom(int [] a, int index){ 39 | return a[index]; 40 | } 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/algoBootcamp/algo/binarysearch/TestApp.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.algo.binarysearch; 2 | 3 | public class TestApp { 4 | 5 | public static void main(String[] args) { 6 | 7 | int myArray[] = selectionSort(new int[] {9,8,3,13,87,12,99}); 8 | 9 | for(int i =0; i < myArray.length; i++){ 10 | System.out.println(myArray[i]); 11 | } 12 | } 13 | 14 | public static int [] selectionSort(int [] a){ 15 | for(int i=0; i < a.length; i++){ 16 | int minimum = i; 17 | for(int j = i+1; j < a.length; j++){ 18 | if(a[j] < a[minimum]){ // will only enter here if we find a smaller value 19 | minimum = j; 20 | } 21 | } 22 | int temp = a[i]; 23 | a[i] = a[minimum]; 24 | a[minimum] = temp; 25 | } 26 | return a; 27 | } 28 | 29 | 30 | 31 | public void doSomeThing(int arg){ 32 | int p = arg; 33 | int i = 1; 34 | int j = 1; 35 | j++; 36 | } 37 | 38 | 39 | public int showFirst(int arg1, int arg2){ 40 | return arg1+arg2; 41 | } 42 | 43 | public int showFirst(int [] args){ 44 | return args[0]; 45 | } 46 | 47 | public int showFirstBad(int [] args){ 48 | 49 | for(int i = 0; i < args.length; i++){ 50 | int temp = args[i]; 51 | } 52 | int j = 0; 53 | return args[0]; 54 | } 55 | 56 | public void drive(int miles){ 57 | 58 | } 59 | 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/algoBootcamp/algo/binarysearch/TestInsertionSort.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.algo.binarysearch; 2 | 3 | public class TestInsertionSort { 4 | 5 | public static void main(String[] args) { 6 | int myArray[] = insertionSort(new int[] {9,8,99, 110, 8,87,637, 8,3,13,87,12,99}); 7 | 8 | for(int i =0; i < myArray.length; i++){ 9 | System.out.println(myArray[i]); 10 | } 11 | } 12 | 13 | public static int [] insertionSort(int [] a){ 14 | for(int i = 1; i < a.length; i++){ 15 | int element = a[i]; // element variable contains the data we intend on bringing over to the sorted section 16 | int j = i-1; // j variable points to the index position of the last element in the sorted section 17 | while(j >= 0 && a[j] > element){ 18 | a[j+1] = a[j]; 19 | j--; 20 | } 21 | a[j+1] = element; 22 | } 23 | return a; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/algoBootcamp/algo/insertionsort/App.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.algo.insertionsort; 2 | 3 | public class App { 4 | 5 | public static void main(String[] args) { 6 | int myArray[] = insertionSort(new int[] {9,8,99, 110, 8,87,637, 8,3,13,87,12,99}); 7 | 8 | for(int i =0; i < myArray.length; i++){ 9 | System.out.println(myArray[i]); 10 | } 11 | } 12 | 13 | public static int[] insertionSort(int [] a){ 14 | for(int i= 1; i < a.length; i++){ 15 | int element = a[i]; // element variable contains the data we intend on bringing over to the sorted area 16 | int j = i-1; // j variable points to the index position of the last value in the sorted area 17 | while(j>=0 && a[j] > element){ 18 | a[j+1] = a[j]; 19 | j--; 20 | } 21 | a[j+1] = element; 22 | } 23 | return a; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/algoBootcamp/algo/linearsearch/App.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.algo.linearsearch; 2 | 3 | public class App { 4 | 5 | public static void main(String[] args) { 6 | 7 | } 8 | 9 | public static int linearSearch(int [] a, int x){ 10 | for(int i=0; i= 0){ 12 | reduceByOne(n-1); 13 | } 14 | 15 | System.out.println("Completed Call: " + n); 16 | } 17 | 18 | 19 | public static int recursiveLinearSearch(int [] a, int i, int x){ 20 | if( i > a.length -1 ){ // if evaluates to true, x was not found in the array 21 | return -1; 22 | } else if(a[i] == x){ 23 | return i; 24 | } else{ 25 | System.out.println("index at: " + i); 26 | return recursiveLinearSearch(a, i+1, x); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/algoBootcamp/algo/selectionsort/App.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.algo.selectionsort; 2 | 3 | public class App { 4 | 5 | public static void main(String[] args) { 6 | int [] myArray = selectionSort(new int[] {9,8,3,13,87,12,99}); 7 | for(int i=0; i old first 27 | first = newNode; // first place 28 | } 29 | 30 | public void insertLast(int data){ 31 | Node newNode = new Node(); 32 | newNode.data = data; 33 | 34 | if(isEmpty()){ 35 | first = newNode; 36 | } else{ 37 | last.next = newNode; // the next value of the last node will point to the new node 38 | last = newNode; // we make the new node we created be the last one in the list 39 | } 40 | } 41 | 42 | public int deleteFirst(){ 43 | int temp = first.data; 44 | 45 | if(first.next == null){ 46 | last = null; 47 | } 48 | 49 | first = first.next; // first will point to old's next value 50 | return temp; 51 | } 52 | 53 | 54 | public void displayList(){ 55 | System.out.println("List (first --> last) "); 56 | Node current = first; 57 | while(current != null){ 58 | current.displayNode(); 59 | current = current.next; 60 | } 61 | System.out.println(); 62 | } 63 | 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/circularlinkedlist/Node.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.circularlinkedlist; 2 | 3 | public class Node { 4 | public int data; 5 | public Node next; 6 | 7 | public void displayNode(){ 8 | System.out.println("{ "+ data + " } "); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/doublylinkedlist/App.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.doublylinkedlist; 2 | 3 | public class App { 4 | 5 | public static void main(String[] args) { 6 | DoublyLinkedList theList = new DoublyLinkedList(); 7 | theList.insertFirst(22); 8 | theList.insertFirst(44); 9 | theList.insertFirst(66); 10 | theList.insertLast(11); 11 | theList.insertLast(33); 12 | theList.insertLast(55); 13 | theList.displayForward(); 14 | theList.displayBackward(); 15 | theList.deleteFirst(); 16 | theList.deleteLast(); 17 | theList.deleteKey(11); 18 | theList.displayForward(); 19 | theList.insertAfter(22, 77); 20 | theList.insertAfter(33, 88); 21 | theList.displayForward(); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/doublylinkedlist/Node.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.doublylinkedlist; 2 | 3 | public class Node { 4 | public int data; 5 | public Node next; 6 | public Node previous; 7 | 8 | public void displayNode(){ 9 | System.out.print("{ "+ data + " } "); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/graph/App.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.graph; 2 | 3 | public class App { 4 | 5 | public static void main(String[] args) { 6 | 7 | BetterGraph myGraph = new BetterGraph(5, "directed"); 8 | myGraph.addVertex("State"); 9 | myGraph.addVertex("Avenel"); 10 | myGraph.addVertex("Elm"); 11 | myGraph.addVertex("Pocono"); 12 | myGraph.addVertex("William"); 13 | 14 | myGraph.addEdge("Avenel", "Pocono"); 15 | myGraph.addEdge("State", "Elm"); 16 | myGraph.addEdge("Elm", "Avenel"); 17 | myGraph.addEdge("Elm", "William"); 18 | myGraph.addEdge("William", "State"); 19 | myGraph.addEdge("William", "Pocono"); 20 | myGraph.addEdge("Pocono", "Elm"); 21 | myGraph.addEdge("State", "Avenel"); 22 | 23 | myGraph.print(); 24 | 25 | // Object [] values = myGraph.adj(1); 26 | // for(Object val : values){ 27 | // System.out.println(val); 28 | // } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/graph/Graph.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.graph; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Graph { 6 | private int vCount; // number of vertices 7 | private int eCount; // Number of edges 8 | 9 | private ArrayList[] adjacents; // array of integer lists 10 | 11 | public Graph(int vCount){ 12 | this.vCount = vCount; 13 | this.eCount = 0; 14 | adjacents = new ArrayList [vCount]; 15 | 16 | for(int i = 0; i < vCount; i++){ 17 | adjacents[i] = new ArrayList(); 18 | } 19 | } 20 | 21 | public int getVertexCount(){ 22 | return vCount; 23 | } 24 | 25 | public int getEdgeCount(){ 26 | return eCount; 27 | } 28 | 29 | public void addEdge(int src, int dest){ 30 | adjacents[src].add(dest); 31 | eCount++; 32 | } 33 | 34 | public Object[] adj(int src){ 35 | return adjacents[src].toArray(); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/heapimplementation/Application.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.heapimplementation; 2 | 3 | public class Application { 4 | 5 | public static void main(String[] args) { 6 | 7 | Heap newHeap = new Heap(10); 8 | newHeap.insert(10); 9 | newHeap.insert(12); 10 | newHeap.insert(42); 11 | newHeap.insert(35); 12 | newHeap.insert(16); 13 | newHeap.insert(88); 14 | newHeap.insert(42); 15 | newHeap.insert(7); 16 | 17 | // The assignment that follows will complete the body of the method used below. 18 | newHeap.displayHeap(); 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/heapimplementation/Node.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.heapimplementation; 2 | 3 | public class Node { 4 | private int key; 5 | 6 | public Node(int key) { 7 | super(); 8 | this.key = key; 9 | } 10 | 11 | public int getKey() { 12 | return key; 13 | } 14 | 15 | public void setKey(int key) { 16 | this.key = key; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/queue/App.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.queue; 2 | 3 | public class App { 4 | 5 | public static void main(String[] args) { 6 | Queue myQueue = new Queue(5); 7 | myQueue.insert(100); 8 | myQueue.insert(1000); 9 | myQueue.insert(14); 10 | myQueue.insert(12); 11 | myQueue.insert(44); 12 | myQueue.insert(99); 13 | myQueue.insert(99); 14 | myQueue.insert(99); 15 | myQueue.insert(99); 16 | myQueue.insert(99999); 17 | myQueue.view(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/singlylinkedlist/App.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.singlylinkedlist; 2 | 3 | public class App { 4 | 5 | public static void main(String[] args) { 6 | SinglyLinkedList mylist = new SinglyLinkedList(); 7 | mylist.insertFirst(100); 8 | mylist.insertFirst(50); 9 | mylist.insertFirst(99); 10 | mylist.insertFirst(88); 11 | mylist.insertLast(9999999); 12 | 13 | mylist.displayList(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/singlylinkedlist/Node.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.singlylinkedlist; 2 | 3 | public class Node { 4 | public int data; 5 | public Node next; 6 | 7 | public void displayNode(){ 8 | System.out.println("{ "+ data + " } "); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/singlylinkedlist/SinglyLinkedList.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.singlylinkedlist; 2 | 3 | public class SinglyLinkedList { 4 | private Node first; 5 | private Node last; 6 | public SinglyLinkedList(){ 7 | 8 | } 9 | 10 | public boolean isEmpty(){ 11 | return (first == null); 12 | } 13 | 14 | // used to insert at the beginning of the list 15 | public void insertFirst(int data){ 16 | Node newNode = new Node(); 17 | newNode.data = data; 18 | newNode.next = first; 19 | first = newNode; 20 | } 21 | 22 | public Node deleteFirst(){ 23 | Node temp = first; 24 | first = first.next; 25 | return temp; 26 | } 27 | 28 | public void displayList(){ 29 | System.out.println("List (first --> last) "); 30 | Node current = first; 31 | while(current != null){ 32 | current.displayNode(); 33 | current = current.next; 34 | } 35 | System.out.println(); 36 | } 37 | 38 | public void insertLast(int data){ 39 | Node current = first; 40 | while(current.next != null){ 41 | current = current.next; // we'll loop until current.next is null 42 | } 43 | Node newNode = new Node(); 44 | newNode.data = data; 45 | current.next = newNode; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/stack/App.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.stack; 2 | 3 | public class App { 4 | 5 | public static void main(String[] args) { 6 | Stack theStack = new Stack(3); 7 | // theStack.push(20); 8 | // theStack.push(40); 9 | // theStack.push(60); 10 | // theStack.push(80); 11 | //// 12 | // while(!theStack.isEmpty()){ 13 | // long value = theStack.pop(); 14 | // System.out.println(value); 15 | // } 16 | 17 | System.out.println(reverseString("Imtiaz")); 18 | } 19 | 20 | public static String reverseString(String str){ 21 | int stackSize = str.length(); // get the max stack size 22 | Stack theStack = new Stack(stackSize); // we make the stack 23 | for(int j = 0; j < str.length(); j++){ 24 | char ch = str.charAt(j); // getting a char from the input string 25 | theStack.push(ch); 26 | } 27 | 28 | String result = ""; 29 | while(!theStack.isEmpty()){ 30 | char ch = theStack.pop(); 31 | result = result+ ch; // appending to the output 32 | } 33 | 34 | return result; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/algoBootcamp/ds/stack/Stack.java: -------------------------------------------------------------------------------- 1 | package algoBootcamp.ds.stack; 2 | 3 | public class Stack { 4 | 5 | private int maxSize; 6 | private char[] stackArray; 7 | private int top; 8 | 9 | public Stack(int size){ 10 | this.maxSize = size; 11 | this.stackArray = new char[maxSize]; 12 | this.top = -1; 13 | } 14 | 15 | public void push(char j){ 16 | if(isFull()){ 17 | System.out.println(" this stack is already full"); 18 | }else{ 19 | top++; 20 | stackArray[top] = j; 21 | } 22 | 23 | } 24 | 25 | public char pop(){ 26 | if(isEmpty()){ 27 | System.out.println("the stack is already empty"); 28 | return 'O'; 29 | } else{ 30 | int old_top = top; 31 | top--; 32 | return stackArray[old_top]; 33 | } 34 | } 35 | 36 | public char peak(){ 37 | return stackArray[top]; 38 | } 39 | 40 | public boolean isEmpty(){ 41 | return (top == -1); 42 | } 43 | 44 | public boolean isFull(){ 45 | return (maxSize-1 == top); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/codechallenge/easy/BubbleSort.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | import org.junit.Test; 4 | 5 | /** 6 | * Implement bubble sort 7 | */ 8 | public class BubbleSort { 9 | 10 | @Test 11 | public void test(){ 12 | int[] a = {40,20,60,10,100,5}; 13 | bubbleSort(a); 14 | for (int i = 0; i < a.length-1; i++) { 15 | if (a[i] > a[i+1]){ 16 | assert false; 17 | } 18 | } 19 | } 20 | 21 | void bubbleSort(int[] a){ 22 | 23 | for (int j = 0; j < a.length; j++) { 24 | for (int i = 0; i < a.length-1-j; i++) { 25 | if (a[i] > a[i + 1]) { 26 | int swap = a[i]; 27 | a[i + 1] = a[i]; 28 | a[i] = swap; 29 | } 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/codechallenge/easy/CapatilizeFirstLetterOfWords.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Have the function LetterCapitalize(str) take the str parameter being passed and capitalize the first letter of each word. 8 | * Words will be separated by only one space. 9 | */ 10 | public class CapatilizeFirstLetterOfWords { 11 | 12 | @Test 13 | public void test(){ 14 | Assert.assertEquals("Hello World Hack It", main("hello world hack it")); 15 | } 16 | 17 | 18 | public String main(String str) { 19 | String finalStr=""; 20 | 21 | // split it by regex 22 | String[] sa = str.split("\\s+"); 23 | 24 | // iterate over and replace each first charater by doing to Upper function 25 | for(int i=0; i < sa.length; i++){ 26 | String s = sa[i]; 27 | String fc = String.valueOf(s.charAt(0)).toUpperCase(); 28 | finalStr += fc + s.substring(1) +" "; 29 | } 30 | return finalStr.trim(); 31 | } 32 | 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/codechallenge/easy/CheckNums.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /* 7 | Challenge 8 | Have the function CheckNums(num1,num2) take both parameters being passed and 9 | return the string true if num2 is greater than num1, otherwise return the string false. 10 | If the parameter values are equal to each other then return the string -1. 11 | Sample Test Cases 12 | Input:3 & num2 = 122 13 | Output:true 14 | 15 | 16 | Input:67 & num2 = 67 17 | 18 | Output:-1 19 | 20 | */ 21 | public class CheckNums { 22 | 23 | @Test 24 | public void test(){ 25 | Assert.assertEquals("true", doCompare(2,5)); 26 | Assert.assertEquals("-1", doCompare(5,5)); 27 | Assert.assertEquals("false", doCompare(8,5)); 28 | } 29 | 30 | String doCompare(int num1, int num2) { 31 | return (num1 == num2 ? "-1" : String.valueOf(num2 > num1)); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/codechallenge/easy/ConvertLinkedListToArray.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.LinkedList; 6 | 7 | /* 8 | Convert Linked list into Array 9 | */ 10 | public class ConvertLinkedListToArray { 11 | 12 | // Test Driven Development by Aseem Jain 13 | @Test 14 | public void test() { 15 | 16 | LinkedList ll = new LinkedList(); 17 | ll.add("first"); 18 | ll.add("second"); 19 | ll.add("third"); 20 | ll.add("last"); 21 | 22 | 23 | String[] arr = convert(ll); 24 | assert 4 == arr.length; 25 | assert "first" == arr[0]; 26 | assert "last" == arr[3]; 27 | 28 | } 29 | 30 | String[] convert(LinkedList ll) { 31 | String[] arr = new String[ll.size()]; 32 | int i = 0; 33 | for (String s : ll) { 34 | arr[i++] = s; 35 | } 36 | return arr; 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/codechallenge/easy/LogN.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Calculate the run time based on Log(n) for an algo. 8 | * Expectation is to get nearest whole number. 9 | */ 10 | public class LogN { 11 | // Test Driven Development by Aseem Jain 12 | @Test 13 | public void test() { 14 | 15 | Assert.assertEquals(6, method(64)); 16 | Assert.assertEquals(6, method(65)); 17 | Assert.assertEquals(5, method(60)); 18 | Assert.assertEquals(1, method(2)); 19 | Assert.assertEquals(0, method(1)); 20 | 21 | Assert.assertEquals(6, methodR(64,1)); 22 | Assert.assertEquals(6, methodR(65,1)); 23 | Assert.assertEquals(5, methodR(60,1)); 24 | Assert.assertEquals(1, methodR(2,1)); 25 | Assert.assertEquals(0, methodR(1,1)); 26 | 27 | } 28 | 29 | int method(int n) { 30 | int c = 0; 31 | while (n > 1) { 32 | c++; 33 | n = n / 2; 34 | 35 | } 36 | return c; 37 | } 38 | 39 | int methodR(int n, Integer c){ 40 | // if(n<=1) { 41 | // return 0; 42 | // } 43 | // return c + methodR(n/2,c); 44 | 45 | return n <= 1 ? 0 : c + methodR(n/2,1); 46 | 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/codechallenge/easy/PowerOf.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.util.ArrayList; 7 | import java.util.LinkedList; 8 | import java.util.List; 9 | import java.util.Queue; 10 | 11 | /* 12 | 13 | calculate x to the power n 14 | using iterative and recursive approach. 15 | 16 | */ 17 | public class PowerOf { 18 | // Test Driven Development by Aseem Jain 19 | @Test 20 | public void test() { 21 | 22 | // iterative 23 | assert 4 == Math.pow(2,2); 24 | assert 64 == Math.pow(2, 6); 25 | assert 1000 == Math.pow(10, 3); 26 | 27 | // iterative 28 | Assert.assertEquals(4, method(2, 2)); 29 | Assert.assertEquals(64, method(2, 6)); 30 | Assert.assertEquals(1000, method(10, 3)); 31 | 32 | // recursive 33 | Assert.assertEquals(64, methodR(2, 6)); 34 | Assert.assertEquals(64, method(2, 6)); 35 | Assert.assertEquals(1000, methodR(10, 3)); 36 | } 37 | 38 | int method(int x, int n) { 39 | if (n == 0) { 40 | return 1; 41 | } 42 | 43 | int ans = 1; 44 | for (int i = 1; i <= n; i++) { 45 | ans = ans * x; 46 | } 47 | return ans; 48 | } 49 | 50 | 51 | int methodR(int x, int n){ 52 | // if (n == 0) { 53 | // return 1; 54 | // } 55 | // return x * methodR(x, n-1); 56 | 57 | return n == 0 ? 1 : x * methodR(x,n-1); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/codechallenge/easy/RemoveAdjacentDuplicate.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /* 7 | Given a string s, recursively remove adjacent duplicate characters from the string s. 8 | The output string should not have any adjacent duplicates. 9 | 10 | */ 11 | public class RemoveAdjacentDuplicate { 12 | 13 | // Test Driven Development by Aseem Jain 14 | @Test 15 | public void test() { 16 | 17 | assert "acac" == sol("acaaabbbacdddd",0); 18 | 19 | } 20 | 21 | private String sol(String s, int n) { 22 | 23 | // base case 24 | if(n == s.length()-1){ 25 | return ""; 26 | } 27 | 28 | // expected char after comparision 29 | String c = s.charAt(n) == s.charAt(n+1) ? "" : s.charAt(n) + ""; 30 | 31 | System.out.print(c); 32 | // recursion 33 | return c + sol(s, n+1); 34 | } 35 | 36 | // private String rec(char[] sa, int i){ 37 | // if(i == 1){ 38 | // 39 | // } 40 | // } 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/codechallenge/easy/SimpleAddition.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Tags: math fundamentals | Difficulty: Easy 8 | * Challenge 9 | * Have the function SimpleAdding(num) add up all the numbers from 1 to num. 10 | * For example: if the input is 4 then your program should return 10 because 1 + 2 + 3 + 4 = 10. For the test cases, the parameter num will be any number from 1 to 1000. 11 | * 12 | * Sample Test Cases 13 | * Input:12 14 | * Output:78 15 | * 16 | * 17 | * Input:140 18 | * Output:9870 19 | */ 20 | public class SimpleAddition { 21 | 22 | @Test 23 | public void t(){ 24 | Assert.assertEquals(78,SimpleAddingWithLoop(12)); 25 | Assert.assertEquals(9870,SimpleAddingWithLoop(140)); 26 | 27 | Assert.assertEquals(78,addWithRecursion(12)); 28 | Assert.assertEquals(9870,addWithRecursion(140)); 29 | 30 | } 31 | 32 | int SimpleAddingWithLoop(int num){ 33 | int sum =0; 34 | for(int i=1; i<=num; i++){ 35 | sum += i; 36 | } 37 | return sum; 38 | } 39 | 40 | int addWithRecursion(int num){ 41 | if (num <= 1 ){ 42 | return 1; 43 | } 44 | return num + addWithRecursion(num -1); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/codechallenge/easy/Sort.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | /* 4 | Implement a sort algo given Arrays.sort() or Collections.sort() is not provided 5 | 6 | */ 7 | import org.junit.Test; 8 | 9 | public class Sort { 10 | 11 | @Test 12 | public void t(){ 13 | int[] arr = {5,3,6,2,9,6,}; 14 | sort(arr); 15 | for (int i = 1; i < arr.length; i++) { 16 | System.out.println(arr[i]); 17 | if (arr[i-1]>arr[i]){ 18 | assert false; 19 | } 20 | } 21 | } 22 | 23 | public void sort(int[]a) { 24 | 25 | for (int i=0; i < a.length; i++) { 26 | for (int j=0; j < a.length - i - 1; j++) { 27 | if (a[j] > a[j + 1]) { 28 | int swap = a[j]; 29 | a[j] = a[j + 1]; 30 | a[j + 1] = swap; 31 | } 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/codechallenge/easy/SumAndProduct.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | /** 4 | * separate the digits in given number and find the different between 5 | * the product and sum of all the digits in given number. 6 | */ 7 | public class SumAndProduct { 8 | 9 | public static void main(String[] args) { 10 | 11 | int num = 3452; 12 | 13 | int sum = 0; 14 | int prod = 1; 15 | String sn = "" + num; 16 | for (int i = 0; i < sn.length(); i++) { 17 | Integer numberAt = Integer.parseInt(sn.charAt(i) + ""); 18 | sum += numberAt; 19 | prod *= numberAt; 20 | 21 | } 22 | 23 | int ans = prod - sum; 24 | 25 | System.out.println("Number of digits: " + ans); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/codechallenge/easy/TimeConverter.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /* 7 | Challenge 8 | Have the function TimeConvert(num) take the num parameter being passed and return the number of hours and minutes the parameter converts to 9 | (ie. if num = 63 then the output should be 1:3). Separate the number of hours and minutes with a colon. 10 | Sample Test Cases 11 | Input:126 12 | Output:2:6 13 | 14 | Input:45 15 | Output:0:45 16 | */ 17 | public class TimeConverter { 18 | 19 | @Test 20 | public void t(){ 21 | Assert.assertEquals("2:6",convert(126)); 22 | Assert.assertEquals("0:45",convert(45)); 23 | Assert.assertEquals("2:30",convert(150)); 24 | } 25 | 26 | String convert(int a) { 27 | int m = a%60; 28 | int h = a/60; 29 | return h+":"+m; 30 | } 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /src/codechallenge/easy/URLConverter.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | // Challenge: See if you can replaces all the spaces in a a string with the 4 | // ASCII symbol for space '%20'. Assume you are given the length of the final 5 | 6 | import org.junit.Assert; 7 | import org.junit.Test; 8 | 9 | public class URLConverter { 10 | 11 | @Test 12 | public void test(){ 13 | Assert.assertEquals("My%20Home%20Page", urlify("My Home Page ", 16)); 14 | 15 | } 16 | 17 | String urlify(String word, int max){ 18 | 19 | word = word.trim(); 20 | StringBuilder sb = new StringBuilder(); 21 | char blank = " ".charAt(0); 22 | for (int i = 0; i < word.length(); i++) { 23 | Character cc = word.charAt(i); 24 | if( cc == blank){ 25 | sb.append("%20"); 26 | }else{ 27 | sb.append(cc); 28 | } 29 | } 30 | return sb.toString(); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/codechallenge/easy/UniqueChar.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.util.HashSet; 7 | import java.util.Set; 8 | 9 | public class UniqueChar { 10 | 11 | // Find the word is made of only unique characters. 12 | 13 | @Test 14 | public void test(){ 15 | String input1 = "aseem"; boolean output1 = false; 16 | String input2 = "asdfgh"; boolean output2 = true; 17 | Assert.assertFalse(areAllCharsUnique(input1)); 18 | Assert.assertTrue(areAllCharsUnique(input2)); 19 | } 20 | 21 | boolean areAllCharsUnique(String word){ 22 | 23 | Set chars = new HashSet<>(); 24 | for (int i = 0; i < word.length(); i++) { 25 | Character current = word.charAt(i); 26 | if(chars.contains(current)){ 27 | return false; 28 | } 29 | chars.add(current); 30 | } 31 | return true; 32 | } 33 | 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/codechallenge/easy/array/IsArrayInSequence.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy.array; 2 | 3 | import me.premaseem.MyUtils; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | 8 | /* 9 | Challenge: find out if given array is in sequence using recursion 10 | */ 11 | public class IsArrayInSequence { 12 | 13 | // Test Driven Development by Aseem Jain 14 | @Test 15 | public void test() { 16 | 17 | Integer[] seq1 = {3,4,5,6,7,8}; 18 | Integer[] seq2 = {3,5,6,7,8}; 19 | 20 | assert isSeq(seq1,seq1.length-1); 21 | assert ! isSeq(seq2, seq2.length-1); 22 | 23 | } 24 | 25 | private boolean isSeq(Integer[] seq, int i) { 26 | if(i==0){ 27 | return true; 28 | } 29 | System.out.println("checking for index "+ i ); 30 | return seq[i] - 1 == seq[i-1] && isSeq(seq, i-1); 31 | } 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/codechallenge/easy/array/Merge2ArraysInsort.java: -------------------------------------------------------------------------------- 1 | package codechallenge.easy.array; 2 | 3 | /* 4 | Merge 2 sorted arrays of different lengths and final array should be in sorted order 5 | */ 6 | 7 | import me.premaseem.MyUtils; 8 | import org.junit.Test; 9 | 10 | public class Merge2ArraysInsort { 11 | 12 | // Test Driven Development by Aseem Jain 13 | @Test 14 | public void test() { 15 | 16 | int a1[] = {20, 20, 50, 100}; 17 | int a2[] = {10, 30, 90, 100,110,150}; 18 | 19 | int[] ints = mergeArrayAndsort(a2, a1); 20 | MyUtils.isArrSorted(ints); 21 | } 22 | 23 | int[] mergeArrayAndsort(int[] a, int[] b) { 24 | 25 | int[] c = new int[a.length + b.length]; 26 | 27 | int i1=0,i2=0; 28 | for (int i = 0; i < c.length; i++) { 29 | 30 | int ae=0; int be=0; 31 | if(i1 < a.length){ 32 | ae= a[i1]; 33 | } else{ 34 | while(i2 11 | * EXAMPLE 12 | * Input: Tact Coa 13 | * Output: True (permutations: "taco cat", "atco eta", etc.) 14 | */ 15 | 16 | public class Palindrome { 17 | // Test Driven Development by Aseem Jain 18 | @Test 19 | public void test() { 20 | 21 | assert solution1("Rats live on no evil star"); 22 | assert !solution1("Ha*K"); 23 | 24 | assert solution2("Rats live on no evil star"); 25 | assert !solution2("Ha*K"); 26 | 27 | } 28 | 29 | // Basic solution 30 | private boolean solution1(String s) { 31 | s = s.trim().toLowerCase(); 32 | for (int i = 0; i < s.length()/2; i++) { 33 | if(s.charAt(i) != s.charAt(s.length()-1-i)){ 34 | return false; 35 | } 36 | } 37 | return true; 38 | } 39 | 40 | private boolean solution2(String s) { 41 | s = s.trim().toLowerCase(); 42 | String s1 =""; 43 | String s2 =""; 44 | for (int i = 0; i < s.length()/2; i++) { 45 | s1 += s.charAt(i); 46 | s2 += s.charAt(s.length()-1-i); 47 | } 48 | return s1.equalsIgnoreCase(s2); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/crackingTheCodingInterview/chap1/StringRotation.java: -------------------------------------------------------------------------------- 1 | package crackingTheCodingInterview.chap1; 2 | 3 | import org.junit.Test; 4 | 5 | /** 6 | * ### String Rotation: 7 | * Assume you have a method isSubstringwhich checks if one word is a substring of another. 8 | * Given two strings, sl and s2, write code to check if s2 is a rotation of sl using only one call to isSubstring 9 | * (e.g.,"waterbottle" is a rotation of "erbottlewat"). 10 | */ 11 | public class StringRotation { 12 | // Test Driven Development by Aseem Jain 13 | @Test 14 | public void test() { 15 | 16 | assert solution1("waterbottle", "erbottlewat"); 17 | assert solution1("apple", "pleap"); 18 | assert !solution1("camera", "macera"); 19 | 20 | } 21 | 22 | // Basic solution 23 | private boolean solution1(String s1, String s2) { 24 | if (s1.length() != s2.length()) { 25 | return false; 26 | } 27 | 28 | String dobuleString = s1 + s1; 29 | // make sure s2 is part of s1 30 | return dobuleString.contains(s2); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/crackingTheCodingInterview/chap2/DeleteMiddleNode.java: -------------------------------------------------------------------------------- 1 | package crackingTheCodingInterview.chap2; 2 | 3 | import me.premaseem.myLib.MySLLNode; 4 | import org.junit.Test; 5 | 6 | /** 7 | * ### Delete Middle Node: 8 | * Implement an algorithm to delete a node in the middle 9 | * (i.e., any node but the first and last node, not necessarily the exact middle) 10 | * of a singly linked list, given only access to that node. 11 | * 12 | * EXAMPLE 13 | * lnput:the node c from the linked lista->b->c->d->e->f 14 | * Result: nothing is returned, but the new linked list looks likea->b->d->e- >f 15 | * 16 | */ 17 | public class DeleteMiddleNode { 18 | 19 | // Test Driven Development by Aseem Jain 20 | @Test 21 | public void test() { 22 | 23 | // input 24 | int[] in = {2, 4, 14, 5, 6, 6, 8, 2}; 25 | MySLLNode head = new MySLLNode(in); 26 | 27 | // output 28 | assert 4 == solution1(head).data; 29 | 30 | } 31 | 32 | // Basic solution 33 | private MySLLNode solution1(MySLLNode head) { 34 | 35 | if(head.next != null){ 36 | head.data = head.next.data; 37 | head.next = head.next.next; 38 | } 39 | 40 | return head; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/crackingTheCodingInterview/chap2/PalindromeDoubleLinkedList.java: -------------------------------------------------------------------------------- 1 | package crackingTheCodingInterview.chap2; 2 | 3 | import me.premaseem.myLib.MyDLLNode; 4 | import me.premaseem.myLib.MySLLNode; 5 | import org.junit.Test; 6 | 7 | /** 8 | * ### Palindrome: 9 | * Implement a function to check if a linked list is a palindrome. 10 | */ 11 | public class PalindromeDoubleLinkedList { 12 | // Test Driven Development by Aseem Jain 13 | @Test 14 | public void test() { 15 | 16 | // input 17 | int[] in = {1,2,3,3,2,1}; 18 | MyDLLNode head = new MyDLLNode(in); 19 | assert solution1(head); 20 | 21 | } 22 | 23 | // Basic solution 24 | private boolean solution1(MyDLLNode head) { 25 | if (head == null) { 26 | return false; // depends on our definition of a palindrome. 27 | } 28 | MyDLLNode c = head; 29 | while(c.next != null){ 30 | c = c.next; 31 | } 32 | MyDLLNode tail = c; 33 | MyDLLNode fast = head; 34 | 35 | int count = 0; 36 | // find out tail and iterate it till half of the time 37 | while(head != null && tail != null && fast!= null && fast.next != null){ 38 | if (head.data != tail.data){ 39 | return false; 40 | } 41 | 42 | head = head.next; 43 | tail = tail.prev; 44 | fast = fast.next.next; 45 | System.out.println(count++); 46 | } 47 | 48 | 49 | return true; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/crackingTheCodingInterview/chap2/ReturnKToLast.java: -------------------------------------------------------------------------------- 1 | package crackingTheCodingInterview.chap2; 2 | 3 | import me.premaseem.myLib.MyDLLNode; 4 | import org.junit.Test; 5 | 6 | /** 7 | ### Return Kth to Last: 8 | Implement an algorithm to find the kth to last element of a singly linked list. 9 | 10 | */ 11 | public class ReturnKToLast { 12 | 13 | // Test Driven Development by Aseem Jain 14 | @Test 15 | public void test() { 16 | 17 | int[] in = {2, 4, 4, 5, 9, 6, 8, 2}; 18 | MyDLLNode head = new MyDLLNode(in); 19 | 20 | // get 3rd from last 21 | assert solution1(head,2).data == 8; 22 | assert solution2(head,2).data == 8; 23 | 24 | } 25 | 26 | // Basic solution 27 | private MyDLLNode solution1(MyDLLNode head, int k) { 28 | 29 | // count total number in one loop. 30 | // iterate for last - k to return element 31 | MyDLLNode c = head; 32 | int len = 0; 33 | while (c != null){ 34 | len++; 35 | c = c.next; 36 | } 37 | 38 | System.out.println("the total leng is " + len); 39 | 40 | c = head; 41 | for (int i = 1; i <= len -k; i++) { 42 | c = c.next; 43 | } 44 | return c; 45 | } 46 | 47 | private MyDLLNode solution2(MyDLLNode head, int k) { 48 | MyDLLNode c = head; 49 | while (c.next != null){ 50 | c = c.next; 51 | } 52 | 53 | for (int i = 1; i < k ; i++) { 54 | c = c.prev; 55 | } 56 | 57 | return c; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/dsGuy/AVLTree/Main.java: -------------------------------------------------------------------------------- 1 | package dsGuy.AVLTree; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | // Constructor 8 | AVLTree tree = new AVLTree(); 9 | 10 | // Insert values in AVL Tree 11 | 12 | 13 | tree.insert(30); 14 | 15 | 16 | tree.insert(10); 17 | tree.insert(5); 18 | tree.insert(3); 19 | tree.insert(4); 20 | tree.insert(50); 21 | tree.insert(65); 22 | tree.insert(1); 23 | 24 | tree.levelOrderTraversal(); 25 | tree.printTreeGraphically(); 26 | 27 | tree.deleteNodeOfBST(5);//LL Condition 28 | tree.printTreeGraphically(); 29 | 30 | tree.insert(2); 31 | tree.printTreeGraphically(); 32 | 33 | tree.deleteNodeOfBST(4);//LR Condition 34 | tree.printTreeGraphically(); 35 | 36 | tree.insert(20); 37 | tree.deleteNodeOfBST(65);//RR Condition 38 | tree.printTreeGraphically(); 39 | 40 | tree.insert(40); 41 | tree.deleteNodeOfBST(20);//RL Condition 42 | tree.printTreeGraphically(); 43 | 44 | }// end of method 45 | }// end of class 46 | -------------------------------------------------------------------------------- /src/dsGuy/README.md: -------------------------------------------------------------------------------- 1 | All credit of the code example for this folder goes to DS guy (Instructor). 2 | 3 | He has explained the code in detain in his video tutorial on Udemy. 4 | https://www.udemy.com/course/learn-data-structure-algorithms-with-java-interview/ 5 | -------------------------------------------------------------------------------- /src/dsGuy/bfs/BFSByAdjacencyMatrixMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.bfs; 2 | 3 | import java.util.ArrayList; 4 | 5 | import dsGuy.node.*; 6 | 7 | public class BFSByAdjacencyMatrixMain { 8 | 9 | public static void main(String[] args) { 10 | 11 | 12 | //Will store Nodes in this List 13 | ArrayList nodeList = new ArrayList<>(); 14 | 15 | 16 | //Create 10 nodes: V1-V10 17 | for(int i=1;i<11; i++) { 18 | nodeList.add(new GraphNode("V"+i,i-1)); 19 | } 20 | 21 | 22 | //Pass Graph arraylist for further processing 23 | BFSByAdjacencyMatrix graph = new BFSByAdjacencyMatrix(nodeList); 24 | 25 | 26 | //Add edges in graph 27 | graph.addUndirectedEdge(1,2); 28 | graph.addUndirectedEdge(1,4); 29 | graph.addUndirectedEdge(2,3); 30 | graph.addUndirectedEdge(2,5); 31 | graph.addUndirectedEdge(3,6); 32 | graph.addUndirectedEdge(3,10); 33 | graph.addUndirectedEdge(4,7); 34 | graph.addUndirectedEdge(5,8); 35 | graph.addUndirectedEdge(6,9); 36 | graph.addUndirectedEdge(7,8); 37 | graph.addUndirectedEdge(8,9); 38 | graph.addUndirectedEdge(9,10); 39 | 40 | //bfs from v1 41 | System.out.println("Printing BFS from source: V1"); 42 | graph.bfs(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/dsGuy/bfs/BFSByLinkedListMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.bfs; 2 | import java.util.ArrayList; 3 | import dsGuy.node.*; 4 | 5 | public class BFSByLinkedListMain { 6 | 7 | public static void main(String[] args) { 8 | 9 | //Initialize a Arraylist for storing all the graph nodes 10 | ArrayList nodeList = new ArrayList<>(); 11 | 12 | 13 | //create 10 nodes: v1-v10 14 | for(int i=1;i<11; i++) { 15 | nodeList.add(new GraphNode("V"+i)); 16 | } 17 | 18 | 19 | //Constructor 20 | BFSByLinkedList graph = new BFSByLinkedList(nodeList); 21 | 22 | 23 | //add edges 24 | graph.addUndirectedEdge(1,2); 25 | graph.addUndirectedEdge(1,4); 26 | graph.addUndirectedEdge(2,3); 27 | graph.addUndirectedEdge(2,5); 28 | graph.addUndirectedEdge(3,6); 29 | graph.addUndirectedEdge(3,10); 30 | graph.addUndirectedEdge(4,7); 31 | graph.addUndirectedEdge(5,8); 32 | graph.addUndirectedEdge(6,9); 33 | graph.addUndirectedEdge(7,8); 34 | graph.addUndirectedEdge(8,9); 35 | graph.addUndirectedEdge(9,10); 36 | 37 | 38 | //bfs from v1 39 | System.out.println("Printing BFS from source: V1"); 40 | graph.bfs(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/dsGuy/bfs/PathFindingByBFSMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.bfs; 2 | 3 | import java.util.ArrayList; 4 | 5 | import dsGuy.node.*; 6 | 7 | public class PathFindingByBFSMain { 8 | 9 | public static void main(String[] args) { 10 | 11 | ArrayList nodeList = new ArrayList<>(); 12 | 13 | //create 10 nodes: v1-v10 14 | for(int i=0;i<10; i++) { 15 | nodeList.add(new GraphNode(""+i)); 16 | } 17 | 18 | //Constructor 19 | PathFindingByBFS graph = new PathFindingByBFS(nodeList); 20 | //add edges following graph in graph.docx 21 | graph.addUndirectedEdge(0,8); 22 | graph.addUndirectedEdge(8,2); 23 | graph.addUndirectedEdge(8,9); 24 | graph.addUndirectedEdge(2,1); 25 | graph.addUndirectedEdge(9,1); 26 | graph.addUndirectedEdge(2,4); 27 | graph.addUndirectedEdge(1,3); 28 | graph.addUndirectedEdge(1,7); 29 | graph.addUndirectedEdge(3,4); 30 | graph.addUndirectedEdge(3,5); 31 | graph.addUndirectedEdge(7,6); 32 | graph.addUndirectedEdge(5,6); 33 | 34 | System.out.println("Printing BFS from source: 2"); 35 | graph.BFSForSSSP(nodeList.get(2)); 36 | }//end of method 37 | 38 | }//end of class 39 | -------------------------------------------------------------------------------- /src/dsGuy/binarySearchTree/BinarySearchTreeByLinkedListMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.binarySearchTree; 2 | 3 | public class BinarySearchTreeByLinkedListMain { 4 | 5 | public static void main(String[] args) { 6 | 7 | //Constructor 8 | BinarySearchTreeByLinkedList tree = new BinarySearchTreeByLinkedList(); 9 | 10 | //Inserting values in BST 11 | System.out.println("Inserting 10 nodes in Tree"); 12 | tree.insert(100); 13 | tree.insert(80); 14 | tree.insert(70); 15 | tree.insert(90); 16 | tree.insert(50); 17 | 18 | 19 | 20 | tree.printTreeGraphically(); 21 | 22 | //Searching non-existing value in Tree 23 | System.out.println("\n\nSearching for value on BST..."); 24 | tree.searchForValue(80); 25 | 26 | //Searching existing value in Tree 27 | System.out.println("\nSearching for value on BST..."); 28 | tree.searchForValue(60); 29 | 30 | 31 | //Deleting Node from Tree 32 | tree.deleteNodeOfBST(80); //Node does not exists 33 | tree.printTreeGraphically(); 34 | 35 | /*tree.deleteNodeOfBST(57); //Node is having 0 Child 36 | tree.printTreeGraphically(); 37 | 38 | tree.deleteNodeOfBST(60); //Node is having 1 Child 39 | tree.printTreeGraphically(); 40 | 41 | tree.deleteNodeOfBST(50); //Node is having 2 Child 42 | tree.printTreeGraphically(); 43 | 44 | //Deleting entire Tree 45 | tree.deleteTree(); 46 | 47 | //Traversing Tree 48 | tree.levelOrderTraversal(); 49 | */ 50 | }//end of method 51 | 52 | }//end of class -------------------------------------------------------------------------------- /src/dsGuy/dfs/DFSIterative.java: -------------------------------------------------------------------------------- 1 | package dsGuy.dfs; 2 | import java.util.*; 3 | import dsGuy.node.GraphNode; 4 | 5 | public class DFSIterative { 6 | ArrayList nodeList = new ArrayList(); 7 | 8 | 9 | public DFSIterative(ArrayList nodeList) { 10 | 11 | this.nodeList = nodeList; 12 | } 13 | 14 | 15 | void dfs() { 16 | //if a node is unvisited then run dfs on it 17 | for(GraphNode node: nodeList) { 18 | if(!node.isVisited()) 19 | dfsVisit(node); 20 | } 21 | } 22 | 23 | 24 | //dfs traversal by a source node 25 | void dfsVisit(GraphNode node) { 26 | //make an empty stack 27 | Stackstack = new Stack<>(); 28 | //add source node to stack 29 | stack.push(node); 30 | //while queue is not empty 31 | while(!stack.isEmpty()) { 32 | //pop a node from stack 33 | GraphNode presentNode = stack.pop(); 34 | //mark node as visited 35 | presentNode.setVisited(true); 36 | //print the node 37 | System.out.print(presentNode.getName()+" "); 38 | //for each neighbor of present node 39 | for(GraphNode neighbor: presentNode.getNeighbors()) { 40 | //if neighbor is not visited then add it to queue 41 | if(!neighbor.isVisited()) { 42 | 43 | stack.add(neighbor); 44 | neighbor.setVisited(true); 45 | } 46 | } 47 | 48 | } 49 | } 50 | 51 | 52 | // add an undirected edge between two nodes 53 | public void addUndirectedEdge(int i, int j) { 54 | GraphNode first = nodeList.get(i-1); 55 | GraphNode second = nodeList.get(j-1); 56 | first.getNeighbors().add(second); 57 | second.getNeighbors().add(first); 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/dsGuy/dfs/DFSIterativeMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.dfs; 2 | 3 | import java.util.ArrayList; 4 | 5 | import dsGuy.node.GraphNode; 6 | 7 | public class DFSIterativeMain { 8 | 9 | public static void main(String[] args) { 10 | 11 | ArrayList nodeList = new ArrayList<>(); 12 | 13 | //create 10 nodes: v1-v10 14 | for(int i=1;i<11; i++) { 15 | nodeList.add(new GraphNode("V"+i)); 16 | } 17 | 18 | //Constructor 19 | DFSIterative graph = new DFSIterative(nodeList); 20 | 21 | //add edges following graph in graph.docx 22 | graph.addUndirectedEdge(1,2); 23 | graph.addUndirectedEdge(1,4); 24 | graph.addUndirectedEdge(2,3); 25 | graph.addUndirectedEdge(2,5); 26 | graph.addUndirectedEdge(3,6); 27 | graph.addUndirectedEdge(3,10); 28 | graph.addUndirectedEdge(4,7); 29 | graph.addUndirectedEdge(5,8); 30 | graph.addUndirectedEdge(6,9); 31 | graph.addUndirectedEdge(7,8); 32 | graph.addUndirectedEdge(8,9); 33 | graph.addUndirectedEdge(9,10); 34 | 35 | //dfs from v1 36 | System.out.println("Printing DFS from source: V1"); 37 | graph.dfs(); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/dsGuy/dfs/DFSRecursive.java: -------------------------------------------------------------------------------- 1 | package dsGuy.dfs; 2 | import java.util.ArrayList; 3 | import dsGuy.node.GraphNode; 4 | 5 | public class DFSRecursive { 6 | ArrayList nodeList = new ArrayList(); 7 | 8 | 9 | public DFSRecursive(ArrayList nodeList) { 10 | this.nodeList = nodeList; 11 | } 12 | 13 | 14 | void dfs() { 15 | //if a node is unvisited then run dfs on it 16 | for(GraphNode node: nodeList) { 17 | if(!node.isVisited()) 18 | dfsVisit(node); 19 | } 20 | } 21 | 22 | 23 | //dfs traversal by a source node 24 | void dfsVisit(GraphNode node) { 25 | //mark node as visited 26 | node.setVisited(true); 27 | //print the node 28 | System.out.print(node.getName()+" "); 29 | //for each neighbor of present node 30 | for(GraphNode neighbor: node.getNeighbors()) { 31 | //if neighbor is not visited 32 | if(!neighbor.isVisited()) { 33 | //recursive call to dfs function 34 | dfsVisit(neighbor); 35 | } 36 | } 37 | } 38 | 39 | // add an undirected edge between two nodes 40 | public void addUndirectedEdge(int i, int j) { 41 | GraphNode first = nodeList.get(i-1); 42 | GraphNode second = nodeList.get(j-1); 43 | first.getNeighbors().add(second); 44 | second.getNeighbors().add(first); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/dsGuy/dfs/DFSRecursiveMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.dfs; 2 | import java.util.ArrayList; 3 | import dsGuy.node.GraphNode; 4 | 5 | public class DFSRecursiveMain { 6 | 7 | public static void main(String[] args) { 8 | ArrayList nodeList = new ArrayList<>(); 9 | 10 | 11 | //create 10 nodes: v1-v10 12 | for(int i=1;i<11; i++) { 13 | nodeList.add(new GraphNode("V"+i)); 14 | } 15 | 16 | 17 | DFSRecursive graph = new DFSRecursive(nodeList); 18 | 19 | 20 | //add edges following graph in graph.docx 21 | graph.addUndirectedEdge(1,2); 22 | graph.addUndirectedEdge(1,4); 23 | graph.addUndirectedEdge(2,3); 24 | graph.addUndirectedEdge(2,5); 25 | graph.addUndirectedEdge(3,6); 26 | graph.addUndirectedEdge(3,10); 27 | graph.addUndirectedEdge(4,7); 28 | graph.addUndirectedEdge(5,8); 29 | graph.addUndirectedEdge(6,9); 30 | graph.addUndirectedEdge(7,8); 31 | graph.addUndirectedEdge(8,9); 32 | graph.addUndirectedEdge(9,10); 33 | 34 | //dfs from v1 35 | System.out.println("Printing DFS from source: V1"); 36 | graph.dfs(); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/dsGuy/hashing/DoubleHashingMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.hashing; 2 | 3 | public class DoubleHashingMain { 4 | 5 | public static void main(String[] args) { 6 | 7 | // Constructor 8 | DoubleHashing doubleHashing = new DoubleHashing(); 9 | 10 | doubleHashing.insertKeyInHashTable("The"); 11 | doubleHashing.insertKeyInHashTable("quick"); 12 | doubleHashing.insertKeyInHashTable("brown"); 13 | doubleHashing.insertKeyInHashTable("fox"); 14 | doubleHashing.insertKeyInHashTable("over"); 15 | doubleHashing.insertKeyInHashTable("lazy"); 16 | doubleHashing.displayHashTable(); 17 | 18 | /*doubleHashing.insertKeyInHashTable("fox"); // use for showing collision 19 | doubleHashing.displayHashTable(); 20 | 21 | doubleHashing.insertKeyInHashTable("fox"); 22 | doubleHashing.displayHashTable(); 23 | 24 | doubleHashing.insertKeyInHashTable("fox"); 25 | doubleHashing.displayHashTable(); 26 | 27 | doubleHashing.insertKeyInHashTable("fox"); 28 | doubleHashing.displayHashTable(); 29 | 30 | doubleHashing.insertKeyInHashTable("fox"); 31 | doubleHashing.displayHashTable();*/ 32 | 33 | 34 | doubleHashing.searchKeyInHashTable("jump"); 35 | doubleHashing.searchKeyInHashTable("brown"); 36 | 37 | 38 | doubleHashing.deleteKeyFromHashTable("jump"); 39 | doubleHashing.deleteKeyFromHashTable("quick"); 40 | doubleHashing.displayHashTable(); 41 | 42 | 43 | doubleHashing.deleteHashTable(); 44 | doubleHashing.displayHashTable(); 45 | 46 | 47 | }// end of method 48 | 49 | }// end of class 50 | -------------------------------------------------------------------------------- /src/dsGuy/hashing/LinearProbingMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.hashing; 2 | 3 | public class LinearProbingMain { 4 | public static void main(String[] args) { 5 | 6 | //Constructor 7 | LinearProbing linearProbing = new LinearProbing(); 8 | 9 | 10 | linearProbing.insertKeyInHashTable("The"); 11 | linearProbing.insertKeyInHashTable("quick"); 12 | linearProbing.insertKeyInHashTable("brown"); 13 | linearProbing.insertKeyInHashTable("fox"); 14 | linearProbing.insertKeyInHashTable("over"); 15 | linearProbing.insertKeyInHashTable("lazy"); 16 | linearProbing.displayHashTable(); 17 | 18 | linearProbing.insertKeyInHashTable("fox"); //use for showing collision 19 | linearProbing.displayHashTable(); 20 | 21 | /*linearProbing.insertKeyInHashTable("fox"); 22 | linearProbing.displayHashTable(); 23 | 24 | linearProbing.insertKeyInHashTable("fox"); 25 | linearProbing.displayHashTable(); 26 | 27 | linearProbing.insertKeyInHashTable("fox"); 28 | linearProbing.displayHashTable(); 29 | 30 | linearProbing.insertKeyInHashTable("fox"); 31 | linearProbing.displayHashTable(); 32 | 33 | 34 | linearProbing.searchKeyInHashTable("jump"); 35 | linearProbing.searchKeyInHashTable("brown"); 36 | 37 | 38 | linearProbing.deleteKeyFromHashTable("jump"); 39 | linearProbing.deleteKeyFromHashTable("quick"); 40 | linearProbing.displayHashTable(); 41 | 42 | 43 | linearProbing.deleteHashTable(); 44 | linearProbing.displayHashTable(); 45 | */ 46 | 47 | }//end of method 48 | 49 | }//end of class 50 | -------------------------------------------------------------------------------- /src/dsGuy/heap/HeapByArrayMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.heap; 2 | 3 | public class HeapByArrayMain { 4 | 5 | public static void main(String[] args) { 6 | System.out.println("Creating a blank Heap"); 7 | HeapByArray heap = new HeapByArray(10); 8 | 9 | heap.insertInHeap(100); 10 | heap.insertInHeap(90); 11 | heap.insertInHeap(80); 12 | heap.insertInHeap(70); 13 | heap.insertInHeap(60); 14 | heap.insertInHeap(50); 15 | heap.insertInHeap(40); 16 | heap.insertInHeap(30); 17 | heap.insertInHeap(20); 18 | 19 | heap.extractHeadOfHeap(); 20 | 21 | heap.insertInHeap(110); 22 | heap.extractHeadOfHeap(); 23 | 24 | } 25 | }//end of class 26 | -------------------------------------------------------------------------------- /src/dsGuy/node/BinaryNode.java: -------------------------------------------------------------------------------- 1 | package dsGuy.node; 2 | 3 | public class BinaryNode { 4 | private int value; 5 | private int height; 6 | private BinaryNode left; 7 | private BinaryNode right; 8 | 9 | public int getHeight() { 10 | return height; 11 | }//end of method 12 | 13 | public void setHeight(int height) { 14 | this.height = height; 15 | }//end of method 16 | 17 | public int getValue() { 18 | return value; 19 | }//end of method 20 | 21 | public void setValue(int value) { 22 | this.value = value; 23 | }//end of method 24 | 25 | public BinaryNode getLeft() { 26 | return left; 27 | }//end of method 28 | 29 | public void setLeft(BinaryNode left) { 30 | this.left = left; 31 | }//end of method 32 | 33 | public BinaryNode getRight() { 34 | return right; 35 | }//end of method 36 | 37 | public void setRight(BinaryNode right) { 38 | this.right = right; 39 | }//end of method 40 | 41 | @Override 42 | public String toString() { 43 | return value + ""; 44 | }//end of method 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/dsGuy/node/BinaryNodeWithParent.java: -------------------------------------------------------------------------------- 1 | package dsGuy.node; 2 | 3 | public class BinaryNodeWithParent { 4 | private int value; 5 | private BinaryNodeWithParent parent; 6 | private BinaryNodeWithParent left; 7 | private BinaryNodeWithParent right; 8 | 9 | public int getValue() { 10 | return value; 11 | } 12 | 13 | public void setValue(int value) { 14 | this.value = value; 15 | } 16 | 17 | public BinaryNodeWithParent getParent() { 18 | return parent; 19 | } 20 | 21 | public void setParent(BinaryNodeWithParent parent) { 22 | this.parent = parent; 23 | } 24 | 25 | public BinaryNodeWithParent getLeft() { 26 | return left; 27 | } 28 | 29 | public void setLeft(BinaryNodeWithParent left) { 30 | this.left = left; 31 | } 32 | 33 | public BinaryNodeWithParent getRight() { 34 | return right; 35 | } 36 | 37 | public void setRight(BinaryNodeWithParent right) { 38 | this.right = right; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return value+""; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/dsGuy/node/DoubleNode.java: -------------------------------------------------------------------------------- 1 | package dsGuy.node; 2 | 3 | 4 | public class DoubleNode { 5 | private int value; 6 | private DoubleNode next; 7 | private DoubleNode prev; 8 | 9 | public int getValue() { 10 | return value; 11 | } 12 | 13 | public void setValue(int value) { 14 | this.value = value; 15 | } 16 | 17 | public DoubleNode getNext() { 18 | return next; 19 | } 20 | 21 | public void setNext(DoubleNode next) { 22 | this.next = next; 23 | } 24 | 25 | public DoubleNode getPrev() { 26 | return prev; 27 | } 28 | 29 | public void setPrev(DoubleNode prev) { 30 | this.prev = prev; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return value + ""; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/dsGuy/node/GraphNode.java: -------------------------------------------------------------------------------- 1 | package dsGuy.node; 2 | 3 | import java.util.*; 4 | 5 | public class GraphNode { 6 | private String name; 7 | private int index; //index is used to map this Node's name with index of Adjacency Matrix' cell# 8 | private ArrayList neighbors = new ArrayList(); 9 | private boolean isVisited = false; 10 | private GraphNode parent; 11 | 12 | public GraphNode(String name, int index) { 13 | this.name = name; 14 | this.index = index; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public void setName(String name) { 22 | this.name = name; 23 | } 24 | 25 | public int getIndex() { 26 | return index; 27 | } 28 | 29 | public void setIndex(int index) { 30 | this.index = index; 31 | } 32 | 33 | public ArrayList getNeighbors() { 34 | return neighbors; 35 | } 36 | 37 | public void setNeighbors(ArrayList neighbors) { 38 | this.neighbors = neighbors; 39 | } 40 | 41 | public boolean isVisited() { 42 | return isVisited; 43 | } 44 | 45 | public void setVisited(boolean isVisited) { 46 | this.isVisited = isVisited; 47 | } 48 | 49 | public GraphNode getParent() { 50 | return parent; 51 | } 52 | 53 | public void setParent(GraphNode parent) { 54 | this.parent = parent; 55 | } 56 | 57 | public GraphNode(String name) { 58 | this.name = name; 59 | } 60 | 61 | @Override 62 | public String toString() { 63 | return name ; 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/dsGuy/node/SingleNode.java: -------------------------------------------------------------------------------- 1 | package dsGuy.node; 2 | 3 | public class SingleNode { 4 | private int value; 5 | private SingleNode next; 6 | 7 | public int getValue() { 8 | return value; 9 | } 10 | 11 | public void setValue(int value) { 12 | this.value = value; 13 | } 14 | 15 | public SingleNode getNext() { 16 | return next; 17 | } 18 | 19 | public void setNext(SingleNode next) { 20 | this.next = next; 21 | } 22 | 23 | @Override 24 | public String toString() { 25 | return value + ""; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/dsGuy/sorting/bubble/BubbleSort.java: -------------------------------------------------------------------------------- 1 | package dsGuy.sorting.bubble; 2 | 3 | public class BubbleSort { 4 | 5 | void bubbleSort(int arr[]) { 6 | for (int i = 0; i < arr.length; i++) { 7 | for (int j = 0; j < arr.length -1 -i; j++) { 8 | // if element is bigger then adjecent element, bubble it up. 9 | if(arr[j] > arr[j+1]){ 10 | int swap = arr[j+1]; 11 | arr[j+1] = arr[j]; 12 | arr[j] = swap; 13 | } 14 | } 15 | 16 | } 17 | } 18 | 19 | 20 | 21 | 22 | /* Prints the array */ 23 | void printArray(int arr[]) { 24 | int n = arr.length; 25 | for (int i = 0; i < n; ++i) 26 | System.out.print(arr[i] + " "); 27 | System.out.println(); 28 | } 29 | 30 | }// end of class -------------------------------------------------------------------------------- /src/dsGuy/sorting/bubble/BubbleSortMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.sorting.bubble; 2 | 3 | public class BubbleSortMain { 4 | 5 | public static void main(String[] args) { 6 | 7 | BubbleSort ob = new BubbleSort(); 8 | int arr[] = { 10, 5, 30, 15, 50, 6 }; 9 | System.out.println("Array to be sorted..."); 10 | ob.printArray(arr); 11 | 12 | ob.bubbleSort(arr); 13 | 14 | System.out.println("Sorted Array..."); 15 | ob.printArray(arr); 16 | 17 | }// end of method 18 | 19 | }// end of class -------------------------------------------------------------------------------- /src/dsGuy/sorting/bucket/BucketSortMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.sorting.bucket; 2 | import java.util.Random; 3 | 4 | public class BucketSortMain { 5 | public static void main(String[] args) { 6 | 7 | int arr[] = new int[100]; 8 | 9 | //Generating 100 random numbers in the range of 0-100 10 | Random random = new Random(); 11 | for(int i=0;i<100;i++) { 12 | arr[i] = random.nextInt(100)+100; 13 | } 14 | 15 | 16 | //Passing this array to BucketSort method 17 | BucketSort bs = new BucketSort(arr); 18 | System.out.println("Array before Sorting: "); 19 | bs.printArray(); 20 | bs.bucketSort(); 21 | 22 | 23 | System.out.println("\n\nArray after Sorting: "); 24 | bs.printArray(); 25 | 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/dsGuy/sorting/heap/HeapSort.java: -------------------------------------------------------------------------------- 1 | package dsGuy.sorting.heap; 2 | 3 | public class HeapSort { 4 | int[] arr = null; 5 | 6 | //Constructor 7 | public HeapSort(int[] arr) { 8 | this.arr = arr; 9 | }//end of method 10 | 11 | 12 | public void sort() { 13 | HeapByArray hba = new HeapByArray(arr.length); //We will reuse HeapByArray class to do sorting 14 | for(int i=0; i 0 && A[j - 1] > tmp) { 10 | A[j] = A[j - 1]; 11 | j--; 12 | } 13 | A[j] = tmp; 14 | }//end of for loop 15 | }//end of method 16 | 17 | 18 | public static void printArray(int[] array) { 19 | for (int i = 0; i < array.length; i++) { 20 | System.out.print(array[i] + " "); 21 | } 22 | }//end of method 23 | 24 | }//end of class -------------------------------------------------------------------------------- /src/dsGuy/sorting/insertion/InsertionSortMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.sorting.insertion; 2 | 3 | public class InsertionSortMain { 4 | public static void main(String[] args) { 5 | 6 | int array[] = {10, 3, 2, 5, 8, 4, 3, 1, 2, 9, 7, 8}; 7 | 8 | System.out.println("User entered Array: "); 9 | InsertionSort.printArray(array); 10 | 11 | long start = System.nanoTime(); 12 | InsertionSort.insertionSort(array); 13 | long end = System.nanoTime(); 14 | System.out.println("\n\nTime to execute this algo: " + (end-start)); 15 | 16 | System.out.println("\nAfter sorting: "); 17 | InsertionSort.printArray(array); 18 | }//end of method 19 | 20 | }//end of class 21 | -------------------------------------------------------------------------------- /src/dsGuy/sorting/merge/MergeSort.java: -------------------------------------------------------------------------------- 1 | package dsGuy.sorting.merge; 2 | 3 | public class MergeSort { 4 | 5 | static int count =0; 6 | public static void mergeSort(int[] Array, int left, int right) { 7 | if (right > left) { 8 | count++; 9 | int m = (left + right) / 2; 10 | System.out.println("pass L=" + left +" m="+ m+" " ); 11 | mergeSort(Array, left, m); 12 | System.out.println("pass m=" + (m+1) +" R="+ right+" " ); 13 | mergeSort(Array, m + 1, right); 14 | merge(Array, left, m, right); 15 | System.out.println("total = "+count); 16 | } 17 | }//end of method 18 | 19 | 20 | static void merge(int[] A, int left, int middle, int right) { 21 | int [] leftTmpArray = new int[middle-left+2]; //Create tmp arrays 22 | int [] rightTmpArray = new int[right-middle+1]; 23 | 24 | for(int i=0;i<=middle-left;i++) //Copy values from Array 'A' to these tmp arrays 25 | leftTmpArray[i]= A[left+i]; 26 | for(int i=0;i a[j]){ 12 | minI = j; 13 | } 14 | } 15 | if(minI != i) { 16 | int swap = a[minI]; 17 | a[minI] = a[i]; 18 | a[i] = swap; 19 | } 20 | } 21 | 22 | } 23 | 24 | public static void printArray(int[] array) { 25 | for (int i = 0; i < array.length; i++) { 26 | System.out.print(array[i] + " "); 27 | } 28 | }//end of method 29 | 30 | }//end of class 31 | -------------------------------------------------------------------------------- /src/dsGuy/sorting/selection/SelectionSortMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.sorting.selection; 2 | 3 | public class SelectionSortMain { 4 | 5 | public static void main(String[] args) { 6 | int array[] = {10, 3, 2, 5, 8, 4, 3, 1, 2, 9, 7, 8}; 7 | 8 | System.out.println("User entered Array: "); 9 | SelectionSort.printArray(array); 10 | 11 | SelectionSort.selectionSort(array); 12 | 13 | System.out.println("\n\nAfter sorting: "); 14 | SelectionSort.printArray(array); 15 | }//end of method 16 | 17 | }//end of class -------------------------------------------------------------------------------- /src/dsGuy/sorting/topologicalSort/TopologicalSort.java: -------------------------------------------------------------------------------- 1 | package dsGuy.sorting.topologicalSort; 2 | import java.util.ArrayList; 3 | import java.util.Stack; 4 | import dsGuy.node.GraphNode; 5 | 6 | public class TopologicalSort { 7 | ArrayList nodeList = new ArrayList(); 8 | 9 | 10 | //Constructor 11 | public TopologicalSort(ArrayList nodeList) { 12 | this.nodeList = nodeList; 13 | } 14 | 15 | 16 | void topologicalSort() { 17 | Stackstack = new Stack<>(); 18 | for (GraphNode node : nodeList) { // if a node is unvisited then run topologicalSort on it 19 | if (!node.isVisited()) 20 | topologicalVisit(node,stack); 21 | } 22 | while(!stack.isEmpty()){ 23 | System.out.print(stack.pop().getName()+" "); 24 | } 25 | } 26 | 27 | 28 | // Topological visit by a source node 29 | void topologicalVisit(GraphNode node, Stack stack) { 30 | for (GraphNode neighbor : node.getNeighbors()){ //if neighbor is not visited then recursive call to it 31 | if(!neighbor.isVisited()){ 32 | topologicalVisit(neighbor,stack); 33 | } 34 | } 35 | node.setVisited(true); 36 | stack.push(node); 37 | } // end of method 38 | 39 | 40 | // add a directed edge between two nodes 41 | public void addDirectedEdge(int i, int j) { 42 | GraphNode first = nodeList.get(i - 1); 43 | GraphNode second = nodeList.get(j - 1); 44 | first.getNeighbors().add(second); 45 | } // end of method 46 | 47 | }//end of class -------------------------------------------------------------------------------- /src/dsGuy/trie/TrieMain.java: -------------------------------------------------------------------------------- 1 | package dsGuy.trie; 2 | 3 | public class TrieMain { 4 | 5 | public static void main(String[] args) { 6 | Trie t =new Trie(); 7 | 8 | //CASE#1 9 | t.insert("bcde"); 10 | t.insert("bckg"); 11 | t.delete("bcde"); 12 | t.search("bcde"); 13 | t.search("bckg"); 14 | 15 | 16 | /*//CASE#2 17 | t.insert("bcde"); 18 | t.insert("bcdefg"); 19 | t.delete("bcde"); 20 | t.search("bcde"); 21 | t.search("bcdefg");*/ 22 | 23 | 24 | /*//CASE#3 25 | t.insert("bcde"); 26 | t.insert("bc"); 27 | t.delete("bcde"); 28 | t.search("bcde"); 29 | t.search("bcde"); 30 | t.search("bc"); 31 | t.search("b");*/ 32 | 33 | 34 | /*//CASE#4 35 | t.insert("bcde"); 36 | t.delete("bcde"); 37 | t.search("bcde");*/ 38 | 39 | } 40 | 41 | }//End of Class -------------------------------------------------------------------------------- /src/dynamicProgramming/backtracking/Anagrams.java: -------------------------------------------------------------------------------- 1 | package dynamicProgramming.backtracking; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Anagrams { 6 | 7 | public static void main(String[] args) { 8 | anagrams("GOOD"); 9 | } 10 | 11 | public static void anagrams(String input) { 12 | char[] inputArray = input.toCharArray(); 13 | Arrays.sort(inputArray); 14 | anagrams(inputArray, new char[input.length()], new boolean[input.length()], 0); 15 | } 16 | 17 | public static void anagrams(char[] input, char[] anagram, boolean[] used, int index) { 18 | if (index == input.length) { 19 | System.out.println(new String(anagram)); 20 | return; 21 | } 22 | for (int i = 0; i < input.length; i++) { 23 | if (!used[i] && !(i > 0 && input[i] == input[i - 1] && !used[i - 1])) { 24 | used[i] = true; 25 | anagram[index] = input[i]; 26 | anagrams(input, anagram, used, index + 1); 27 | used[i] = false; 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/dynamicProgramming/backtracking/Backtracking-PDF.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/src/dynamicProgramming/backtracking/Backtracking-PDF.pdf -------------------------------------------------------------------------------- /src/dynamicProgramming/backtracking/CombinationSum.java: -------------------------------------------------------------------------------- 1 | package dynamicProgramming.backtracking; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class CombinationSum { 8 | public static void main(String[] args) { 9 | int[] nums = {10, 1, 2, 7, 6, 1, 5}; 10 | int target = 8; 11 | combinationSum(nums, target); 12 | } 13 | 14 | public static void combinationSum(int[] nums, int target) { 15 | Arrays.sort(nums); 16 | combinationSum(nums, target, 0, new ArrayList<>(), 0); 17 | } 18 | 19 | public static void combinationSum(int[] nums, int target, int sum, List partial, int start) { 20 | if (sum == target) { 21 | System.out.println(Arrays.toString(partial.toArray())); 22 | return; 23 | } 24 | 25 | for (int i = start; i < nums.length; i++) { 26 | int c = nums[i]; 27 | if (sum + c > target || i > start && nums[i] == nums[i - 1]) { 28 | continue; 29 | } 30 | partial.add(c); 31 | combinationSum(nums, target, sum + c, partial, i + 1); 32 | partial.remove(partial.size() - 1); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/dynamicProgramming/backtracking/Permutation.java: -------------------------------------------------------------------------------- 1 | package dynamicProgramming.backtracking; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | 6 | public class Permutation { 7 | public static void main(String[] args) { 8 | int[] input = {1,2,3,4,5}; 9 | permutation(input); 10 | } 11 | 12 | /** 13 | * Returns all the permutations of elements of the given array 14 | * @param input 15 | */ 16 | public static void permutation(int[] input){ 17 | Arrays.sort(input); 18 | permutation(input,new ArrayList<>(),new boolean[input.length]); 19 | } 20 | 21 | /** 22 | * Returns all the permutations of elements of the given array 23 | * @param input 24 | * @param partial contains the partial permutation 25 | * @param used uses flags to indicate whether or not the element at the given index is used or not 26 | */ 27 | public static void permutation(int[] input, ArrayList partial, boolean[] used) { 28 | if (partial.size() == input.length) { 29 | System.out.println(Arrays.toString(partial.toArray())); 30 | return; 31 | } 32 | for (int i = 0; i < input.length; i++) { 33 | if (!used[i] && !(i > 0 && input[i] == input[i - 1] && !used[i - 1])) { 34 | used[i] = true; 35 | partial.add(input[i]); 36 | permutation(input, partial, used); 37 | used[i] = false; 38 | partial.remove(partial.size() - 1); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/dynamicProgramming/backtracking/WordBreak.java: -------------------------------------------------------------------------------- 1 | package dynamicProgramming.backtracking; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.HashSet; 6 | import java.util.List; 7 | 8 | public class WordBreak { 9 | 10 | public static void main(String[] args){ 11 | HashSet dictionary = new HashSet<>(Arrays.asList("cat","cats","and","sand","dogs")); 12 | String input = "catsanddogs"; 13 | wordBreak(input,dictionary,new ArrayList<>()); 14 | } 15 | 16 | public static void wordBreak(String input, HashSet dict, List partial){ 17 | if(input.length() == 0){ 18 | System.out.println(Arrays.toString(partial.toArray())); 19 | return; 20 | } 21 | for(int i=0;i= j) { 11 | return true; 12 | } 13 | return input.charAt(i) == input.charAt(j) && isPalindrome(input, i + 1, j - 1); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/dynamicProgramming/recursion/PrintStar.java: -------------------------------------------------------------------------------- 1 | package dynamicProgramming.recursion; 2 | 3 | public class PrintStar { 4 | 5 | public static void main(String[] args){ 6 | int n = 6; 7 | printPattern(n); 8 | } 9 | 10 | public static void printPattern(int n){ 11 | printPattern(n,n,true); 12 | } 13 | 14 | public static void printPattern(int n,int limit,boolean decreasing){ 15 | printStar(n); 16 | if(n == 1){ 17 | printPattern(n+1,limit,false); // Recursive call 18 | }else if(n arr[j+1]) 11 | { 12 | // swap temp and arr[i] 13 | int temp = arr[j]; 14 | arr[j] = arr[j+1]; 15 | arr[j+1] = temp; 16 | } 17 | } 18 | } 19 | 20 | return arr; 21 | } 22 | } -------------------------------------------------------------------------------- /src/eliminateCodeFear/algorithms/BubbleSorter.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.algorithms; 2 | 3 | public class BubbleSorter { 4 | 5 | public int[] sort(int arr[]) 6 | { 7 | int n = arr.length; 8 | for (int i = 0; i < n-1; i++) { 9 | for (int j = 0; j < n-i-1; j++) { 10 | print(arr, i, j); 11 | if (arr[j] > arr[j+1]) 12 | { 13 | // swap temp and arr[i] 14 | System.out.println("Swapping: " + arr[j] + " " + arr[j+1]); 15 | int temp = arr[j]; 16 | arr[j] = arr[j+1]; 17 | arr[j+1] = temp; 18 | } 19 | } 20 | } 21 | 22 | return arr; 23 | } 24 | 25 | public void print(int[] a, int i, int j) { 26 | System.out.println(a[0] + " " + a[1] + " " + a[2] + " " + a[3] + " " + a[4] + " i=" + i + " j=" + j); 27 | } 28 | } -------------------------------------------------------------------------------- /src/eliminateCodeFear/algorithms/FibonacciMemoized.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.algorithms; 2 | 3 | public class FibonacciMemoized { 4 | 5 | private int[] memo = new int[1001]; 6 | 7 | public int fib(int n) { 8 | System.out.println("n = " + n); 9 | if (n <= 0) { 10 | return 0; 11 | } else if (n == 1) { 12 | return 1; 13 | } else if (memo[n] == 0){ 14 | memo[n] = fib(n - 1) + fib(n - 2); 15 | } 16 | return memo[n]; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/algorithms/FibonacciNaive.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.algorithms; 2 | 3 | public class FibonacciNaive { 4 | 5 | public int fib(int n) { 6 | System.out.println("Start fib(" + n + ")"); 7 | if (n <= 0) { 8 | return 0; 9 | } else if (n == 1) { 10 | return 1; 11 | } else { 12 | return fib(n - 1) + fib(n - 2); 13 | } 14 | } 15 | 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/algorithms/Person.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.algorithms; 2 | 3 | public class Person { 4 | private int id; 5 | private String address; 6 | 7 | public Person(int id, String address) { 8 | this.id = id; 9 | this.address = address; 10 | } 11 | 12 | @Override 13 | public int hashCode() { 14 | int result = id; 15 | result = 31 * result + (address != null ? address.hashCode() : 0); 16 | return result; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/algorithms/graphs/DFS/Graph.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.algorithms.graphs.DFS; 2 | 3 | import java.util.Iterator; 4 | import java.util.LinkedList; 5 | import java.util.Stack; 6 | 7 | @SuppressWarnings("unchecked") 8 | class Graph 9 | { 10 | private int V; // No. of vertices 11 | private LinkedList[] adj; // Adjacency List 12 | 13 | Graph(int v) { 14 | V = v; 15 | adj = new LinkedList[v]; 16 | for (int i=0; i stack = new Stack(); 29 | stack.add(v); 30 | 31 | visited[v] = true; 32 | 33 | while (!stack.isEmpty()) { 34 | int current = stack.pop(); 35 | System.out.print(current + " "); 36 | 37 | Iterator i = adj[current].listIterator(); 38 | while (i.hasNext()) 39 | { 40 | int n = i.next(); 41 | if (!visited[n]) { 42 | stack.add(n); 43 | visited[n] = true; 44 | } 45 | } 46 | } 47 | } 48 | 49 | // based on https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/ 50 | } -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/arraysAndStrings/PalindromeDetector.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.arraysAndStrings; 2 | 3 | public class PalindromeDetector { 4 | 5 | public boolean isPalindrome(String text) { 6 | 7 | // first and last letters must match...and each subsequent letter as we walk in. 8 | // but we only need to walk 1/2 the loop (as the later half will be the same as the first) 9 | 10 | char[] content = text.toCharArray(); 11 | int length = content.length; 12 | 13 | for (int i = 0; i < length / 2; i++) { 14 | if (content[i] != content[length - i - 1]) { 15 | return false; 16 | } 17 | } 18 | return true; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/arraysAndStrings/PermutationDetector.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.arraysAndStrings; 2 | 3 | import java.util.Arrays; 4 | 5 | public class PermutationDetector { 6 | 7 | public boolean isPermutation(String text, String perm) { 8 | if (perm.length() != text.length()) return false; 9 | 10 | return sort(text).equals(sort(perm)); 11 | } 12 | 13 | private String sort(String s) { 14 | char[] content = s.toCharArray(); 15 | Arrays.sort(content); 16 | return new String(content); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/arraysAndStrings/URLConverter.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.arraysAndStrings; 2 | 3 | public class URLConverter { 4 | 5 | 6 | public String urlify(String url, int length) { 7 | 8 | // Create a bucket to hold our final result 9 | char[] result = new char[length]; 10 | 11 | // Strip off any space at beginning or end 12 | url = url.trim(); 13 | 14 | // Loop through url, and insert an ASCII space '%20' whenever we hit a space 15 | char[] urlChars = url.toCharArray(); 16 | 17 | // Also create a pointer to keep track of where we are in our results array 18 | int pointer = 0; 19 | 20 | for (int i=0; i < urlChars.length; i++) { 21 | 22 | if (urlChars[i] != ' ') { 23 | result[pointer] = urlChars[i]; 24 | pointer++; 25 | } else { 26 | result[pointer] = '%'; 27 | result[pointer+1] = '2'; 28 | result[pointer+2] = '0'; 29 | pointer = pointer + 3; 30 | } 31 | prettyPrint(result); 32 | System.out.println("..."); 33 | } 34 | 35 | return String.valueOf(result); 36 | } 37 | 38 | private void prettyPrint(char[] chars) { 39 | for (int i = 0; i < chars.length; i++) { 40 | System.out.println("c[" + chars[i] + "]"); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/arraysAndStrings/UniqueCharacterDetector.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.arraysAndStrings; 2 | 3 | public class UniqueCharacterDetector { 4 | 5 | // There are lots of ways we could solve this: 6 | // - HashMap where we store every character and a boolean on whether found 7 | // - An array that counts the number of times a character occurs 8 | // 9 | // But as an example of of how optimized and tight we can make this, checkout this solution below 10 | // to see how a simple array, that stores booleans, gives us everything we need. 11 | 12 | public boolean isUnique(String text) { 13 | // Create an array representing all unique 128 characters in ASCII. 14 | // Set a flag to true for each letter. 15 | 16 | // Return false when we find we already have a match. 17 | 18 | // If length > 128 there must be a duplicate 19 | if (text.length() > 128) return false; 20 | 21 | boolean[] char_set = new boolean[128]; 22 | for (int i = 0; i < text.length(); i++) { 23 | int val = text.charAt(i); // char can be int 24 | if (char_set[val]) { // Already found 25 | return false; 26 | } 27 | char_set[val] = true; 28 | } 29 | return true; 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/arraysAndStrings/Zeroer1.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.arraysAndStrings; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Zeroer1 { 6 | 7 | public class Pair { 8 | 9 | public int y; 10 | public int x; 11 | 12 | public Pair(int y, int x) { 13 | this.y = y; 14 | this.x = x; 15 | } 16 | } 17 | 18 | 19 | public void zeroOut(int[][] matrix) { 20 | ArrayList zeros = getZeros(matrix); 21 | 22 | for (int i = 0; i < zeros.size(); i++) { 23 | zeroOutRow(zeros.get(i).y, matrix); 24 | zeroOutCol(zeros.get(i).x, matrix); 25 | } 26 | } 27 | 28 | public ArrayList getZeros(int[][] matrix) { 29 | ArrayList results = new ArrayList(); 30 | 31 | for (int row = 0; row < matrix.length; row++) { 32 | for (int col = 0; col < matrix[row].length; col++) { 33 | if (matrix[row][col] == 0) { 34 | results.add(new Pair(row, col)); 35 | } 36 | } 37 | } 38 | 39 | return results; 40 | } 41 | 42 | public void zeroOutRow(int y, int[][] matrix) { 43 | int[] row = matrix[y]; 44 | 45 | for (int col = 0; col < row.length; col++) { 46 | matrix[y][col] = 0; 47 | } 48 | } 49 | 50 | public void zeroOutCol(int x, int[][] matrix) { 51 | for (int row = 0; row < matrix.length; row++) { 52 | matrix[row][x] = 0; 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/binaryTrees/CustomBinaryTreeNode.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.binaryTrees; 2 | 3 | public class CustomBinaryTreeNode { 4 | 5 | public int key; 6 | public CustomBinaryTreeNode left; 7 | public CustomBinaryTreeNode right; 8 | 9 | public CustomBinaryTreeNode (int key) { 10 | this.key = key; 11 | right = null; 12 | left = null; 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/binaryTrees/SubtreeChecker.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.binaryTrees; 2 | 3 | public class SubtreeChecker { 4 | 5 | public boolean isSubTree(SimpleBinarySearchTree t1, SimpleBinarySearchTree t2) { 6 | String order1 = t1.getOrder(); 7 | String order2 = t2.getOrder(); 8 | 9 | return order1.contains(order2); 10 | } 11 | } -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/classics/FizzBuzz.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.classics; 2 | 3 | public class FizzBuzz { 4 | 5 | // 6 | // This solution is all about the 'modulus' operator. 7 | // If you can divide the number by 3, 5, or 15 and have no remainder, 8 | // you have a candidate for printing. 9 | // 10 | // Things to watch out for: 11 | // - start your loop at index 1 and end at 101 12 | // - know the modulus operator 13 | // 14 | public void print() { 15 | for (int i=1; i < 101; i++) { 16 | if (i % 3 == 0 && i % 5 == 0) { 17 | System.out.println("FizzBuzz"); 18 | } else if (i % 3 == 0) { 19 | System.out.println("Fizz"); 20 | } else if (i % 5 == 0) { 21 | System.out.println("Buzz"); 22 | } else { 23 | System.out.println(i); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/classics/IntReverser.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.classics; 2 | 3 | public class IntReverser { 4 | 5 | // 6 | // So the way this works is you take your number (say x)... 7 | // and then constantly apply the modulus 10 operator (%10) 8 | // to get the '1s' number, and then repeatedly 'shift' it up by multiplying the result by x10. 9 | // 10 | // i.e. 123 % 10 => 3 11 | // 12 | // Once you have the single digit number, you take your current reverse result, 13 | // multiple it by 10, and then add your new digit. 14 | // 15 | // reverse = reverse * 10 + lastDigit; => 0 + 3 = 3 16 | // 17 | // Then you take the current (123) and divide it by 10 to get rid of the last digit which you have added to the result 18 | // 19 | // x = x / 10; => 123 => 12 20 | // 21 | // Then you repeat the process with 12. Grab the single digit (2), add it to the accumulating reverse (32), shave off a 10. 22 | // 23 | 24 | public int reverse(int x) { 25 | boolean isNegative = x < 0; 26 | 27 | if(isNegative) { 28 | x = x * -1; 29 | } 30 | 31 | int reverse = 0; 32 | int lastDigit = 0; 33 | 34 | while (x >= 1) { 35 | lastDigit = x % 10; // gives you last digit 36 | reverse = reverse * 10 + lastDigit; 37 | x = x / 10; // get rid of last digit 38 | } 39 | 40 | return isNegative ? reverse*-1 : reverse; 41 | 42 | } 43 | 44 | // Soln based off http://www.java67.com/2015/08/how-to-reverse-integer-in-java-leetcode-solution.html 45 | } 46 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/classics/SieveOfEratosthenes.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.classics; 2 | 3 | public class SieveOfEratosthenes { 4 | 5 | public void print(int upperBound) { 6 | int upperBoundSquareRoot = (int) Math.sqrt(upperBound); 7 | 8 | // isComposite means not prime 9 | boolean[] isComposite = new boolean[upperBound + 1]; 10 | 11 | // going from prime 2 up to sqrt n 12 | for (int m = 2; m <= upperBoundSquareRoot; m++) { 13 | 14 | // if prime 15 | if (!isComposite[m]) { 16 | // Print it... 17 | System.out.print(m + " "); 18 | 19 | // Then mark all the others multiples as no prime 20 | for (int k = m * m; k <= upperBound; k += m) { 21 | isComposite[k] = true; 22 | } 23 | } 24 | } 25 | 26 | System.out.println("\nEverything left over is prime..."); 27 | 28 | for (int m = upperBoundSquareRoot; m <= upperBound; m++) { 29 | if (!isComposite[m]) 30 | System.out.print(m + " "); 31 | } 32 | 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/classics/StringReverser.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.classics; 2 | 3 | public class StringReverser { 4 | 5 | // Soln1: Easy - use StringBuilder 6 | 7 | public String soln2(String text) { 8 | StringBuilder sb = new StringBuilder(text); 9 | sb.reverse(); 10 | return sb.toString(); 11 | } 12 | 13 | 14 | // Soln2: Create an array of chars and swap with pointers 15 | 16 | public String soln1(String text) { 17 | 18 | // Convert into array 19 | char[] chars = text.toCharArray(); 20 | 21 | // Initialize pointers 22 | int left; 23 | int right = text.length() - 1; 24 | 25 | // Loop through swapping left and right until we hit the middle 26 | for (left = 0; left < right; left++, right--) { 27 | char temp = chars[left]; 28 | chars[left] = chars[right]; 29 | chars[right] = temp; 30 | } 31 | 32 | return String.valueOf(chars); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/interviews/CreditCard.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.interviews; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | 6 | public class CreditCard { 7 | 8 | private String name; 9 | private ArrayList prefixes; 10 | private ArrayList lengths; 11 | 12 | public String Name() { 13 | return name; 14 | } 15 | 16 | public CreditCard(String name, String[] prefixes, int[] lengths) { 17 | this.name = name; 18 | this.prefixes = new ArrayList<>(Arrays.asList(prefixes)); 19 | 20 | Integer[] lengthsAsIntegers = Arrays.stream( lengths ).boxed().toArray( Integer[]::new ); 21 | this.lengths = new ArrayList<>(Arrays.asList(lengthsAsIntegers)); 22 | } 23 | 24 | public boolean matches(String cardNumber) { 25 | 26 | // check lengths 27 | if (!lengths.contains(cardNumber.length())) { 28 | return false; 29 | } 30 | 31 | // check prefixes 32 | for (String prefix: prefixes) { 33 | if (cardNumber.startsWith(prefix)) { 34 | return true; 35 | } 36 | } 37 | 38 | return false; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/interviews/CreditCardProcessor.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.interviews; 2 | 3 | public class CreditCardProcessor { 4 | 5 | private CreditCard[] creditCards; 6 | 7 | private CreditCard VISA; 8 | private CreditCard Amex; 9 | private CreditCard Discovery; 10 | private CreditCard JCB; 11 | 12 | public CreditCardProcessor() { 13 | VISA = new CreditCard("VISA", new String[]{"4"}, new int[]{16}); 14 | Amex = new CreditCard("Amex", new String[]{"34", "37"}, new int[]{15}); 15 | Discovery = new CreditCard("Discovery", new String[]{"60", "65"}, new int[]{16}); 16 | JCB = new CreditCard("JCB", new String[]{"35"}, new int[]{16, 17, 18, 19}); 17 | 18 | creditCards = new CreditCard[]{ VISA, Amex, Discovery, JCB }; 19 | } 20 | 21 | public String getCardType(String cardNumber) { 22 | for (CreditCard creditCard : creditCards) { 23 | if (creditCard.matches(cardNumber)) { 24 | return creditCard.Name(); 25 | } 26 | } 27 | return "Unknown"; 28 | } 29 | 30 | // public String getCardType(String cardNumber) { 31 | // if (cardNumber.startsWith("4111111111111111")) { 32 | // return "VISA"; 33 | // } else if (cardNumber.startsWith("341111111111111")) { 34 | // return "Amex"; 35 | // } else if (cardNumber.startsWith("371111111111111")) { 36 | // return "Amex"; 37 | // } 38 | // return "Unknown"; 39 | // } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/linkedLists/LinkedListLoopDetector.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.linkedLists; 2 | 3 | public class LinkedListLoopDetector { 4 | 5 | public Node head; 6 | 7 | public void addBack(Node newNode) { 8 | 9 | // if head... set and return 10 | if (head == null) { 11 | head = newNode; 12 | return; 13 | } 14 | 15 | // Else starting at head... 16 | Node current = head; 17 | 18 | // Walk until to hit tail 19 | while (current.next != null) { 20 | current = current.next; 21 | } 22 | 23 | // Set current node to equal newNode 24 | current.next = newNode; 25 | } 26 | 27 | // 28 | // Solution: Loop detection 29 | // 30 | // This is an implementation of Floyd's cycle-finding algorithm for detecting loops 31 | // https://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare 32 | // https://stackoverflow.com/questions/2663115/how-to-detect-a-loop-in-a-linked-list 33 | 34 | public boolean hasLoop() { 35 | return hasLoop(head); 36 | } 37 | 38 | boolean hasLoop(Node first) { 39 | Node slow = first; 40 | Node fast = first; 41 | 42 | while(fast != null && fast.next != null) { 43 | slow = slow.next; // 1 hop 44 | fast = fast.next.next; // 2 hops 45 | 46 | if(slow == fast) // fast caught up to slow, so there is a loop 47 | return true; 48 | } 49 | return false; // fast reached null, so the list terminates 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/challenges/linkedLists/Node.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.challenges.linkedLists; 2 | 3 | public class Node { 4 | int data; 5 | Node next; 6 | 7 | public Node(int data) { 8 | this.data = data; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/datastructures/LinkedListWithIndex.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.datastructures; 2 | 3 | public class LinkedListWithIndex { 4 | 5 | private static class Node { 6 | int key; 7 | int data; 8 | Node next; 9 | 10 | public Node(int key, int data) { 11 | this.key = key; 12 | this.data = data; 13 | } 14 | } 15 | 16 | private Node head; 17 | 18 | public void addFront(int key, int data) { 19 | 20 | Node newNode = new Node(key, data); 21 | 22 | if (head == null) { 23 | head = newNode; 24 | return; 25 | } 26 | 27 | newNode.next = head; 28 | 29 | head = newNode; 30 | } 31 | 32 | public int getByIndex(int key) { 33 | Node current = head; 34 | 35 | // while we are not at the tail 36 | while (current.next != null) { 37 | // If we have a match 38 | if (current.key == key) { 39 | return current.data; 40 | } 41 | // Else goto the next node 42 | current = current.next; 43 | } 44 | 45 | // Check the last node 46 | if (current.key == key) { 47 | return current.data; 48 | } 49 | 50 | return -1; // Not found 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/datastructures/PrettyPrinter.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.datastructures; 2 | 3 | public class PrettyPrinter { 4 | 5 | public static void printMatrix(int[][] matrix) { 6 | 7 | int rowCount = matrix.length; // y 8 | int columnCount = matrix[0].length; // x 9 | 10 | for (int row = 0; row < rowCount; row++) { 11 | StringBuilder sb = new StringBuilder(); 12 | for (int col = 0; col < columnCount; col++) { 13 | sb.append(matrix[row][col] + " "); 14 | } 15 | System.out.println(sb.toString()); 16 | } 17 | System.out.println(""); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/datastructures/Queue.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.datastructures; 2 | 3 | public class Queue { 4 | 5 | private class Node { 6 | private int data; 7 | private Node next; 8 | private Node (int data) { 9 | this.data = data; 10 | } 11 | } 12 | 13 | private Node head; // remove things here 14 | private Node tail; // add things here 15 | 16 | public boolean isEmpty() { 17 | return head == null; 18 | } 19 | 20 | public int peek() { 21 | return head.data; 22 | } 23 | 24 | public void add(int data) { 25 | // Create a new node 26 | // Set the current tail.next to point to this new node 27 | // Then set the new node to be the new tail 28 | 29 | Node newNode = new Node(data); 30 | if (tail != null) { 31 | tail.next = newNode; 32 | } 33 | tail = newNode; 34 | 35 | // handle case of first element where head is null 36 | if (head == null) { 37 | head = tail; 38 | } 39 | } 40 | 41 | public int remove() { 42 | // Save the data 43 | // Point the head to the next element (effectively removing it) 44 | // Then return the data 45 | 46 | int data = head.data; 47 | head = head.next; 48 | 49 | // Handle queue now being empty 50 | if (head == null) { 51 | tail = null; 52 | } 53 | 54 | return data; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/eliminateCodeFear/datastructures/Stack.java: -------------------------------------------------------------------------------- 1 | package eliminateCodeFear.datastructures; 2 | 3 | import java.util.EmptyStackException; 4 | 5 | // LIFO - Last In First Out 6 | 7 | public class Stack { 8 | 9 | private class Node { 10 | private int data; 11 | private Node next; 12 | private Node (int data) { 13 | this.data = data; 14 | } 15 | } 16 | 17 | private Node head; // add and remove things here 18 | private int size; 19 | 20 | public boolean isEmpty() { 21 | return head == null; 22 | } 23 | 24 | public int peek() { 25 | return head.data; 26 | } 27 | 28 | public void push(int data) { 29 | // Create new node 30 | // Set it's next to be head 31 | // Set head to be the new node 32 | 33 | Node newNode = new Node(data); 34 | newNode.next = head; 35 | head = newNode; 36 | size++; 37 | } 38 | 39 | public int pop() { 40 | // Store the value you want to return 41 | // Set the current head.next to be the new head 42 | // return the value 43 | 44 | if (head == null) throw new EmptyStackException(); 45 | 46 | int data = head.data; 47 | head = head.next; 48 | size--; 49 | 50 | return data; 51 | } 52 | 53 | public int size() { 54 | return size; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/javaConcepts/collection/Enum.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.collection; 2 | 3 | import java.util.EnumMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * Created by asee2278 on 2/1/17. 8 | */ 9 | public class Enum { 10 | 11 | public enum Days { 12 | Monday, Tuesday, Wednesday, Thursday 13 | }; 14 | public static void main(String[] args) { 15 | //create and populate enum map 16 | EnumMap map = new EnumMap(Days.class); 17 | 18 | map.put(Days.Monday, "1"); 19 | map.put(Days.Tuesday, "2"); 20 | map.put(Days.Wednesday, "3"); 21 | map.put(Days.Thursday, "4"); 22 | 23 | // print the map 24 | for(Map.Entry m:map.entrySet()){ 25 | System.out.println(m.getKey()+" "+m.getValue()); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/javaConcepts/collection/LinkedListDemo.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.collection; 2 | 3 | import java.util.Deque; 4 | import java.util.LinkedList; 5 | 6 | public class LinkedListDemo { 7 | 8 | public static void main(String[] args) { 9 | LinkedList ll = new LinkedList<>(); 10 | Deque q = new LinkedList<>(); 11 | ll.add(10); 12 | ll.add(20); 13 | ll.add(30); 14 | ll.add(40); 15 | ll.add(50); 16 | 17 | // ll.poll(); 18 | 19 | q.addFirst(5); 20 | q.push(4); 21 | q.pop(); 22 | q.pollFirst(); 23 | ll.addFirst(5); 24 | System.out.println(ll.indexOf(30)); 25 | ll.add(2,-20); 26 | // ll.pollLast(); 27 | System.out.println(ll); 28 | } 29 | 30 | 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/javaConcepts/collection/MyArrayList.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.collection; 2 | 3 | import java.util.Iterator; 4 | import java.util.PriorityQueue; 5 | 6 | /** 7 | * Created by asee2278 on 2/1/17. 8 | */ 9 | 10 | 11 | class Student{ 12 | int rollno; 13 | String name; 14 | int age; 15 | Student(int rollno,String name,int age){ 16 | this.rollno=rollno; 17 | this.name=name; 18 | this.age=age; 19 | } 20 | 21 | 22 | @Override 23 | public int hashCode(){ 24 | return 420; 25 | } 26 | 27 | } 28 | 29 | 30 | public class MyArrayList { 31 | public static void main(String args[]) { 32 | 33 | PriorityQueue queue=new PriorityQueue(); 34 | queue.add("Rahul"); 35 | queue.add("Amit"); 36 | queue.add("Vijay"); 37 | queue.add("Karan"); 38 | queue.add("Jai"); 39 | queue.add("Rahul"); 40 | 41 | System.out.println("head:"+queue.element()); 42 | System.out.println("head:"+queue.peek()); 43 | System.out.println("iterating the queue elements:"); 44 | Iterator itr=queue.iterator(); 45 | while(itr.hasNext()){ 46 | System.out.println(itr.next()); 47 | } 48 | queue.remove(); 49 | queue.poll(); 50 | System.out.println("after removing two elements:"); 51 | Iterator itr2=queue.iterator(); 52 | while(itr2.hasNext()){ 53 | System.out.println(itr2.next()); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/javaConcepts/collection/MyMaps.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.collection; 2 | 3 | import java.util.HashMap; 4 | import java.util.Iterator; 5 | import java.util.Map; 6 | import java.util.Set; 7 | 8 | /** 9 | * Created by asee2278 on 2/1/17. 10 | */ 11 | public class MyMaps { 12 | 13 | public static void main(String[] args) { 14 | 15 | Map map=new HashMap(); 16 | map.put("100","Amit"); 17 | map.put("101","Vijay"); 18 | map.put(null,"aseem"); 19 | map.put(null,null); 20 | 21 | 22 | map.put("102","Aseem"); 23 | 24 | for(Map.Entry m : map.entrySet()){ 25 | System.out.println(m.getKey()+" "+m.getValue()); 26 | } 27 | 28 | Set set=map.entrySet();//Converting to Set so that we can traverse 29 | Iterator itr=set.iterator(); 30 | 31 | while(itr.hasNext()){ 32 | //Converting to Map.Entry so that we can get key and value separately 33 | Map.Entry entry=(Map.Entry)itr.next(); 34 | System.out.println(entry.getKey()+" "+entry.getValue()); 35 | } 36 | 37 | } 38 | 39 | 40 | } 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/javaConcepts/collection/MyTreeMap.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.collection; 2 | 3 | import java.util.Map; 4 | import java.util.TreeMap; 5 | 6 | /** 7 | * Created by asee2278 on 2/1/17. 8 | */ 9 | public class MyTreeMap { 10 | 11 | 12 | public static void main(String[] args) { 13 | TreeMap hm=new TreeMap(); 14 | hm.put(103,"Rahul"); 15 | hm.put(100,"Amit"); 16 | hm.put(102,"Ravi"); 17 | hm.put(33,"Vijay"); 18 | 19 | hm.remove(10233); 20 | 21 | for(Map.Entry m:hm.entrySet()){ 22 | System.out.println(m.getKey()+" "+m.getValue()); 23 | } 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /src/javaConcepts/collection/comparator/Client.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.collection.comparator; 2 | 3 | /** 4 | * Created by asee2278 on 2/4/17. 5 | */ 6 | public class Client { 7 | } 8 | -------------------------------------------------------------------------------- /src/javaConcepts/exceptions/TryMe.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.exceptions; 2 | 3 | /** 4 | * Created by asee2278 on 2/7/17. 5 | */ 6 | public class TryMe { 7 | public static void main(String[] args) { 8 | Integer data = null; 9 | Integer balance = 0; 10 | int x = 1; 11 | try{ 12 | System.out.println("write some to a file"); 13 | data=50/balance; 14 | // data = null; 15 | 16 | try{ 17 | data.toString(); 18 | }catch(NullPointerException e){ 19 | System.out.println(e); 20 | data = 1; 21 | System.out.println(x); 22 | System.out.println("write default valie to a file"); 23 | } 24 | 25 | }catch(ArithmeticException e){ 26 | System.out.println(e); 27 | data = 1; 28 | }finally { 29 | System.out.println("Run always no matter exception occures or not ... "); 30 | System.out.println("close the file "); 31 | } 32 | 33 | System.out.println("rest of the code..."+data); 34 | } 35 | 36 | } 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/javaConcepts/failSafe/FailFastIteratorExample.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.failSafe; 2 | 3 | /** 4 | * Created by asee2278 on 2/13/17. 5 | */ 6 | import java.util.ArrayList; 7 | import java.util.Iterator; 8 | 9 | public class FailFastIteratorExample 10 | { 11 | public static void main(String[] args) 12 | { 13 | //Creating an ArrayList of integers 14 | 15 | ArrayList list = new ArrayList(); 16 | 17 | // 10 -> 20 18 | 19 | //Adding elements to list 20 | 21 | list.add(1452); 22 | 23 | list.add(6854); 24 | 25 | list.add(8741); 26 | 27 | list.add(6542); 28 | 29 | list.add(3845); 30 | 31 | //Getting an Iterator from list 32 | 33 | Iterator it = list.iterator(); 34 | 35 | while (it.hasNext()) 36 | { 37 | Integer integer = (Integer) it.next(); 38 | 39 | // list.add(8457); //This will throw ConcurrentModificationException 40 | } 41 | 42 | list.add(8457); 43 | 44 | 45 | it = list.iterator(); 46 | 47 | while (it.hasNext()) 48 | { 49 | Integer integer = (Integer) it.next(); 50 | 51 | // list.add(8457); //This will throw ConcurrentModificationException 52 | } 53 | 54 | } 55 | } -------------------------------------------------------------------------------- /src/javaConcepts/garbagecollection/GC.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.garbagecollection; 2 | 3 | /** 4 | * Created by asee2278 on 2/6/17. 5 | */ 6 | public class GC { 7 | public void finalize(){ 8 | System.out.println("object is garbage collected"); 9 | } 10 | public static void main(String args[]){ 11 | GC s1=new GC(); 12 | GC s2=new GC(); 13 | s1=null; 14 | 15 | System.gc(); 16 | } 17 | } -------------------------------------------------------------------------------- /src/javaConcepts/human/Client.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.human; 2 | 3 | /** 4 | * Created by asee2278 on 1/28/17. 5 | */ 6 | public class Client { 7 | 8 | public static void main(String[] args) { 9 | 10 | Person premAseem = new Person(); 11 | 12 | premAseem.setName(" premaseem "); 13 | premAseem.setAge(500); 14 | 15 | premAseem.eat(" vegetarian - salad"); 16 | premAseem.personDeatils(); 17 | 18 | GenderFemale sphoorti = new GenderFemale(); 19 | sphoorti.eat("fish "); 20 | sphoorti.setAge(21); 21 | sphoorti.setName("spoorthi"); 22 | sphoorti.personDeatils(); 23 | 24 | GenderMale naren = new GenderMale(); 25 | naren.eat(" veg "); 26 | naren.setAge(20); 27 | naren.setName("naren"); 28 | naren.personDeatils(); 29 | 30 | } 31 | 32 | 33 | 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/javaConcepts/human/GenderFemale.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.human; 2 | 3 | /** 4 | * Created by asee2278 on 1/28/17. 5 | */ 6 | public class GenderFemale extends Person{ 7 | 8 | String dressUp = "frock"; 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /src/javaConcepts/human/GenderMale.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.human; 2 | 3 | /** 4 | * Created by asee2278 on 1/28/17. 5 | */ 6 | public class GenderMale extends Person{ 7 | 8 | String dressUp = "Pent and Shirt"; 9 | } 10 | -------------------------------------------------------------------------------- /src/javaConcepts/human/Person.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.human; 2 | 3 | /** 4 | * Created by asee2278 on 1/28/17. 5 | * This class is responsible to define a person. 6 | */ 7 | public class Person { 8 | 9 | private String name = null; 10 | 11 | public void setAge(Integer age) { 12 | 13 | if(age <0){ 14 | System.out.println("Since persons age cannot be negative, setting the default value to 0"); 15 | this.age=0; 16 | 17 | return; 18 | } 19 | 20 | if(age >100){ 21 | System.out.println("Since persons age cannot be more than 100, setting the default value to 40"); 22 | this.age=40; 23 | return; 24 | } 25 | 26 | this.age = age; 27 | } 28 | 29 | private Integer age = null; 30 | private String food = null; 31 | 32 | public void eat(String food){ 33 | this.food= food; 34 | } 35 | 36 | 37 | public void personDeatils(){ 38 | System.out.println(" Name " + name + " has age " + age + " and eats " + food ); 39 | } 40 | 41 | public String getName() { 42 | return name; 43 | } 44 | 45 | public void setName(String name) { 46 | this.name = name; 47 | } 48 | 49 | public void setName(String name,int a) { 50 | this.name = name; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/javaConcepts/innerclass/AnonClass.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.innerclass; 2 | 3 | /** 4 | * Created by asee2278 on 2/8/17. 5 | */ 6 | 7 | abstract class Lady{ 8 | abstract void eat(); 9 | } 10 | 11 | interface i { 12 | public void anyMethod(); 13 | } 14 | 15 | class AnonClass{ 16 | public static void main(String args[]){ 17 | Lady p= new Lady(){ 18 | void eat(){ 19 | System.out.println(" haha just created an object of Annonymus class "); 20 | } 21 | }; 22 | i obj = new i(){ 23 | public void anyMethod(){ 24 | 25 | }; 26 | }; 27 | p.eat(); 28 | } 29 | } -------------------------------------------------------------------------------- /src/javaConcepts/innerclass/MethodLocalInnerClass.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.innerclass; 2 | 3 | /** 4 | * Created by asee2278 on 2/8/17. 5 | */ 6 | 7 | 8 | public class MethodLocalInnerClass { 9 | private int data = 30;//instance variable 10 | 11 | 12 | void display() { 13 | class Local { 14 | void msg() { 15 | System.out.println(data); 16 | class YetAnotherLocal { 17 | } 18 | } 19 | } 20 | Local l = new Local(); 21 | l.msg(); 22 | } 23 | 24 | public static void main(String args[]) { 25 | MethodLocalInnerClass obj = new MethodLocalInnerClass(); 26 | obj.display(); 27 | 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /src/javaConcepts/innerclass/NestedInnerClass.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.innerclass; 2 | 3 | /** 4 | * Created by asee2278 on 2/8/17. 5 | */ 6 | public class NestedInnerClass { 7 | } 8 | 9 | class TestOuter1{ 10 | static int data=30; 11 | 12 | static class Inner{ 13 | void msg(){System.out.println("data is "+data);} 14 | } 15 | public static void main(String args[]){ 16 | Inner obj= new Inner(); 17 | 18 | obj.msg(); 19 | } 20 | } -------------------------------------------------------------------------------- /src/javaConcepts/innerclass/SimpleNestedClass.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.innerclass; 2 | 3 | /** 4 | * Created by asee2278 on 2/8/17. 5 | */ 6 | public class SimpleNestedClass { 7 | private int data = 30; 8 | 9 | class Inner { 10 | void msg() { 11 | System.out.println("data is " + data); 12 | } 13 | class SuperInner { 14 | 15 | void msg() { 16 | System.out.println("data is " + data); 17 | } 18 | class SuperDuperInner { 19 | void msg() { 20 | System.out.println("data is " + data); 21 | } 22 | } 23 | } 24 | } 25 | 26 | 27 | public static void main(String args[]) { 28 | SimpleNestedClass obj = new SimpleNestedClass(); 29 | 30 | Inner in = obj.new Inner(); 31 | Inner.SuperInner si = in.new SuperInner(); 32 | Inner.SuperInner.SuperDuperInner sdi = si.new SuperDuperInner(); 33 | sdi.msg(); 34 | 35 | in.msg(); 36 | } 37 | } 38 | 39 | class Inner { 40 | void msg() { 41 | System.out.println("data is "); 42 | } 43 | } -------------------------------------------------------------------------------- /src/javaConcepts/lists/Collections1.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.lists; 2 | 3 | 4 | import java.util.ArrayList; 5 | import java.util.Collections; 6 | // 7 | 8 | 9 | /** 10 | * Created by asee2278 on 1/30/17. 11 | */ 12 | public class Collections1 { 13 | 14 | 15 | static public ArrayList mySort(ArrayList tempList){ 16 | Collections.sort(tempList); 17 | return tempList; 18 | 19 | } 20 | 21 | 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/javaConcepts/lists/LinkedListDemo.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.lists; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.util.LinkedList; 7 | 8 | public class LinkedListDemo { 9 | 10 | // Test Driven Development by Aseem Jain 11 | @Test 12 | public void test() { 13 | 14 | LinkedList ll = new LinkedList<>(); 15 | ll.getLast(); 16 | 17 | } 18 | 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/javaConcepts/lists/ListExamples.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.lists; 2 | 3 | import java.util.ArrayList; 4 | //import java.util.Collections; 5 | 6 | /** 7 | * Created by asee2278 on 1/30/17. 8 | */ 9 | public class ListExamples { 10 | 11 | 12 | Integer birds = 3; 13 | int animals = 5; 14 | 15 | Integer a = new Integer(animals); 16 | Integer b = new Integer("34"); 17 | Integer sum = 0; 18 | 19 | Float sharePrice = 5.00232f; 20 | boolean flag; 21 | String name = "java training"; 22 | 23 | static int []num = new int[3]; // array 24 | 25 | static ArrayList al = new ArrayList(); 26 | 27 | 28 | 29 | 30 | public static void main(String[] args) { 31 | 32 | ListExamples obj = new ListExamples(); 33 | obj.name = "fullStact"; 34 | // System.out.print(obj.flag); 35 | obj.name = "new Name"; 36 | obj.sum++; 37 | 38 | num[0]=22; 39 | num[1]=20; 40 | num[2]=26; 41 | // num[3]=40; 42 | 43 | al.add(13); 44 | al.add(3); 45 | al.add(3); 46 | al.add(3); 47 | al.add(300); 48 | al.add(3); 49 | al.add(3); 50 | al.add(23); 51 | al.add(3); 52 | al.add(3); 53 | // Collections.reverse(al); 54 | // Collections.sort(al); 55 | // Collections1 coll = new Collections1(); 56 | 57 | Collections1.mySort(al); 58 | 59 | System.out.print(al); 60 | 61 | 62 | 63 | 64 | 65 | } 66 | 67 | 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/javaConcepts/serialization/Depersist.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.serialization; 2 | 3 | /** 4 | * Created by asee2278 on 2/14/17. 5 | */ 6 | import java.io.*; 7 | class Depersist{ 8 | public static void main(String args[])throws Exception{ 9 | 10 | ObjectInputStream in=new ObjectInputStream(new FileInputStream("objectOut.txt")); 11 | 12 | Student s=(Student) in.readObject(); 13 | System.out.println(s.id+" "+s.name + " " +s.marks); 14 | 15 | in.close(); 16 | } 17 | } -------------------------------------------------------------------------------- /src/javaConcepts/serialization/Persist.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.serialization; 2 | 3 | /** 4 | * Created by asee2278 on 2/14/17. 5 | */ 6 | 7 | import java.io.*; 8 | class Persist{ 9 | public static void main(String args[])throws Exception{ 10 | Student s1 =new Student(211,"Naren"); 11 | // Student sw =new Student(222,"Spoort"); 12 | 13 | FileOutputStream fout=new FileOutputStream("objectOut.txt"); 14 | ObjectOutputStream out=new ObjectOutputStream(fout); 15 | 16 | out.writeObject(s1); 17 | out.flush(); 18 | System.out.println("success"); 19 | } 20 | } -------------------------------------------------------------------------------- /src/javaConcepts/serialization/Student.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.serialization; 2 | 3 | /** 4 | * Created by asee2278 on 2/14/17. 5 | */ 6 | import java.io.Serializable; 7 | public class Student implements Serializable{ 8 | int id; 9 | String name; 10 | Integer marks; 11 | public Student(int id, String name) { 12 | this.id = id; 13 | this.name = name; 14 | this.marks = 100; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /src/javaConcepts/set/SetEample.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.set; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | /** 8 | * Created by asee2278 on 1/31/17. 9 | */ 10 | 11 | 12 | 13 | class SetEample{ 14 | public static void main(String args[]){ 15 | //Creating HashSet and adding elements 16 | // Set set=new HashSet(); 17 | List set = new ArrayList(); 18 | set.add("Ravi"); 19 | set.add("Vijay"); 20 | set.add("Vijay"); 21 | set.add("Ravi"); 22 | set.add("Ajay"); 23 | //Traversing elements 24 | Iterator itr=set.iterator(); 25 | while(itr.hasNext()){ 26 | System.out.println(itr.next()); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /src/javaConcepts/threads/MyMultiThread.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.threads; 2 | 3 | /** 4 | * Created by asee2278 on 2/6/17. 5 | */ 6 | 7 | 8 | // This is job for Data migration 9 | class Job implements Runnable{ 10 | 11 | @Override 12 | public void run() { 13 | System.out.println("copy from a "); 14 | System.out.println("paste in b "); 15 | } 16 | 17 | public void sleep() { 18 | System.out.println("slept for a while "); 19 | } 20 | 21 | 22 | 23 | } 24 | 25 | public class MyMultiThread { 26 | 27 | 28 | public static void main(String args[]){ 29 | // Thread t1 =new Thread(new Job()); 30 | // Thread t2 =new Thread(new Job()); 31 | // Thread t3 =new Thread(new Job()); 32 | 33 | Job job1 = new Job(); 34 | Job job2 = new Job(); 35 | Job job3 = new Job(); 36 | 37 | Thread t1 = new Thread(job1); 38 | Thread t2 = new Thread(job2); 39 | Thread t3 = new Thread(job3); 40 | 41 | t1.start(); 42 | t2.start(); 43 | t3.start(); 44 | 45 | 46 | // job1.run(); 47 | // job1.sleep(); 48 | // job2.run(); 49 | // job2.sleep(); 50 | // job3.run(); 51 | // job3.sleep(); 52 | 53 | 54 | // t1.start(); 55 | // t2.start(); 56 | // t3.start(); 57 | 58 | } 59 | } -------------------------------------------------------------------------------- /src/javaConcepts/threads/MyThreadUsingInterface.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.threads; 2 | 3 | /** 4 | * Created by asee2278 on 2/5/17. 5 | */ 6 | 7 | class MyThreadUsingInterface implements Runnable{ 8 | 9 | public void run(){ 10 | System.out.println("thread is running..."); 11 | System.out.println("thread's task is to bring medicne ..."); 12 | 13 | } 14 | 15 | public static void main(String args[]){ 16 | MyThreadUsingInterface m1=new MyThreadUsingInterface(); 17 | Thread t1 =new Thread(m1); 18 | t1.start(); 19 | } 20 | } -------------------------------------------------------------------------------- /src/javaConcepts/threads/TestThreadPool.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.threads; 2 | 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | class WorkerThread1 implements Runnable { 6 | private String message; 7 | public WorkerThread1(String s){ 8 | this.message=s; 9 | } 10 | public void run() { 11 | System.out.println(Thread.currentThread().getName()+" (Start) message = "+message); 12 | processmessage();//call processmessage method that sleeps the thread for 2 seconds 13 | System.out.println(Thread.currentThread().getName()+" (End)");//prints thread name 14 | } 15 | private void processmessage() { 16 | try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } 17 | } 18 | } 19 | 20 | 21 | 22 | public class TestThreadPool { 23 | public static void main(String[] args) { 24 | ExecutorService executor = Executors.newFixedThreadPool(5);//creating a pool of 5 threads 25 | 26 | for (int i = 0; i < 10; i++) { 27 | Runnable worker = new WorkerThread1(""); 28 | executor.execute(worker);//calling execute method of ExecutorService 29 | } 30 | executor.shutdown(); 31 | while (!executor.isTerminated()) { } 32 | 33 | System.out.println("Finished all threads"); 34 | } 35 | } -------------------------------------------------------------------------------- /src/javaConcepts/threads/ThreadGroupDemo.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.threads; 2 | 3 | /** 4 | * Created by asee2278 on 2/6/17. 5 | */ 6 | public class ThreadGroupDemo implements Runnable{ 7 | public void run() { 8 | System.out.println(Thread.currentThread().getName()); 9 | } 10 | public static void main(String[] args) { 11 | ThreadGroupDemo runnable = new ThreadGroupDemo(); 12 | ThreadGroup tg1 = new ThreadGroup("Parent ThreadGroup"); 13 | 14 | Thread t1 = new Thread(tg1, runnable,"one"); 15 | t1.start(); 16 | Thread t2 = new Thread(tg1, runnable,"two"); 17 | t2.start(); 18 | Thread t3 = new Thread(tg1, runnable,"three"); 19 | t3.start(); 20 | 21 | System.out.println("Thread Group Name: "+tg1.getName()); 22 | tg1.list(); 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /src/javaConcepts/threads/TreadJoin.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.threads; 2 | 3 | /** 4 | * Created by asee2278 on 2/6/17. 5 | */ 6 | 7 | class TreadJoin extends Thread{ 8 | public void run(){ 9 | 10 | if(Thread.currentThread().isDaemon()){//checking for daemon thread 11 | System.out.println("daemon thread work"); 12 | } 13 | 14 | else { 15 | 16 | for(int i=1;i<=5;i++){ 17 | try{ 18 | Thread.sleep(500); 19 | System.out.println(Thread.currentThread().getName()); 20 | }catch(Exception e){System.out.println(e);} 21 | System.out.println(i); 22 | } 23 | } 24 | } 25 | public static void main(String args[]){ 26 | TreadJoin t1=new TreadJoin(); 27 | t1.setName("Monitor Thread "); 28 | t1.setPriority(Thread.MAX_PRIORITY); 29 | 30 | TreadJoin t2=new TreadJoin(); 31 | t2.setName("Normal student "); 32 | t2.setPriority(Thread.NORM_PRIORITY); 33 | 34 | TreadJoin t3=new TreadJoin(); 35 | t3.setName("Dumb student "); 36 | t3.setPriority(Thread.MIN_PRIORITY); 37 | 38 | 39 | t1.start(); 40 | t1.setDaemon(true); 41 | 42 | try{ 43 | t1.join(1000); 44 | }catch(Exception e){System.out.println(e);} 45 | 46 | t2.start(); 47 | t3.start(); 48 | } 49 | } -------------------------------------------------------------------------------- /src/javaConcepts/variableShadowing/Bike.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.variableShadowing; 2 | 3 | /** 4 | * Created by asee2278 on 1/29/17. 5 | */ 6 | public class Bike extends Vehicle{ 7 | 8 | int noOfWheel = 2; 9 | 10 | String engineSound(){ 11 | 12 | return "tuk tuk"; 13 | } 14 | 15 | public String villy(){ 16 | return "Bike stunt"; 17 | } 18 | 19 | @Override 20 | public String stunt(){ 21 | return super.stunt(); 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | 27 | return " vars " + this.noOfWheel +" "+ getClass().getName(); 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/javaConcepts/variableShadowing/Car.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.variableShadowing; 2 | 3 | /** 4 | * Created by asee2278 on 1/29/17. 5 | */ 6 | public class Car extends Vehicle { 7 | 8 | public String skidAndTurn(){ 9 | return "Bike stunt"; 10 | } 11 | 12 | String engineSound(){ 13 | 14 | return "Voo voo "; 15 | } 16 | 17 | public String stunt(){ 18 | return skidAndTurn(); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/javaConcepts/variableShadowing/Client.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.variableShadowing; 2 | 3 | /** 4 | * Created by asee2278 on 1/29/17. 5 | */ 6 | public class Client { 7 | 8 | 9 | public static void main(String[] args) { 10 | 11 | Bike vechile = new Bike(); 12 | Vehicle vBike = new Bike(); 13 | Vehicle truck = new Truck(); 14 | System.out.println(truck.engineSound()); 15 | 16 | 17 | 18 | 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/javaConcepts/variableShadowing/Truck.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.variableShadowing; 2 | 3 | /** 4 | * Created by asee2278 on 1/29/17. 5 | */ 6 | public class Truck extends Vehicle{ 7 | 8 | 9 | @Override 10 | String engineSound() { 11 | return "Ghoo Ghoo"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/javaConcepts/variableShadowing/Vehicle.java: -------------------------------------------------------------------------------- 1 | package javaConcepts.variableShadowing; 2 | 3 | /** 4 | * Created by asee2278 on 1/29/17. 5 | */ 6 | public abstract class Vehicle extends Object{ 7 | 8 | int noOfWheel=4; 9 | 10 | abstract String engineSound(); 11 | 12 | public String stunt(){ 13 | return "Vehile stunt"; 14 | } 15 | 16 | 17 | @Override 18 | public String toString() { 19 | 20 | return " vars " + noOfWheel +" "+ getClass().getName(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/me/premaseem/MyUtils.java: -------------------------------------------------------------------------------- 1 | package me.premaseem; 2 | 3 | public class MyUtils { 4 | 5 | static public int[] getIntArr() { 6 | int[] arr = new int[]{70, -20, 30, 0, 40, 100}; 7 | System.out.print("Unsorted array looks like => "); 8 | printArr(arr); 9 | return arr; 10 | } 11 | 12 | static public void printArr(int[] arr) { 13 | System.out.print("[ "); 14 | for (int e : arr) { 15 | System.out.print(e + " "); 16 | } 17 | System.out.println("]"); 18 | 19 | } 20 | 21 | static public boolean isArrSorted(int[] ar) { 22 | System.out.print("Is array sorted? ... "); 23 | printArr(ar); 24 | for (int i = 0; i < ar.length - 1; i++) { 25 | if (ar[i + 1] < ar[i]) { 26 | System.out.println("No, problem is " + ar[i + 1] + " > " + ar[i]); 27 | assert false; 28 | } 29 | 30 | } 31 | 32 | System.out.println("Yes"); 33 | return true; 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/me/premaseem/algorithm/binarySearch/Test.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.algorithm.binarySearch; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class Test { 6 | 7 | SearchApp binarySearcher = new SearchApp(); 8 | @org.junit.Test 9 | public void testBinarySearch(){ 10 | int[] arr1 = {1,3,6,10,12,15,19,22,28,50,60,99,102}; 11 | assertEquals(3,binarySearcher.binarySearch(arr1, 11)); 12 | assertEquals(0,binarySearcher.binarySearch(arr1, 1)); 13 | 14 | assertEquals(-1,binarySearcher.binarySearch(arr1, 103)); 15 | assertEquals(7,binarySearcher.binarySearch(arr1, 22)); 16 | assertEquals(9,binarySearcher.binarySearch(arr1, 50)); 17 | } 18 | 19 | @org.junit.Test 20 | public void testBinarySearchRecursive(){ 21 | int[] arr1 = {10,20,30,40,50,60,70,80,90,100}; 22 | assertEquals(2,binarySearcher.binarySearchRecursive(arr1, 0,9,30)); 23 | assertEquals(9,binarySearcher.binarySearchRecursive(arr1, 0,9,100)); 24 | assertEquals(4,binarySearcher.binarySearchRecursive(arr1, 0,9,50)); 25 | assertEquals(-1,binarySearcher.binarySearchRecursive(arr1, 0,9,32)); 26 | } 27 | 28 | @org.junit.Test 29 | public void testLinearSearchRecursive(){ 30 | int[] arr1 = {10,20,30,40,50,60,70,80,90,100}; 31 | assertEquals(2,binarySearcher.linearSearchRecursive(arr1, 0,30)); 32 | assertEquals(-1,binarySearcher.linearSearchRecursive(arr1, 0,31)); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/me/premaseem/algorithm/bubblesort/BubbleSort.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.algorithm.bubblesort; 2 | 3 | import me.premaseem.MyUtils; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | public class BubbleSort { 8 | 9 | // Test Driven Development by Aseem Jain 10 | @Test 11 | public void test() { 12 | 13 | int[] a = MyUtils.getIntArr(); 14 | sort(a); 15 | 16 | MyUtils.isArrSorted(a); 17 | 18 | } 19 | 20 | public void sort(int[] a){ 21 | 22 | for (int i = 0; i < a.length ; i++) { 23 | for (int j = 0; j < a.length -1 ; j++) { 24 | 25 | if(a[j] > a[j+1]){ 26 | System.out.println( a[j] + " " + a[j+1]); 27 | int swap = a[j]; 28 | a[j] = a[j+1]; 29 | a[j+1] = swap; 30 | } 31 | 32 | } 33 | 34 | } 35 | 36 | } 37 | 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/me/premaseem/algorithm/bubblesort/bubble-sort.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/src/me/premaseem/algorithm/bubblesort/bubble-sort.png -------------------------------------------------------------------------------- /src/me/premaseem/algorithm/insersionSort/InsertionSort.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.algorithm.insersionSort; 2 | 3 | import me.premaseem.MyUtils; 4 | import org.junit.Test; 5 | 6 | public class InsertionSort { 7 | // Test Driven Development by Aseem Jain 8 | @Test 9 | public void test() { 10 | 11 | int[] a = MyUtils.getIntArr(); 12 | sort(a); 13 | MyUtils.isArrSorted(a); 14 | 15 | } 16 | 17 | public void sort(int[] a) { 18 | 19 | for (int i = 1; i < a.length; i++) { 20 | 21 | // insertion key 22 | int ik = a[i]; 23 | int j = i - 1; 24 | 25 | // compare the sorted array with the insertion key and 26 | // shift to right to make place for the insertion key. 27 | while (j >= 0 && a[j] > ik) { 28 | a[j + 1] = a[j]; 29 | j--; 30 | } 31 | a[j + 1] = ik; 32 | 33 | } 34 | 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/me/premaseem/algorithm/insersionSort/insertion sort.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/src/me/premaseem/algorithm/insersionSort/insertion sort.jpg -------------------------------------------------------------------------------- /src/me/premaseem/algorithm/recursion/RecursiveAlgo.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.algorithm.recursion; 2 | 3 | public class RecursiveAlgo { 4 | 5 | 6 | public void downCounter(int seconds){ 7 | if(seconds != 0) { 8 | System.out.println("time remaining "+ seconds); 9 | downCounter(seconds - 1); 10 | } 11 | } 12 | 13 | public int factorialWithRecursion(int number) { 14 | int factorial =1; 15 | if (number != 1) { 16 | factorial = number * factorialWithRecursion(number - 1); 17 | } 18 | return factorial; 19 | } 20 | 21 | public int factorialWithLoop(int n){ 22 | int fact = 1; 23 | for (int i=1; i<=n; i++){ 24 | fact = fact * i; 25 | } 26 | return fact; 27 | } 28 | 29 | public int fibonacciRecursion(int n){ 30 | if (n <= 1){ 31 | return n; 32 | } 33 | return fibonacciRecursion(n-1)+ fibonacciRecursion(n-2); 34 | 35 | } 36 | 37 | public int fibonacciWithLoop(int series){ 38 | int arr[] = new int[100]; 39 | arr[0]=0; 40 | arr[1]=1; 41 | for(int i=0;i arr[i + 1]) { 15 | System.out.println("unsorted"); 16 | assert false; 17 | } 18 | } 19 | } 20 | 21 | void selectionSort(int[] a) { 22 | 23 | for (int i = 0; i < a.length; i++) { 24 | int selectMinIndex = i; 25 | for (int j = i; j < a.length; j++) { 26 | if(a[j] < a[selectMinIndex]){ 27 | selectMinIndex = j; 28 | } 29 | } 30 | int swap = a[i]; 31 | a[i] = a[selectMinIndex]; 32 | a[selectMinIndex] = swap; 33 | 34 | } 35 | }} 36 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/binaryTree/binaryTreeArray/BinaryTreeArrMain.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.binaryTree.binaryTreeArray; 2 | 3 | public class BinaryTreeArrMain { 4 | 5 | 6 | public static void main(String[] args) { 7 | BinaryTreeArr bsta = new BinaryTreeArr(15); 8 | 9 | // insert 10 | for (int i = 1; i <=15 ; i++) { 11 | System.out.println("inserted " +bsta.insert(i)); 12 | } 13 | 14 | 15 | // search 16 | System.out.println("search result "+bsta.search(3)); 17 | System.out.println("search result "+bsta.search(17)); 18 | 19 | // delete 20 | // System.out.println("delete result "+bsta.delete(7)); 21 | // System.out.println("delete result "+bsta.delete(29)); 22 | 23 | // pre order traversal 24 | bsta.pre(1); 25 | System.out.println(); 26 | // In order traversal 27 | bsta.in(1); 28 | System.out.println(); 29 | // Post order traversal 30 | bsta.post(1); 31 | System.out.println(); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/binaryTree/binaryTreeLinkedList/BinaryTreeLinkedListMain.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.binaryTree.binaryTreeLinkedList; 2 | 3 | public class BinaryTreeLinkedListMain { 4 | 5 | public static void main(String[] args) { 6 | 7 | BinaryTreeLinkedList bt = new BinaryTreeLinkedList(); 8 | for (int i = 1; i < 15; i++) { 9 | bt.insert(i); 10 | } 11 | 12 | bt.search(14); 13 | bt.search(34); 14 | bt.inLine(bt.root); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/doublelinkedlist/Node.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.doublelinkedlist; 2 | 3 | public class Node { 4 | public Node(int data){ 5 | this.data = data; 6 | } 7 | int data; 8 | Node next; 9 | Node previous; 10 | } 11 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/doublelinkedlist/Tests.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.doublelinkedlist; 2 | 3 | import org.junit.Test; 4 | 5 | import static junit.framework.TestCase.assertTrue; 6 | import static org.junit.Assert.assertEquals; 7 | import static org.junit.Assert.assertFalse; 8 | 9 | public class Tests { 10 | 11 | @Test 12 | public void testInsert(){ 13 | DoubleLinkedList dll = new DoubleLinkedList(); 14 | dll.insert(10); 15 | dll.insert(20); 16 | dll.insert(30); 17 | dll.printForward(); 18 | assertEquals(3,dll.length()); 19 | dll.printBackwords(); 20 | } 21 | 22 | @Test 23 | public void testSingleItemPrints(){ 24 | DoubleLinkedList dll = new DoubleLinkedList(); 25 | dll.insert(10); 26 | dll.printForward(); 27 | assertEquals(1,dll.length()); 28 | dll.printBackwords(); 29 | } 30 | 31 | @Test 32 | public void testDelete(){ 33 | DoubleLinkedList dll = new DoubleLinkedList(); 34 | dll.insert(10); 35 | dll.insert(20); 36 | dll.insert(30); 37 | dll.insert(40); 38 | assertFalse(dll.deleteNodeWith(55)); 39 | 40 | assertTrue(dll.deleteNodeWith(30)); 41 | dll.printForward(); 42 | 43 | assertTrue(dll.insertNodeAfter(20,100)); 44 | dll.printForward(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/graph/busRoute/Graph.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.graph.busRoute; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Graph { 6 | 7 | ArrayList graphArray[]; 8 | 9 | public Graph(int numOfVertices) { 10 | graphArray = new ArrayList[numOfVertices]; 11 | for (int i = 0; i < graphArray.length; i++) { 12 | graphArray[i] = new ArrayList(); 13 | } 14 | 15 | } 16 | 17 | public void addEdge(int from, int to) { 18 | graphArray[from].add(to); 19 | } 20 | 21 | public void getAdjecents(int vertex) { 22 | ArrayList edges = graphArray[vertex]; 23 | for (Object i : edges) { 24 | System.out.printf(" %s --> %s \n", vertex, i); 25 | } 26 | } 27 | 28 | public void printGraphEdges() { 29 | for (int i = 0; i < graphArray.length; i++) { 30 | for (Object obj : graphArray[i]) { 31 | System.out.printf(" %s --> %s \n", i, obj); 32 | } 33 | } 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/graph/busRoute/Test.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.graph.busRoute; 2 | 3 | public class Test { 4 | 5 | @org.junit.Test 6 | public void testGraph(){ 7 | Graph graph = new Graph(5); 8 | graph.addEdge(0,1); 9 | graph.addEdge(0,5); 10 | graph.addEdge(1,2); 11 | graph.addEdge(2,3); 12 | graph.addEdge(3,4); 13 | graph.addEdge(4,5); 14 | 15 | graph.getAdjecents(0); 16 | graph.printGraphEdges(); 17 | } 18 | 19 | @org.junit.Test 20 | public void testBusRoutes(){ 21 | BusRoute myGraph = new BusRoute(5,"directed"); 22 | 23 | myGraph.addVertex("State"); 24 | myGraph.addVertex("Avenel"); 25 | myGraph.addVertex("Elm"); 26 | myGraph.addVertex("Pocono"); 27 | myGraph.addVertex("William"); 28 | 29 | myGraph.addRoute("State", "Avenel"); 30 | myGraph.addRoute("State", "Elm"); 31 | myGraph.addRoute("State", "Pocono"); 32 | myGraph.addRoute("Avenel", "Pocono"); 33 | myGraph.addRoute("Elm", "Avenel"); 34 | myGraph.addRoute("Elm", "William"); 35 | myGraph.addRoute("William", "State"); 36 | myGraph.addRoute("William", "Pocono"); 37 | myGraph.addRoute("Pocono", "Elm"); 38 | 39 | myGraph.printRoutes(); 40 | } 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/hashtable/hashmap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/premaseem/AlgorithmAndDataStructureInJava/6d249f0db9d6896aef8010569d1807d51f6a1d5c/src/me/premaseem/datastructure/hashtable/hashmap.png -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/heap/MinHeap.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.heap; 2 | 3 | public class MinHeap { 4 | 5 | 6 | } 7 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/heap/imp1/Node.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.heap.imp1; 2 | 3 | public class Node { 4 | int key; 5 | public Node(int key){ 6 | this.key = key; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/heap/imp1/Test.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.heap.imp1; 2 | 3 | public class Test { 4 | Heap heap = new Heap(20); 5 | 6 | @org.junit.Test 7 | public void insertTest(){ 8 | heap.insert(10); 9 | heap.insert(100); 10 | heap.insert(20); 11 | // heap.print(); 12 | 13 | heap.insert(50); 14 | heap.insert(15); 15 | heap.insert(35); 16 | heap.insert(45); 17 | heap.insert(5); 18 | 19 | heap.insert(51); 20 | heap.insert(11); 21 | heap.insert(351); 22 | heap.insert(451); 23 | heap.insert(51); 24 | heap.insert(9); 25 | heap.insert(10); 26 | // heap.print(); 27 | heap.printTree(); 28 | // heap.displayHeap1(); 29 | 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/simplequeue/CircularQueueTests.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.simplequeue; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public class CircularQueueTests { 8 | 9 | CustomCircularQueue ccq; 10 | 11 | // you can insert in never ending loop 12 | @Test 13 | public void testInsert(){ 14 | ccq = new CustomCircularQueue(4); 15 | assertEquals(0, ccq.peakRear().longValue()); 16 | ccq.insert(10); 17 | ccq.insert(20); 18 | ccq.insert(30); 19 | ccq.insert(40); 20 | assertEquals(40, ccq.peakRear().longValue()); 21 | ccq.insert(45); 22 | assertEquals(45, ccq.peakRear().longValue()); 23 | ccq.insert(10); 24 | ccq.insert(20); 25 | ccq.insert(30); 26 | ccq.insert(560); 27 | assertEquals(560, ccq.peakRear().longValue()); 28 | } 29 | 30 | @Test 31 | public void testRemoval(){ 32 | ccq = new CustomCircularQueue(4); 33 | ccq.insert(10); 34 | ccq.insert(20); 35 | ccq.remove(); 36 | assertEquals(20,ccq.peakFront()); 37 | assertEquals(1,ccq.queueLenght()); 38 | ccq.insert(30); 39 | ccq.insert(40); 40 | assertEquals(20,ccq.peakFront()); 41 | 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/simplequeue/CustomCircularQueue.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.simplequeue; 2 | 3 | public class CustomCircularQueue extends CustomQueue { 4 | 5 | public CustomCircularQueue(int maxSize) { 6 | super(maxSize); 7 | } 8 | 9 | public void insert(int value) { 10 | queueArray[rear] = value; 11 | items++; 12 | if(rear == maxSize-1){ 13 | rear = 0; 14 | // front++; 15 | }else{ 16 | rear++; 17 | 18 | } 19 | } 20 | 21 | public Long peakRear(){ 22 | if(rear == 0){ 23 | return queueArray[maxSize -1]; 24 | } 25 | return queueArray[rear-1]; 26 | } 27 | 28 | public void remove(){ 29 | front++; 30 | items--; 31 | if(front == maxSize){ 32 | front =0; 33 | } 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/simplequeue/CustomQueue.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.simplequeue; 2 | 3 | public class CustomQueue { 4 | 5 | int front=0, rear=0, items =0, maxSize=10; 6 | 7 | long[] queueArray; 8 | 9 | public CustomQueue(int maxSize){ 10 | this.maxSize = maxSize; 11 | queueArray = new long[maxSize]; 12 | } 13 | 14 | public void insert(int value) throws Exception { 15 | if(rear == maxSize-1){ 16 | System.out.printf("Cannot insert as Queue is full"); 17 | throw new Exception("Cannot insert"); 18 | }else { 19 | queueArray[rear] = value; 20 | rear++; 21 | items++; 22 | } 23 | } 24 | 25 | public void remove(){ 26 | if(front < rear && front < maxSize){ 27 | front++; 28 | items--; 29 | }else{ 30 | System.out.println("no element to remove"); 31 | 32 | } 33 | } 34 | 35 | public long peakFront(){ 36 | return queueArray[front]; 37 | } 38 | 39 | public Long peakRear(){ 40 | if(rear == 0 ){ 41 | return null; 42 | } 43 | return queueArray[rear-1]; 44 | } 45 | 46 | public void printQueue(){ 47 | for(int i=front; i= sa.length){ 22 | return false; 23 | } 24 | pointer++; 25 | sa[pointer] = e; 26 | return true; 27 | } 28 | 29 | boolean isEmpty(){ 30 | return pointer < 0; 31 | } 32 | 33 | int pop(){ 34 | 35 | int v = sa[pointer]; 36 | pointer--; 37 | return v; 38 | } 39 | 40 | boolean search(int e){ 41 | for ( int item : sa){ 42 | if (item == e){ 43 | return true; 44 | } 45 | } 46 | return false; 47 | } 48 | 49 | // Test Driven Development by Aseem Jain 50 | @Test 51 | public void test() { 52 | 53 | StackWithArray sa = new StackWithArray(); 54 | assert sa.isEmpty(); 55 | assert sa.push(7); 56 | assert sa.push(4); 57 | 58 | assert sa.search(7); 59 | assert ! sa.search(6); 60 | 61 | assert ! sa.isEmpty(); 62 | assert 4 == sa.pop(); 63 | assert 7 == sa.pop(); 64 | assert sa.isEmpty(); 65 | 66 | 67 | 68 | 69 | } 70 | } 71 | 72 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/stacks/StackWithArrayList.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.stacks; 2 | 3 | 4 | // Test Driven Development by Aseem Jain 5 | 6 | import org.junit.Test; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | 12 | public class StackWithArrayList { 13 | 14 | List sa; 15 | Integer count = -1; 16 | 17 | public StackWithArrayList() { 18 | sa = new ArrayList<>(); 19 | 20 | } 21 | 22 | void push(E e) { 23 | sa.add(e); 24 | count++; 25 | } 26 | 27 | boolean isEmpty() { 28 | return count < 0; 29 | } 30 | 31 | E pop() { 32 | E e = sa.get(count); 33 | sa.remove(count); 34 | count--; 35 | return e; 36 | } 37 | 38 | boolean search(E e) { 39 | return sa.contains(e); 40 | } 41 | 42 | // Test Driven Development by Aseem Jain 43 | @Test 44 | public void test() { 45 | 46 | StackWithArrayList sa = new StackWithArrayList(); 47 | assert sa.isEmpty(); 48 | 49 | sa.push(7); 50 | assert !sa.isEmpty(); 51 | assert sa.search(7); 52 | 53 | assert 7 == sa.pop(); 54 | assert sa.isEmpty(); 55 | 56 | } 57 | } 58 | 59 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/stacks/StackWithCustomizedLinkedList.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.stacks; 2 | 3 | 4 | // Test Driven Development by Aseem Jain 5 | 6 | import org.junit.Test; 7 | 8 | 9 | class StackNode { 10 | public StackNode(E e) { 11 | this.data = e; 12 | } 13 | 14 | E data; 15 | StackNode next; 16 | 17 | } 18 | 19 | 20 | public class StackWithCustomizedLinkedList { 21 | 22 | StackNode top = null; 23 | 24 | 25 | boolean isEmpty() { 26 | return top == null; 27 | } 28 | 29 | void push(E e) { 30 | StackNode t = new StackNode(e); 31 | t.next = top; 32 | top = t; 33 | } 34 | 35 | E pop() { 36 | StackNode t = top; 37 | if (top != null) { 38 | top = top.next; 39 | } 40 | return (E) t.data; 41 | } 42 | 43 | boolean search(E e) { 44 | StackNode t = top; 45 | while (t != null) { 46 | if (t.data == e) 47 | return true; 48 | t = t.next; 49 | } 50 | 51 | return false; 52 | } 53 | 54 | // Test Driven Development by Aseem Jain 55 | @Test 56 | public void test() { 57 | 58 | StackWithCustomizedLinkedList sa = new StackWithCustomizedLinkedList(); 59 | assert sa.isEmpty(); 60 | 61 | sa.push(7); 62 | sa.push(9); 63 | assert !sa.isEmpty(); 64 | assert sa.search(7); 65 | assert !sa.search(61); 66 | assert 9 == sa.pop(); 67 | assert 7 == sa.pop(); 68 | assert sa.isEmpty(); 69 | 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/stacks/StackWithLinkedList.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.stacks; 2 | 3 | 4 | // Test Driven Development by Aseem Jain 5 | 6 | import org.junit.Test; 7 | 8 | 9 | public class StackWithLinkedList { 10 | 11 | java.util.LinkedList ll = new java.util.LinkedList<>(); 12 | 13 | boolean isEmpty(){ 14 | return ll.isEmpty(); 15 | } 16 | 17 | void push(E e){ 18 | ll.push(e); 19 | } 20 | 21 | boolean search(E e){ 22 | return -1 != ll.indexOf(e); 23 | } 24 | 25 | E pop(){ 26 | return ll.pop(); 27 | } 28 | 29 | // Test Driven Development by Aseem Jain 30 | @Test 31 | public void test() { 32 | 33 | StackWithLinkedList sa = new StackWithLinkedList(); 34 | assert sa.isEmpty(); 35 | 36 | sa.push(7); 37 | sa.push(9); 38 | assert !sa.isEmpty(); 39 | assert sa.search(7); 40 | 41 | assert 9 == sa.pop(); 42 | assert 7 == sa.pop(); 43 | assert sa.isEmpty(); 44 | 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/stacks/simplestack/App.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.stacks.simplestack; 2 | 3 | public class App { 4 | public static void main(String... args) { 5 | System.out.println("custom stack implementation"); 6 | CustomStack theStack = new CustomStack(5); 7 | theStack.push(13); 8 | theStack.push(23); 9 | theStack.push(33); 10 | theStack.push(43); 11 | theStack.push(53); 12 | theStack.push(63); 13 | theStack.push(73); 14 | theStack.push(83); 15 | System.out.println("popped out value "+ theStack.pop()); 16 | 17 | theStack.printStack(); 18 | theStack.pop(); 19 | theStack.printStack(); 20 | 21 | theStack.peak(); 22 | 23 | // empty out the stack 24 | while (!theStack.isEmpty()){ 25 | System.out.println("popped out value "+ theStack.pop()); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/me/premaseem/datastructure/stacks/simplestack/CustomStack.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.datastructure.stacks.simplestack; 2 | 3 | public class CustomStack { 4 | 5 | int top = -1; 6 | int maxSize = 10; 7 | long[] stackArray; 8 | 9 | public CustomStack(int maxSize){ 10 | this.maxSize = maxSize; 11 | stackArray = new long[maxSize]; 12 | } 13 | 14 | public void push(int value) { 15 | if(isFull()){ 16 | System.out.println("Cannot add more as stack is already full "); 17 | }else { 18 | top++; 19 | stackArray[top] = value; 20 | } 21 | } 22 | 23 | public int pop(){ 24 | if(isEmpty()){ 25 | System.out.println("Cannot pop more since stack is empty"); 26 | return -1; 27 | }else { 28 | int old_top = top; 29 | top--; 30 | return (int) stackArray[old_top]; 31 | } 32 | } 33 | 34 | public int peak(){ 35 | System.out.println("top element of stack is"); 36 | return (int) stackArray[top]; 37 | } 38 | 39 | public boolean isEmpty(){ 40 | return top == -1; 41 | } 42 | 43 | public boolean isFull(){ 44 | return top == (maxSize-1); 45 | } 46 | 47 | public void printStack(){ 48 | System.out.println("current values in stack are "); 49 | for(int i=0; i<=top;i++){ 50 | System.out.println(stackArray[i]); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/me/premaseem/myLib/MyDLLNode.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.myLib; 2 | 3 | public class MyDLLNode { 4 | public int data; 5 | public MyDLLNode next; 6 | public MyDLLNode prev; 7 | 8 | public MyDLLNode(int data) { 9 | this.data = data; 10 | } 11 | 12 | public MyDLLNode(MyDLLNode p, int data, MyDLLNode n) { 13 | this.data = data; 14 | this.prev = p; 15 | this.next = n; 16 | } 17 | 18 | public MyDLLNode(int[] arr){ 19 | MyDLLNode c = this; 20 | c.data = arr[0]; 21 | for(int i =1; i> " + c.data); 34 | c = c.next; 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /src/me/premaseem/myLib/MySLLNode.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.myLib; 2 | 3 | public class MySLLNode { 4 | public int data; 5 | public MySLLNode next; 6 | 7 | public MySLLNode(int data) { 8 | this.data = data; 9 | } 10 | 11 | public MySLLNode(int data, MySLLNode next) { 12 | this.data = data; 13 | this.next = next; 14 | } 15 | 16 | // Constructor to convert array into singly linked list 17 | public MySLLNode(int[] arr){ 18 | data = arr[0]; 19 | MySLLNode curr = this; 20 | for (int i = 1; i < arr.length; i++) { 21 | curr.next = new MySLLNode(arr[i]); 22 | curr = curr.next; 23 | } 24 | 25 | } 26 | 27 | public int toInt(){ 28 | MySLLNode c = this; 29 | String ans= ""; 30 | while(c!= null){ 31 | int d = c.data; 32 | ans += ""+d; 33 | c = c.next; 34 | } 35 | return Integer.parseInt(ans); 36 | } 37 | 38 | public void print(){ 39 | MySLLNode c = this; 40 | System.out.print(" Singly Linked List is "); 41 | while(c!=null){ 42 | System.out.print( " >> " + c.data ); 43 | c = c.next; 44 | } 45 | System.out.println(); 46 | } 47 | 48 | public MySLLNode reverse(){ 49 | MySLLNode c = this; 50 | MySLLNode p = null; 51 | while(c != null){ 52 | MySLLNode n = c.next; 53 | c.next = p; 54 | p = c; 55 | c= n; 56 | } 57 | return p; 58 | } 59 | } -------------------------------------------------------------------------------- /src/me/premaseem/practiceRepeatation/Quicksort.java: -------------------------------------------------------------------------------- 1 | package me.premaseem.practiceRepeatation; 2 | 3 | import me.premaseem.MyUtils; 4 | import org.junit.Test; 5 | 6 | public class Quicksort { 7 | 8 | // Test Driven Development by Aseem Jain 9 | @Test 10 | public void test() { 11 | 12 | int[] intArr = MyUtils.getIntArr(); 13 | sort(intArr); 14 | MyUtils.isArrSorted(intArr); 15 | 16 | } 17 | 18 | void sort(int[] a){ 19 | quickSort(a,0,a.length-1); 20 | } 21 | 22 | // partition, start and end 23 | void quickSort(int[] a, int s, int e){ 24 | if(s