Algorithms with-java-advanced-1.0

4,721 views
4,550 views

Published on

More advanced algorhitms, implemented in Java

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,721
On SlideShare
0
From Embeds
0
Number of Embeds
1,348
Actions
Shares
0
Downloads
189
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • (c) 2005 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.
  • Algorithms with-java-advanced-1.0

    1. 1. Searching and sorting arrays combinatorial samples Algorithms with Java
    2. 2. Contents <ul><li>Algorithms and thinking </li></ul><ul><li>Sorting </li></ul><ul><li>Searching </li></ul><ul><li>Combinatorial </li></ul>
    3. 3. What is “algorithm”? <ul><li>An effective method for solving a problem expressed in a finite sequence of steps </li></ul><ul><li>Used for calculations, data processing, solving daily problems </li></ul><ul><ul><li>via Wikipedia </li></ul></ul>
    4. 4. More about algorithms <ul><li>There are common algorithms solving common problems </li></ul><ul><li>The more you think algorithmically, the easier you solve real world issues </li></ul><ul><li>Complex algorithms constructed on simple ones </li></ul>
    5. 5. Similarities with Maths <ul><li>Systems created from primitives </li></ul><ul><li>Abstraction reduces complexity </li></ul><ul><li>Underlying logic backend </li></ul><ul><li>Common way of thinking, but not necessarily related </li></ul>
    6. 6. Complexity of algorithms <ul><li>Two functions: </li></ul><ul><ul><li>Time complexity </li></ul></ul><ul><ul><li>Space complexity </li></ul></ul><ul><li>Common measuring characteristics </li></ul><ul><li>Noted with a big “O” letter </li></ul>
    7. 7. Examples of complexity <ul><li>O(1) – constant time </li></ul><ul><li>O(n) – linear time </li></ul><ul><li>O(n 2 ) – square time </li></ul><ul><li>O(log n) – logarithmic time </li></ul><ul><li>O(2 n ) – exponential time </li></ul>
    8. 8. How to <ul><li>Measure “O” on iterations </li></ul><ul><li>Direct access means complexity of 1 </li></ul><ul><li>1 to N for loop is O(n) </li></ul><ul><li>Three inner loops from 1 to n is O(n 3 ) </li></ul><ul><li>Measurement helps improving algorithms </li></ul>
    9. 9. Sorting <ul><li>To arrange a common subset in a given order </li></ul><ul><ul><li>alphabetically </li></ul></ul><ul><ul><li>by increasing number, size, weight... </li></ul></ul><ul><li>Useful for searching later </li></ul>
    10. 10. Different methodologies <ul><li>Different sorting algorithms: </li></ul><ul><ul><li>bubble sort </li></ul></ul><ul><ul><li>quick sort </li></ul></ul><ul><ul><li>insertion sort... </li></ul></ul><ul><li>Different min/max/avg complexity </li></ul>
    11. 11. Bubble sort <ul><li>Easiest and shortest </li></ul><ul><li>Average O(n2) performance </li></ul><ul><li>Compares each pair and swaps where needed </li></ul>
    12. 12. Bubble sort: steps <ul><li>Input random array </li></ul><ul><li>Start first round for sorting </li></ul><ul><li>Create inner loop to check every element with its neighbor </li></ul><ul><li>If order is wrong, swap elements </li></ul>
    13. 13. Bubble sort – Example <ul><li>Iterate array twice and check neighbors </li></ul>public static void bubble(int[] array) { for(int i = 0; i < array.length; i++) { for(int j = 0; j < array.length-1; j++) { if(array[j] > array[j+1]) { int tmp = array[j]; array[j] = array[j+1]; array[j+1] = tmp; } } } }
    14. 14. Bubble sort Live Demo
    15. 15. Insertion sort <ul><li>Simple to implement </li></ul><ul><li>Works fast for small amounts </li></ul><ul><li>Effective for partially sorted arrays </li></ul><ul><li>Required memory space is O(1) </li></ul>
    16. 16. Insertion sort: steps <ul><li>Consider some part of the array is sorted </li></ul><ul><li>Loop only non-sorted part </li></ul><ul><li>Get lonely elements and insert them in the sorted row </li></ul>
    17. 17. Insertion sort - Example <ul><li>Insertion sort example </li></ul>for(int i = 1; i < array.length; i++) { // take current element and position int current = array[i]; int dec = i; while(dec > 0 && current < array[dec-1]) { array[dec] = array[dec-1]; dec = dec-1; } array[dec] = current; }
    18. 18. Insertion sort Live Demo
    19. 19. Quick sort <ul><li>Best performance in most cases </li></ul><ul><li>Harder to implement </li></ul><ul><li>Recursive implementation </li></ul>
    20. 20. Quick sort: steps <ul><li>Take the whole array and a median </li></ul><ul><li>Smaller numbers – on the left of median, bigger – on the right (or vice verse) </li></ul><ul><li>Call the same function on left and right parts </li></ul><ul><li>Granular sort of small chunks </li></ul>
    21. 21. Quick sort - Example <ul><li>Quick sort pseudo </li></ul>call quick_sort(low, high); left = 0; right = n; mid = a[(left+right)/2]; while left <= right: while a[left] < mid -> left++; while a[right] > mid -> right--; if left <= right: swap(left, right); left++; right--; quicksort(low, right); quicksort(left, high);
    22. 22. Quick sort Live Demo
    23. 23. Searching <ul><li>Finding an element in a given row </li></ul><ul><li>Different approaches depending on the input </li></ul><ul><li>Samples with sequential check or binary searching algorithm </li></ul>
    24. 24. Binary search <ul><li>Works only on ordered (sorted) arrays </li></ul><ul><li>Highly effective and reduced complexity </li></ul><ul><li>Binary algorithms are used in databases and structures for optimizations </li></ul>
    25. 25. Binary search: steps <ul><li>Get a sorted array and select a number </li></ul><ul><li>Search array from 0 to N </li></ul><ul><li>Chose a median from the row and find whether the number in question is on the left or on the right </li></ul><ul><li>If smaller/bigger number, call the function with 0-med or med-N range </li></ul><ul><li>... (recursively) </li></ul>
    26. 26. Binary search - Example <ul><li>Binary search code </li></ul>public static int binarySearch(int[] array, int low, int high, int number) { if (low < high) { int middle = (low + high) / 2; if (number < array[middle]) { return binarySearch(array, low, middle, number); } else if (number > array[middle]) { return binarySearch(array, middle+1, high, number); } else { return middle; // real result } } return -1; // element not found }
    27. 27. Combinatorial <ul><li>Computing different situations </li></ul><ul><li>Three main divisions: </li></ul><ul><ul><li>permutations </li></ul></ul><ul><ul><li>combinations </li></ul></ul><ul><ul><li>variations </li></ul></ul>
    28. 28. Combinatorial (2) <ul><li>Permutations – all sequences without repetitive digits </li></ul><ul><li>Variations – all sequences consisting of all digits in the range </li></ul><ul><li>Combinations – all sequences with no reversal (1, 2) or (2,1) but not both </li></ul>
    29. 29. Permutation <ul><li>For a given number n create n-dimensional array with ordered permutations </li></ul><ul><li>Create an array with digits 1 to n </li></ul><ul><li>Permutations are n! </li></ul>
    30. 30. Permutations (2) <ul><li>Pseudo code </li></ul>for i=n to 0: if a[i] > a[i-1]: for j=i to n: find min(a[j]) > a[i-1]; swap(a[i-1], a[j]); sort(a[i], n);
    31. 31. Permutations Live Demo
    32. 32. Summary <ul><li>Algorithm is a step list that solves a problem </li></ul><ul><li>Arrays could be placeholders for data </li></ul><ul><li>We could sort arrays in different ways and search with different complexity </li></ul><ul><li>Sequences could be arranged in different combinations </li></ul>
    33. 33. Exercises <ul><li>Write a program that allocates array of 20 integers and initializes each element by its index multiplied by 5. Print the obtained array on the console. </li></ul><ul><li>Write a program that reads two arrays from the console and compares them for equality. </li></ul><ul><li>Write a program that compares two char arrays lexicographically (letter by letter). </li></ul><ul><li>Write a program that finds the maximal increasing sequence of equal elements in an array. Example: {3, 2, 3, 4 , 2, 2, 4}  {2, 3, 4}. </li></ul>
    34. 34. Exercises <ul><li>Write a program that reads two integer numbers N and K and an array of N elements from the console. Find in the array those K elements that have maximal sum. </li></ul><ul><li>Sorting an array means to arrange its elements in increasing order. Write a program to sort an array. Use the &quot;selection sort&quot; algorithm: Find the smallest element, move it at the first position, find the smallest from the rest, move it at second position, etc. </li></ul>
    35. 35. Exercises (6) <ul><li>* Write a program that sorts an int array using the quick sort algorithm. </li></ul><ul><li>Write a program that creates an array containing all letters from the alphabet. Read a word from the console and print the index of each letter in the array. </li></ul><ul><li>* Write a program that finds the index of given element in a sorted int array by using the binary search algorithm. </li></ul><ul><li>* Write a program that sorts an int array using the merge sort algorithm. </li></ul>
    36. 36. Homework <ul><li>Write a program that finds all prime numbers in the range [1...10 000 000]. Use the sieve of Eratosthenes algorithm. </li></ul><ul><li>Write a program that reads two numbers n and k and generates all the variations of k elements from the set [ 1 .. n ]. Example: </li></ul><ul><li>n = 3, k = 2  {1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3} </li></ul><ul><li>Write a program that reads a number n and generates all the permutations of the numbers 1..n. Example: n = 3  {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1} </li></ul>
    37. 37. Homework (2) <ul><li>Write a program that fills and prints a matrix (n, n) as shown below: (examples for n = 4) </li></ul>a) b) c) d) 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 7 11 14 16 4 8 12 15 2 5 9 13 1 3 6 10 1 8 9 16 2 7 10 15 3 6 11 14 4 5 12 13 1 12 11 10 2 13 16 9 3 14 15 8 4 5 6 7
    38. 38. Homework (3) <ul><li>* Write a program that finds the largest area of equal neighbor elements in a rectangular matrix and prints its size. Example: </li></ul><ul><li>Hint: you can use the algorithm &quot; Depth-first search &quot; or &quot; Breadth-first search &quot;. </li></ul>13 1 3 2 2 2 4 3 3 3 2 4 4 4 3 1 2 3 3 4 3 1 3 3 1 4 3 3 3 1 1

    ×