algorithms with-java-advanced-1.0

38
Searching and sorting arrays Searching and sorting arrays combinatorial samples combinatorial samples Algorithms with Algorithms with Java Java

Upload: bg-java-ee-course

Post on 06-May-2015

4.197 views

Category:

Technology


0 download

DESCRIPTION

More advanced algorhitms, implemented in Java

TRANSCRIPT

Page 1: Algorithms with-java-advanced-1.0

Searching and sorting arraysSearching and sorting arrays

combinatorial samplescombinatorial samples

Algorithms with Algorithms with JavaJava

Page 2: Algorithms with-java-advanced-1.0

ContentsContents

• Algorithms and thinkingAlgorithms and thinking

• SortingSorting

• SearchingSearching

• Combinatorial Combinatorial

Page 3: Algorithms with-java-advanced-1.0

What is “algorithm”?What is “algorithm”?

• An effective method for solving a An effective method for solving a problem expressed in a finite problem expressed in a finite sequence of stepssequence of steps

• Used for calculations, data Used for calculations, data processing, solving daily problemsprocessing, solving daily problems

– via Wikipediavia Wikipedia

Page 4: Algorithms with-java-advanced-1.0

More about algorithmsMore about algorithms

• There are common algorithms There are common algorithms solving common problemssolving common problems

• The more you think algorithmically, The more you think algorithmically, the easier you solve real world the easier you solve real world issuesissues

• Complex algorithms constructed on Complex algorithms constructed on simple onessimple ones

Page 5: Algorithms with-java-advanced-1.0

Similarities with MathsSimilarities with Maths

• Systems created from primitivesSystems created from primitives

• Abstraction reduces complexityAbstraction reduces complexity

• Underlying logic backendUnderlying logic backend

• Common way of thinking, but not Common way of thinking, but not necessarily relatednecessarily related

Page 6: Algorithms with-java-advanced-1.0

Complexity of algorithmsComplexity of algorithms

• Two functions:Two functions:

– Time complexityTime complexity

– Space complexitySpace complexity

• Common measuring characteristicsCommon measuring characteristics

• Noted with a big “O” letterNoted with a big “O” letter

Page 7: Algorithms with-java-advanced-1.0

Examples of complexityExamples of complexity

• O(1) – constant timeO(1) – constant time

• O(n) – linear timeO(n) – linear time

• O(nO(n22) – square time) – square time

• O(log n) – logarithmic timeO(log n) – logarithmic time

• O(2O(2nn) – exponential time) – exponential time

Page 8: Algorithms with-java-advanced-1.0

How toHow to

• Measure “O” on iterationsMeasure “O” on iterations

• Direct access means complexity of 1Direct access means complexity of 1

• 1 to N for loop is O(n)1 to N for loop is O(n)

• Three inner loops from 1 to n is O(nThree inner loops from 1 to n is O(n33))

• Measurement helps improving Measurement helps improving algorithmsalgorithms

Page 9: Algorithms with-java-advanced-1.0

SortingSorting

• To arrange a common subset in a To arrange a common subset in a given ordergiven order

– alphabeticallyalphabetically

– by increasing number, size, by increasing number, size, weight...weight...

• Useful for searching laterUseful for searching later

Page 10: Algorithms with-java-advanced-1.0

Different methodologiesDifferent methodologies

• Different sorting algorithms:Different sorting algorithms:

– bubble sortbubble sort

– quick sortquick sort

– insertion sort...insertion sort...

• Different min/max/avg complexityDifferent min/max/avg complexity

Page 11: Algorithms with-java-advanced-1.0

Bubble sortBubble sort

• Easiest and shortestEasiest and shortest

• Average O(n2) performanceAverage O(n2) performance

• Compares each pair and swaps Compares each pair and swaps where neededwhere needed

Page 12: Algorithms with-java-advanced-1.0

Bubble sort: stepsBubble sort: steps

• Input random arrayInput random array

• Start first round for sortingStart first round for sorting

• Create inner loop to check every Create inner loop to check every element with its neighborelement with its neighbor

• If order is wrong, swap elementsIf order is wrong, swap elements

Page 13: Algorithms with-java-advanced-1.0

Bubble sort – ExampleBubble sort – Example

Iterate array twice and check neighbors Iterate array twice and check neighbors

13

public static void bubble(int[] array) {public static void bubble(int[] array) {for(int i = 0; i < array.length; i++) for(int i = 0; i < array.length; i++)

{{for(int j = 0; j < array.length-for(int j = 0; j < array.length-

1; j++) {1; j++) {if(array[j] > array[j+1]) {if(array[j] > array[j+1]) {

int tmp = array[j];int tmp = array[j];array[j] = array[j+1];array[j] = array[j+1];array[j+1] = tmp;array[j+1] = tmp;

}}}}

}}}}

Page 14: Algorithms with-java-advanced-1.0

Bubble sortBubble sortLive DemoLive Demo

Page 15: Algorithms with-java-advanced-1.0

Insertion sortInsertion sort

• Simple to implementSimple to implement

• Works fast for small amountsWorks fast for small amounts

• Effective for partially sorted arraysEffective for partially sorted arrays

• Required memory space is O(1)Required memory space is O(1)

Page 16: Algorithms with-java-advanced-1.0

Insertion sort: stepsInsertion sort: steps

• Consider some part of the array is Consider some part of the array is sortedsorted

• Loop only non-sorted partLoop only non-sorted part

• Get lonely elements and insert them Get lonely elements and insert them in the sorted rowin the sorted row

Page 17: Algorithms with-java-advanced-1.0

Insertion sort - ExampleInsertion sort - Example

• Insertion sort exampleInsertion sort example

for(int i = 1; i < array.length; i++) {for(int i = 1; i < array.length; i++) {// take current element and position// take current element and positionint current = array[i];int current = array[i];int dec = i;int dec = i;while(dec > 0 && current < array[dec-1]) {while(dec > 0 && current < array[dec-1]) {

array[dec] = array[dec-1];array[dec] = array[dec-1];dec = dec-1;dec = dec-1;

}}array[dec] = current;array[dec] = current;

}}

Page 18: Algorithms with-java-advanced-1.0

Insertion sortInsertion sortLive DemoLive Demo

Page 19: Algorithms with-java-advanced-1.0

Quick sortQuick sort

• Best performance in most casesBest performance in most cases

• Harder to implementHarder to implement

• Recursive implementationRecursive implementation

Page 20: Algorithms with-java-advanced-1.0

Quick sort: stepsQuick sort: steps

• Take the whole array and a median Take the whole array and a median

• Smaller numbers – on the left of Smaller numbers – on the left of median, bigger – on the right (or vice median, bigger – on the right (or vice verse)verse)

• Call the same function on left and Call the same function on left and right partsright parts

• Granular sort of small chunks Granular sort of small chunks

Page 21: Algorithms with-java-advanced-1.0

Quick sort - ExampleQuick sort - Example

• Quick sort pseudoQuick sort pseudo

call quick_sort(low, high);call quick_sort(low, high);left = 0; right = n;left = 0; right = n;

mid = a[(left+right)/2];mid = a[(left+right)/2];while left <= right:while left <= right:while a[left] < mid -> left++;while a[left] < mid -> left++;while a[right] > mid -> right--;while a[right] > mid -> right--;if left <= right:if left <= right:

swap(left, right);swap(left, right);left++; right--;left++; right--;

quicksort(low, right);quicksort(low, right);quicksort(left, high);quicksort(left, high);

Page 22: Algorithms with-java-advanced-1.0

Quick sortQuick sortLive DemoLive Demo

Page 23: Algorithms with-java-advanced-1.0

SearchingSearching

• Finding an element in a given rowFinding an element in a given row

• Different approaches depending on Different approaches depending on the inputthe input

• Samples with sequential check or Samples with sequential check or binary searching algorithmbinary searching algorithm

Page 24: Algorithms with-java-advanced-1.0

Binary searchBinary search

• Works only on ordered (sorted) Works only on ordered (sorted) arraysarrays

• Highly effective and reduced Highly effective and reduced complexitycomplexity

• Binary algorithms are used in Binary algorithms are used in databases and structures for databases and structures for optimizationsoptimizations

Page 25: Algorithms with-java-advanced-1.0

Binary search: stepsBinary search: steps

• Get a sorted array and select a numberGet a sorted array and select a number

• Search array from 0 to NSearch array from 0 to N

• Chose a median from the row and find Chose a median from the row and find whether the number in question is on whether the number in question is on the left or on the rightthe left or on the right

• If smaller/bigger number, call the If smaller/bigger number, call the function with 0-med or med-N rangefunction with 0-med or med-N range

• ... (recursively)... (recursively)

Page 26: Algorithms with-java-advanced-1.0

Binary search - ExampleBinary search - Example

• Binary search codeBinary search codepublic static int binarySearch(int[] array, public static int binarySearch(int[] array, int low, int high, int number) {int low, int high, int number) {

if (low < high) {if (low < high) { int middle = (low + high) / 2; int middle = (low + high) / 2; if (number < array[middle]) {if (number < array[middle]) { return binarySearch(array, low, return binarySearch(array, low,

middle, number);middle, number); } else if (number > array[middle]) {} else if (number > array[middle]) { return binarySearch(array, return binarySearch(array,

middle+1, high, number);middle+1, high, number); } else {} else { return middle; // real result return middle; // real result }} }} return -1; // element not foundreturn -1; // element not found}}

Page 27: Algorithms with-java-advanced-1.0

CombinatorialCombinatorial

• Computing different situationsComputing different situations

• Three main divisions:Three main divisions:

– permutationspermutations

– combinationscombinations

– variationsvariations

Page 28: Algorithms with-java-advanced-1.0

Combinatorial (2) Combinatorial (2)

• Permutations – all sequences Permutations – all sequences without repetitive digitswithout repetitive digits

• Variations – all sequences consisting Variations – all sequences consisting of all digits in the rangeof all digits in the range

• Combinations – all sequences with Combinations – all sequences with no reversal (1, 2) or (2,1) but not both no reversal (1, 2) or (2,1) but not both

Page 29: Algorithms with-java-advanced-1.0

PermutationPermutation

• For a given number n create n-For a given number n create n-dimensional array with ordered dimensional array with ordered permutationspermutations

• Create an array with digits 1 to nCreate an array with digits 1 to n

• Permutations are n!Permutations are n!

Page 30: Algorithms with-java-advanced-1.0

Permutations (2)Permutations (2)

• Pseudo codePseudo 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);

Page 31: Algorithms with-java-advanced-1.0

PermutationsPermutationsLive DemoLive Demo

Page 32: Algorithms with-java-advanced-1.0

SummarySummary

Algorithm is a step list that solves a Algorithm is a step list that solves a problemproblem

Arrays could be placeholders for dataArrays could be placeholders for data We could sort arrays in different ways and We could sort arrays in different ways and

search with different complexitysearch with different complexity Sequences could be arranged in different Sequences could be arranged in different

combinationscombinations

32

Page 33: Algorithms with-java-advanced-1.0

ExercisesExercises

1.1. Write a program that allocates array of 20 Write a program that allocates array of 20 integers and initializes each element by its integers and initializes each element by its index multiplied by 5. Print the obtained array index multiplied by 5. Print the obtained array on the console.on the console.

2.2. Write a program that reads two arrays from Write a program that reads two arrays from the console and compares them for equality.the console and compares them for equality.

3.3. Write a program that compares two Write a program that compares two charchar arrays lexicographically (letter by letter).arrays lexicographically (letter by letter).

4.4. Write a program that finds the maximal Write a program that finds the maximal increasing sequence of equal elements in an increasing sequence of equal elements in an array. Example: {3, array. Example: {3, 2, 3, 42, 3, 4, 2, 2, 4} , 2, 2, 4} {2, 3, 4}. {2, 3, 4}.33

Page 34: Algorithms with-java-advanced-1.0

ExercisesExercises

5.5. Write a program that reads two integer Write a program that reads two integer numbers N and K and an array of N elements numbers N and K and an array of N elements from the console. Find in the array those K from the console. Find in the array those K elements that have maximal sum.elements that have maximal sum.

6.6. Sorting an array means to arrange its Sorting an array means to arrange its elements in increasing order. Write a program elements in increasing order. Write a program to sort an array. Use the "selection sort" to sort an array. Use the "selection sort" algorithm: Find the smallest element, move it algorithm: Find the smallest element, move it at the first position, find the smallest from the at the first position, find the smallest from the rest, move it at second position, etc.rest, move it at second position, etc.

34

Page 35: Algorithms with-java-advanced-1.0

Exercises (6)Exercises (6)

7.7. * Write a program that sorts an * Write a program that sorts an intint array array using the using the quick sortquick sort algorithm. algorithm.

8.8. Write a program that creates an array Write a program that creates an array containing all letters from the alphabet. Read containing all letters from the alphabet. Read a word from the console and print the index a word from the console and print the index of each letter in the array.of each letter in the array.

9.9. * Write a program that finds the index of * Write a program that finds the index of given element in a sorted given element in a sorted intint array by using array by using the the binary searchbinary search algorithm. algorithm.

10.10. * Write a program that sorts an * Write a program that sorts an intint array array using the using the merge sortmerge sort algorithm. algorithm.

35

Page 36: Algorithms with-java-advanced-1.0

HomeworkHomework1.1. Write a program that finds all prime numbers Write a program that finds all prime numbers

in the range [1...10 000 000]. Use the in the range [1...10 000 000]. Use the sieve of sieve of EratosthenesEratosthenes algorithm. algorithm.

2.2. Write a program that reads two numbers Write a program that reads two numbers nn and and kk and generates all the variations of and generates all the variations of kk elements from the set [elements from the set [11....nn]. Example:]. Example:

n = 3, k = 2 n = 3, k = 2 {1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, {1, 1}, {1, 2}, {1, 3}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}3}, {3, 1}, {3, 2}, {3, 3}

3.3. Write a program that reads a number Write a program that reads a number nn and and generates all the permutations of the numbers generates all the permutations of the numbers 1..n. Example: n = 3 1..n. Example: n = 3 {1, 2, 3}, {1, 3, 2}, {2, 1, {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}

36

Page 37: Algorithms with-java-advanced-1.0

Homework (2)Homework (2)

3.3. Write a program that fills and prints a matrix Write a program that fills and prints a matrix (n, n) as shown below: (examples for n = 4)(n, n) as shown below: (examples for n = 4)

37

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

a)a) b)b)

c)c) d)d)

Page 38: Algorithms with-java-advanced-1.0

Homework (3)Homework (3)

1.1. * Write a program that finds the largest area * Write a program that finds the largest area of equal neighbor elements in a rectangular of equal neighbor elements in a rectangular matrix and prints its size. Example:matrix and prints its size. Example:

Hint: you can use the algorithm "Hint: you can use the algorithm "Depth-first Depth-first searchsearch" or "" or "Breadth-first searchBreadth-first search".".

38

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

1313