1 / 36

Algorithms and data structures

Algorithms and data structures. Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/. Creative Commons. You are free to: share — copy and redistribute the material in any medium or format adapt — remix, transform, and build upon the material Under the following terms:

kalkin
Download Presentation

Algorithms and data structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithms and data structures Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/

  2. Creative Commons • You are free to: • share — copy and redistribute the material in any medium or format • adapt — remix, transform, and build upon the material • Under the following terms: • Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. • NonCommercial — You may not use the material for commercial purposes. • ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. Notices: You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. Text copiedfrom http://creativecommons.org/licenses/by-nc-sa/3.0/ Algorithms and data structures, FER

  3. Sorting algorithms

  4. Algorithms Selected for illustration: Selection sort Bubble sort Insertion sort Shell sort Merge sort Quick sort Heap sort - later!  Sortovi (Sorting) http://www.solidware.com/sort/ Algorithms and data structures, FER

  5. Selection sort Find the smallest element inthe array and swap it withthe first array element Repeat it with the rest of the array, while decreasing the unsorted portion of the array 6 4 1 8 7 5 3 2 1 4 6 8 7 5 3 2 1 2 6 8 7 5 3 4 1 2 3 8 7 5 6 4 1 2 3 4 7 5 6 8 1 2 3 4 5 7 6 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 Algorithms and data structures, FER

  6. Algorithm and complexity Implementation - 2 loops: The outer loop determines the range of the sorted portion of the array The inner loop finds the minimum array element void SelectionSort (int A [], int N) { int i, j, min; for (i = 0; i < N; i++) { min = i; for (j = i+1; j < N; j++) { if (A[j] < A[min]) min = j; } Swap(&A[i], &A[min]); } } O(n-i-1) Algorithms and data structures, FER

  7. Analysis of the execution time Comparison prevails; there are less swaps The execution time does not depend on the initial arrangement, but on the number of array elements O(n2) – roughly n2/2 comparisons and n swaps in an average and in a worst case Execution is not essentially faster if the input elements are closer to their final placements Acceleration: Start sorting simultaneously from both ends The worst case: reversely sorted sequence Algorithms and data structures, FER

  8. Bubble sort Algorithms and data structures, FER The main idea: swapping of neighbouring elements if they are in wrong sequence • Start from the array beginning and progress towards the end • Swap 2 elements if the first is larger than the second one • Acceleration: ifno swap has occurred while traversing through the whole array, the array is sorted

  9. Bubble sort - example 1. traversal 2. traversal 3. traversal 4. traversal 6 4 1 8 7 5 3 2 4 6 1 8 7 5 3 2 4 1 6 8 7 5 3 2 4 1 6 8 7 5 3 2 4 1 6 7 8 5 3 2 4 1 6 7 5 8 3 2 4 1 6 7 5 3 8 2 4 1 6 7 5 3 2 8 4 1 6 7 5 3 2 8 1 4 67 5 3 2 8 1 4 67 5 3 2 8 1 4 6 7 5 3 2 8 1 4 6 5 73 2 8 1 4 6 53 728 1 4 6 53 2 78 1 4 6 53 2 78 1 4 6 53 2 78 1 4 6 5 3 2 78 1 4 5 6 3 2 78 1 4 5 3 6 278 1 4 5 32 6 78 1 4 5 32 6 78 1 4 5 32 6 78 1 4 5 3 2 6 78 1 4 3 5 26 78 1 4 3 256 78 5. traversal 6. traversal 7. traversal 1 4 3 256 78 1 4 3 256 78 1 3 4 2 56 78 1 3 2 456 78 1 3 2 456 78 1 3 2 456 78 1 2 3456 78 1 2 3456 78 123456 78 Algorithms and data structures, FER

  10. Algorithm void BubbleSort (int A [], int N) { int i, j; for (i = 0; i < N-1; i++) { for (j = 0; j < N-1-i; j++) { if (A[j+1] < A[j]) Swap(&A[j], &A[j+1]); } } } O(n-1-i) Algorithms and data structures, FER

  11. Improved bubble sort If in atraversalno swapping has occurred, the array is sorted void BubbleSort (int A [], int N) { int i, j, SwapOccurred; for (i = 0, SwapOccurred = 1; SwapOccurred; i++) { SwapOccurred = 0; for (j = 0; j < N-1-i; j++) { if (A[j+1] < A[j]) { Swap(&A[j], &A[j+1]); SwapOccurred = 1; } } } } Algorithms and data structures, FER

  12. Analysis of the execution time O(n2) – roughly n2/2comparisons andn2/2swaps in an average and a worst case If the input elements are close to their final positions, sorting can be quickly completed The worst case: reversely sorted sequence The best case: already sorted sequence The position of elements is essential for efficiency Large elements at the beginning of the array are not a problem – they quickly move towards the end - hares Small elements at the end of array are a problem - they slowly progress towards the beginning - turtles Algorithms and data structures, FER

  13. Insertion sort The idea: there are two parts of an array: the sorted and the non-sorted one In each step, the first element from the non-sorted partis inserted into the sorted part at the right place The way the cards are (usually) sorted in card games <=x >x x non-sorted <=x x >x non-sorted Algorithms and data structures, FER 21.8.2014.

  14. Insertion sort - example 6 4 1 8 7 5 3 2 6 4 1 8 7 5 3 2 4 6 1 8 7 5 3 2 1 4 6 8 7 5 3 2 1 4 6 8 7 5 3 2 1 4 6 7 8 5 3 2 1 4 5 6 7 8 3 2 1 3 4 5 6 7 8 2 1 2 3 4 5 6 7 8 Algorithms and data structures, FER

  15. Algorithm and complexity Implementation - 2 loops: The outer loop determines the range of the sorted portion of the array The inner loop inserts an element into the sorted part and shifts the rest of elements void InsertionSort (int A [], int N) { int i, j; int aux; for (i = 1; i < N; i++) { aux = A[i]; for (j = i; j >= 1 && A[j-1] > aux; j--) A[j] = A[j-1]; A[j] = aux; } } O(i) Algorithms and data structures, FER

  16. Analysis of the execution time O(n2) – roughly n2/4 comparisons and n2/4 swaps in an average case and double that much in a worst case When the input elements are close to their final positions, it is quickly completed Sorting is stable Elements with the same key value are not swapped if aib bear the same key value and a was positioned before b, after a stable sort a shall maintain its relative position to b Best and worst cases? The worst case: a reversely sorted array The best case: an already sorted array Algorithms and data structures, FER

  17. Shell sort The oldest among quick sorting algoritms, a modified insertion sort author: Donald Shell Idea: For a k-sorted array A it is valid A[i]  A [i + k], i, i+kwhere iandi+kare regular indices If an array is k-sorted and then additionally t-sorted (t<k), it remains also k-sorted Completely sorted array is 1-sorted Generally, an incremental sequence of numbersh1, h2, h3, … ,ht is used in reverse order Choice of the sequence is crucial for the algorithm efficiency Animation: http://www.cis.fiu.edu/~weiss/Shellsort.html Algorithms and data structures, FER

  18. Shell sort – example step=4 step =2 step=1 6 4 1 8 7 5 3 2 6 4 1 8 7 5 3 2 6 4 1 8 7 5 3 2 6 4 1 27 5 3 8 14 62 7 5 3 8 126 4 7 5 3 8 126 4 7 5 3 8 126 4 7 5 3 8 1 2 3 4 6 5 78 1 2 34 6 5 7 8 1 234 6 5 7 8 12 34 6 5 7 8 1 2 34 6 5 7 8 1 2 34 6 5 7 8 1 2 34 5 6 7 8 1 2 34 5 6 78 1 2 34 5 6 7 8 Algorithms and data structures, FER

  19. Algorithm void ShellSort (int A [], int N) { int i, j, step, aux; for (step = N / 2; step > 0; step /= 2) { for (i = step; i < N; i++) { aux = A [i]; for (j = i; j >= step && A[j-step] > aux; j -= step) { A [j] = A [j - step]; } A [j] = aux; } } } Algorithms and data structures, FER

  20. Complexity analysis The average execution time has been an open (unsolved) problem for a long time Worst case O(n2) Hibbard’s sequence: {1, 3, 7, …, 2k -1} results in the worst case with O(n3/2) The average O(n5/4) has been determined by simulation; there is no proof for it yet! Sedgwick’s sequence: {1, 5, 19, 41, 109,…}, namely 9*4i - 9*2i + 1 alternating with 4i - 3*2i +1 Worst case O(n4/3), and the average is O(n7/6) It is not known whether a better sequence can be found A simple algorithm with an extremely complicated complexity analysis Algorithms and data structures, FER

  21. Comparison of sorting algorithms with complexityO(n2) Bubble Selection Insertion Shell Algorithms and data structures, FER

  22. Mergesort Algorithms and data structures, FER Divide-and-conquer strategy is applied, with recursion author: John von Neumann, in year 1945 Idea: • Un-sorted sequence is divided into two approximately equal parts • Each sub-array is sorted recursively, until the sub-array is reduced to a single element • This single element array is sorted! • Two sorted sub-arrays are merged into a sorted array • From two sorted arrays (A and B) a third array (C) is formed By branching, log2 n levels are created, and in each level a process of complexity O(n)is performed • The execution time is O(n log2 n)

  23. Mergesort - example 31 24 47 1 6 78 12 65 31 24 47 1 6 78 12 65 31 24 47 1 6 78 12 65 31 24 47 1 6 78 12 65 24 31 1 47 6 78 12 65 1 24 31 47 6 12 65 78 1 6 12 24 31 47 65 78 Algorithms and data structures, FER

  24. Algorithm void MSort(int A [], int AuxArray[], int left, int right) { int middle; if (left < right) { middle = left + (right - left) / 2; MSort (A, AuxArray, left, middle); MSort (A, AuxArray, middle + 1, right); Merge (A, AuxArray, left, middle + 1, right); } } Algorithms and data structures, FER Exercise: write the function Merge • The function merges the left and the right (approximate) half into a sorted array

  25. Remarks Algorithms and data structures, FER Price of faster sorting: memory • An auxiliary array is created! Rarely used for sorting in core memory • Increased demand for additional memory and copying This is the principal algorithm for sorting using external memory Complexity in the average and the worst case: O(nlog2n) Not faster if the input sequence is already sorted!

  26. Quicksort The fastest algorithm known so far Recursion: “divide-and-conquer“ http://euler.slu.edu/~goldwasser/demos/quicksort/ http://www.cs.queensu.ca/home/cisc121/2004f/lecturenotes/malamb/SortingDemos/QuickSortDemo.html 4 steps – quicksort (S) If the number of members of array S equals 0 or 1, return to the calling program Choose any member vin the array S. Let it be the pivot. Distribute the remaining members of the array S, S \ {v} into two disjointsets: S1 = { x  S \ {v}  x v} (everything less than pivot move to the left) S2 = { x  S \ {v}  x v}(everything bigger than pivot, move to the right) Return the array constructed from {quicksort (S1), v, quicksort (S2)} Algorithms and data structures, FER

  27. Pivot selection Not uniquely determined Not uniquely determinedeither what to do with array members equal to the pivot The question of algorithm implementation A part of good implementation is an efficient solution to this question, see: Weiss: "Data Structures and Algorithm Analysis in C". Possible methods for pivot selection: Estimation of the median, based on 3 elements (the first element, the last element, the element in the middle of the array) At the estimation of the median, these elements are immediately sorted Other possibilities: randomly chosen element, the first element, the last element E.g. the array: 8 1 4 9 6 3 5 2 7 0 pivot = med3 (8, 6, 0) = 6 What would be the worst pivot? What is the actual median? Algorithms and data structures, FER

  28. Quicksort - example Selection of the pivot i i jare bypassing 8 1 4 9 6 3 5 2 7 0 ^^ ^ 0 1 4 9 6 3 5 2 7 8 0 1 4 25 3 7 96 8 j i 0 1 4 25 3 7 96 8 Pivot penultimate Returning the pivot to the position i 0 1 4 9 7 3 5 2 68 i-> <-j 0 1 4 25 3 7 96 8 i 0 1 4 25 3 6 978 0 1 4 9 7 3 5 2 6 8 i j 0 1 4 27 3 5 968 i j 0 1 4 25 3 7968 Selection of pivot Selection of pivot 0 1 4 2 5 3 9 7 8 6 Algorithms and data structures, FER

  29. Algorithm complexity Algorithms and data structures, FER Average execution time is O(n log n) • Sorting is very quick, mostly due to very efficient inner loop The worst case is O(n2) • For a wrong choice of pivot (min or max member), n partitions are obtained and for each of them the execution timeisO(n) • It can be arranged that the probability of such a case exponentially decreases

  30. Comparison of sorts with complexityO(nlogn) Heap Merge Quick Algorithms and data structures, FER

  31. Sorting procedures Sorting of about a million of records is not rare in practice If a single execution of a loop takes 1 s: Classical sort would need about 106 s (i.e. more than 11 days) Quick sort takes about 20 s The solution should not be always attempted in acquisition of faster and more expensive computers Investment in development and application of better algorithms may pay off Algorithms and data structures, FER

  32. Indirect sorting For sorting of large data structures it would not be efficient to swap many records Examples for such structures Student’s ID, family name, name, address, enrolled courses and grades If the data are sorted e.g. by the ID, then a separate array of IDs is formed with adjoined pointers to the rest of the data. Only the formed array is sorted (using any appropriate algorithm) Algorithms and data structures, FER

  33. Comparison Algorithms and data structures, FER

  34. Animations of algorithms http://www.geocities.com/SiliconValley/Network/1854/Sort1.html http://www.solidware.com/sort/ http://www.cs.hope.edu/~dershem/ccaa/animator/Animator.html http://cg.scs.carleton.ca/~morin/misc/sortalg/ http://homepages.dcc.ufmg.br/~dorgival/applets/SortingPoints/SortingPoints.html http://www.cis.fiu.edu/~weiss/Shellsort.html http://www.educypedia.be/education/mathematicsjavasorting.htm Google... Algorithms and data structures, FER

  35. Exercises Write a program to find the kth largest member in an integer array of n members. Sort the input array in descending sequence and print out the member with index k-1. Enter k array members, sort them in descending sequence. Enter the remaining array members. If a member is smaller than the one with index k-1, ignore it, if it is larger insert it into the appropriate place, and throw out the array member that would obtain the index k now. Apply various sorting algorithms and determine the corresponding a priori execution times and measure the a posteriori execution times. Algorithms and data structures, FER

  36. Exercises Write a program to merge two sequential sorted files into a third sorted sequential file. UpariDatoteke (MergeFiles) Algorithms and data structures, FER

More Related