Algorithms with-java-advanced-1.0
Upcoming SlideShare
Loading in...5
×
 

Algorithms with-java-advanced-1.0

on

  • 4,232 views

More advanced algorhitms, implemented in Java

More advanced algorhitms, implemented in Java

Statistics

Views

Total Views
4,232
Slideshare-icon Views on SlideShare
2,925
Embed Views
1,307

Actions

Likes
1
Downloads
150
Comments
0

3 Embeds 1,307

http://javaeecourse.devbg.org 1305
http://static.slidesharecdn.com 1
http://webcache.googleusercontent.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • (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 Algorithms with-java-advanced-1.0 Presentation Transcript

  • Searching and sorting arrays combinatorial samples Algorithms with Java
  • Contents
    • Algorithms and thinking
    • Sorting
    • Searching
    • Combinatorial
  • What is “algorithm”?
    • An effective method for solving a problem expressed in a finite sequence of steps
    • Used for calculations, data processing, solving daily problems
      • via Wikipedia
  • More about algorithms
    • There are common algorithms solving common problems
    • The more you think algorithmically, the easier you solve real world issues
    • Complex algorithms constructed on simple ones
  • Similarities with Maths
    • Systems created from primitives
    • Abstraction reduces complexity
    • Underlying logic backend
    • Common way of thinking, but not necessarily related
  • Complexity of algorithms
    • Two functions:
      • Time complexity
      • Space complexity
    • Common measuring characteristics
    • Noted with a big “O” letter
  • Examples of complexity
    • O(1) – constant time
    • O(n) – linear time
    • O(n 2 ) – square time
    • O(log n) – logarithmic time
    • O(2 n ) – exponential time
  • How to
    • Measure “O” on iterations
    • Direct access means complexity of 1
    • 1 to N for loop is O(n)
    • Three inner loops from 1 to n is O(n 3 )
    • Measurement helps improving algorithms
  • Sorting
    • To arrange a common subset in a given order
      • alphabetically
      • by increasing number, size, weight...
    • Useful for searching later
  • Different methodologies
    • Different sorting algorithms:
      • bubble sort
      • quick sort
      • insertion sort...
    • Different min/max/avg complexity
  • Bubble sort
    • Easiest and shortest
    • Average O(n2) performance
    • Compares each pair and swaps where needed
  • Bubble sort: steps
    • Input random array
    • Start first round for sorting
    • Create inner loop to check every element with its neighbor
    • If order is wrong, swap elements
  • Bubble sort – Example
    • Iterate array twice and check neighbors
    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; } } } }
  • Bubble sort Live Demo
  • Insertion sort
    • Simple to implement
    • Works fast for small amounts
    • Effective for partially sorted arrays
    • Required memory space is O(1)
  • Insertion sort: steps
    • Consider some part of the array is sorted
    • Loop only non-sorted part
    • Get lonely elements and insert them in the sorted row
  • Insertion sort - Example
    • Insertion sort example
    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; }
  • Insertion sort Live Demo
  • Quick sort
    • Best performance in most cases
    • Harder to implement
    • Recursive implementation
  • Quick sort: steps
    • Take the whole array and a median
    • Smaller numbers – on the left of median, bigger – on the right (or vice verse)
    • Call the same function on left and right parts
    • Granular sort of small chunks
  • Quick sort - Example
    • Quick sort pseudo
    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);
  • Quick sort Live Demo
  • Searching
    • Finding an element in a given row
    • Different approaches depending on the input
    • Samples with sequential check or binary searching algorithm
  • Binary search
    • Works only on ordered (sorted) arrays
    • Highly effective and reduced complexity
    • Binary algorithms are used in databases and structures for optimizations
  • Binary search: steps
    • Get a sorted array and select a number
    • Search array from 0 to N
    • Chose a median from the row and find whether the number in question is on the left or on the right
    • If smaller/bigger number, call the function with 0-med or med-N range
    • ... (recursively)
  • Binary search - Example
    • Binary search code
    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 }
  • Combinatorial
    • Computing different situations
    • Three main divisions:
      • permutations
      • combinations
      • variations
  • Combinatorial (2)
    • Permutations – all sequences without repetitive digits
    • Variations – all sequences consisting of all digits in the range
    • Combinations – all sequences with no reversal (1, 2) or (2,1) but not both
  • Permutation
    • For a given number n create n-dimensional array with ordered permutations
    • Create an array with digits 1 to n
    • Permutations are n!
  • Permutations (2)
    • Pseudo code
    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);
  • Permutations Live Demo
  • Summary
    • Algorithm is a step list that solves a problem
    • Arrays could be placeholders for data
    • We could sort arrays in different ways and search with different complexity
    • Sequences could be arranged in different combinations
  • Exercises
    • 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.
    • Write a program that reads two arrays from the console and compares them for equality.
    • Write a program that compares two char arrays lexicographically (letter by letter).
    • 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}.
  • Exercises
    • 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.
    • 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.
  • Exercises (6)
    • * Write a program that sorts an int array using the quick sort algorithm.
    • 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.
    • * Write a program that finds the index of given element in a sorted int array by using the binary search algorithm.
    • * Write a program that sorts an int array using the merge sort algorithm.
  • Homework
    • Write a program that finds all prime numbers in the range [1...10 000 000]. Use the sieve of Eratosthenes algorithm.
    • Write a program that reads two numbers n and k and generates all the variations of k elements from the set [ 1 .. n ]. Example:
    • n = 3, k = 2  {1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}
    • 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}
  • Homework (2)
    • Write a program that fills and prints a matrix (n, n) as shown below: (examples for n = 4)
    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
  • Homework (3)
    • * Write a program that finds the largest area of equal neighbor elements in a rectangular matrix and prints its size. Example:
    • Hint: you can use the algorithm &quot; Depth-first search &quot; or &quot; Breadth-first search &quot;.
    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