Create Presentation
Download Presentation

Download Presentation
## Algorithms -- What we’ll do

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Algorithms -- What we’ll do**• Accomplishing tasks with algorithms • Working with existing algorithms: • Selection Sort • Insertion Sort • MergeSort • Binary Search • Euclid’s algorithm**Knowing all of the rules of English, grammar and spelling,**will not help you give directions from place a to place b if you do not know how to get there. • In systems, an analyst can describe a method in more abstract terms to a programmer without knowing the exact syntax of the programming language**An algorithm is an abstract and formalstep by step recipe**that tells how to perform a certain task or solve a certain problem on a computer • An algorithm can describe a method for accomplishing a task without relying on any particular programming or any particular computer model**Flowcharts assist in the decomposition of a problem and are**frequently used although not in the most formal form as illustrated in handout (flowchart) • Pseudocode is a solution in a loosely formatted style of the actual software, JAVA, code but without the syntax. This is the shorthand that developers use to flesh out a solution.**Bubble Sort**• Sort an array in ascending or descending order by evaluating the nth element against the nth+1 element. If they are not in the prescribed order, swap them. When we reach the end of the array, all of the items will be sorted. • This sort traverses the array swapping all the pairs of adjacent elements that are out of order. • The total number of comparisons is (n-1)n/2 • This is an O(n^2) sort**BUBBLE SORT**8 5 10 1 6**PASS 1**8 5 10 1 6**PASS 1**5 8 10 1 6**PASS 1**5 8 10 1 6**PASS 1**5 8 10 1 6**PASS 1**5 8 1 10 6**PASS 1**5 8 1 10 6**PASS 1**5 8 1 6 10**5**8 1 6 10**PASS 2**5 8 1 6 10**PASS 2**5 8 1 6 10**PASS 2**5 1 8 6 10**PASS 2**5 1 8 6 10**PASS 2**5 1 6 8 10**5**1 6 8 10**PASS 3**5 1 6 8 10**PASS 3**1 5 6 8 10**PASS 3**1 5 6 8 10**1**5 6 8 10**PASS 4**1 5 6 8 10**1**5 6 8 10**1**5 6 8 10**Selection Sort**• For example, an algorithm for arranging the elements of an array in ascending order • Find the largest element on the array and swap it with the last element, then continue the process for the first n-1 elements**1st iteration takes the LARGEST ARRAY element and swaps it**with the LAST array element • The largest element is now in its correct place and will not be moved again**We logically reduce the size of the array and ignore the**“last” element(s)**Steps in selection sort:**• Initialize a variable, n , to the size of the array • Find the largest among the first n elements • Make it swap places with the nth element • Decrement n by 1 • Repeat steps 2 to 4 while n >= 2**SELECTION SORT OUTPUT:**initial array: 2 97 17 37 12 46 10 55 80 42 39 selection sort in progress... 2 39 17 37 12 46 10 55 80 42 97 2 39 17 37 12 46 10 55 42 80 97 2 39 17 37 12 46 10 42 55 80 97 2 39 17 37 12 42 10 46 55 80 97**SELECTION SORT OUTPUT:**initial array: 2 97 17 37 12 46 10 55 80 42 39 selection sort in progress... 2 10 12 17 37 39 42 46 55 80 97**The same procedure can be used to sort the array in**descending order by finding the SMALLEST element in the array**Slowest and most predictable sort**• There are at most n swap operations performed. • This is still an O(n^2) sort because counting the number of comparisons yields a progression similar to the bubble sort. • Quadratic Time • The number of operations is proportional to the size of the task SQUARED 20 tasks = 400**TPS: How would you code for the following:**• How would you Insert an element in an Unsorted Array • How would you Insert an element in a Sorted Array**Insertion Sort O(n^2)**• Places one element at a time into position. • The idea with this sort is to keep the beginning part of the array sorted and insert each next element into the correct place in it • We must always start with an empty or an ordered list to traverse • We are now placing the elements as we find them**The approach is analogous to the fashion in which many**people pick up playing cards and order them in their hands, one by one • You pick up the cards in order and compare the n th card with the cards already held until the appropriate place is found searching from the end of your current hand and working backwards**for (count = 1; count < numberofelements –1; count++) {**assign value of A[count] to a variable temp check each of the array elements to the left of A[count] if the array element is GREATER than temp, shift it right 1 position when an array element is found that is NOT GREATER than temp, temp goes into the open position }**Example:**ordered list: 1 3 5 7 (hand of cards) list to order: 2 8 9 4 6 0 (n th card to pick up) • Take 2 and try to swap it with the next lower position until it finds the proper position or until the array is fully traversed**Example:**• In evaluating the list to order’s 1st element, 2 1 3 5 2 7 1 3 2 5 7 1 2 3 5 7**This sort works better with Linked Lists**• Best case is an O(n) sort --- occurs when list is already sorted • However, in an average case, the math proves this is still an O(n^2) sort.**Example:**For (count = 1; count < numberofelements –1; count++) { assign value of A[count] to a variable temp check each of the array elements to the left of A[count] if the array element is GREATER than temp, shift it right 1 position when an array element is found that is NOT GREATER than temp, temp goes into the open position }**Code Example in file**Algorithm.java**Mergesort O(n log n)**• Mergesort O(n log n) time • A “Divide and Conquer” sort • If we had 2 sorted arrays we could combine (merge) them into 1 sorted array --- this serves as the basis for the mergesort**Recursively, the algorithm works as follows:**• Split the array into 2 equal halves • Sort the first half • Sort the second half • Merge the 2 halfs • The base case occurs when the array has only 1 element**This is an O(n log n) sort (performance is similar for**best and worst cases) 20 tasks = 86.4 • Requires a temporary workspace of n elements • The merge uses a temp (array) to store the halves of the array