Скачать презентацию Algorithms Searching and Sorting 1 What is Скачать презентацию Algorithms Searching and Sorting 1 What is

Algorithms.ppt

  • Количество слайдов: 21

Algorithms Searching and Sorting 1 Algorithms Searching and Sorting 1

What is an algorithm? An algorithm is “a finite set of precise instructions for What is an algorithm? An algorithm is “a finite set of precise instructions for performing a computation or for solving a problem” n A program is one type of algorithm All programs are algorithms Not all algorithms are programs! n n n Directions to somebody’s house is an algorithm A recipe for cooking a cake is an algorithm The steps to compute the cosine of 90° is an algorithm 2

Some algorithms are harder than others Some algorithms are easy n n Finding the Some algorithms are harder than others Some algorithms are easy n n Finding the largest (or smallest) value in a list Finding a specific value in a list Some algorithms are a bit harder n Sorting a list Some algorithms are very hard n Finding the shortest path between Astana and London Some algorithms are essentially impossible n Factoring large composite numbers 3

Properties of algorithms Algorithms generally share a set of properties: n n n n Properties of algorithms Algorithms generally share a set of properties: n n n n Input: what the algorithm takes in as input Output: what the algorithm produces as output Definiteness: the steps are defined precisely Correctness: should produce the correct output Finiteness: the steps required should be finite Effectiveness: each step must be able to be performed in a finite amount of time Generality: the algorithm should be applicable to all problems of a similar form 4

Searching algorithms Given a list, find a specific element in the list We will Searching algorithms Given a list, find a specific element in the list We will see two types n Linear search a. k. a. sequential search n Binary search 5

Algorithm 1: Linear search Given a list, find a specific element in the list Algorithm 1: Linear search Given a list, find a specific element in the list n List does NOT have to be sorted! procedure linear_search (x: integer; a 1, a 2, …, an: integers) i : = 1 while ( i ≤ n and x ≠ ai ) i : = i + 1 if i ≤ n then location : = i else location : = 0 {location is the subscript of the term that equals x, or it is 0 if x is not found} 6

Algorithm 1: Linear search, take 1 procedure linear_search (x: integer; a 1, a 2, Algorithm 1: Linear search, take 1 procedure linear_search (x: integer; a 1, a 2, …, an: integers) i : = 1 while ( i ≤ n and x ≠ ai ) x 3 i : = i + 1 if i ≤ n then location : = i location 8 else location : = 0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 4 1 7 0 5 2 9 3 6 8 i 1 8 7 6 5 4 3 2 7

Algorithm 1: Linear search, take 2 procedure linear_search (x: integer; a 1, a 2, Algorithm 1: Linear search, take 2 procedure linear_search (x: integer; a 1, a 2, …, an: integers) i : = 1 while ( i ≤ n and x ≠ ai ) x 11 i : = i + 1 if i ≤ n then location : = i location 0 else location : = 0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 4 1 7 0 5 2 9 3 6 8 i 11 1 9 8 7 6 5 4 3 20 8

Linear search running time How long does this take? If the list has n Linear search running time How long does this take? If the list has n elements, worst case scenario is that it takes n “steps” n Here, a step is considered a single step through the list 9

Linear Search Quiz Linear Search Quiz "Cat", "Mouse", "Frog", "Lion", "Panda", "Llama", "Bee" For the array above, how many searches would it take to find the following data: a) "Panda“ b) "Camel" Linear Game - Battleship 10

Algorithm 2: Binary search Given a list, find a specific element in the list Algorithm 2: Binary search Given a list, find a specific element in the list n List MUST be sorted! Each time it iterates through, it cuts the list in half procedure binary_search (x: integer; a 1, a 2, …, an: increasing integers) i : = 1 { i is left endpoint of search interval } j : = n { j is right endpoint of search interval } while i < j begin m : = (i+j)/2 { m is the point in the middle } if x > am then i : = m+1 else j : = m end if x = ai then location : = i else location : = 0 {location is the subscript of the term that equals x, or it is 0 if x is not found} 11

Algorithm 2: Binary search, take 1 procedure binary_search (x: integer; a 1, a 2, Algorithm 2: Binary search, take 1 procedure binary_search (x: integer; a 1, a 2, …, an: increasing integers) i : = 1 j : = n while i < j begin m : = (i+j)/2 if x > am then i : = m+1 else j : = m end if x = ai then location : = i else location : = 0 x location 14 7 a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 2 4 6 8 10 12 14 16 18 20 i 7 6 1 m 6 7 8 5 j 7 8 10 12

Algorithm 2: Binary search, take 2 procedure binary_search (x: integer; a 1, a 2, Algorithm 2: Binary search, take 2 procedure binary_search (x: integer; a 1, a 2, …, an: increasing integers) i : = 1 j : = n while i < j begin m : = (i+j)/2 if x > am then i : = m+1 else j : = m end if x = ai then location : = i I else location : = 0 x location 15 0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 2 4 6 8 10 12 14 16 18 20 i 8 6 1 m 7 8 5 j 8 10 13

Binary search running time How long does this take (worst case)? If the list Binary search running time How long does this take (worst case)? If the list has 8 elements n It takes 3 steps If the list has 16 elements n It takes 4 steps If the list has 64 elements n It takes 6 steps If the list has n elements n It takes log 2 n steps 14

Binary Search Binary Game - Battleship 15 Binary Search Binary Game - Battleship 15

Sorting algorithms Given a list, put it into some order n Numerical, lexicographic, etc. Sorting algorithms Given a list, put it into some order n Numerical, lexicographic, etc. We will see two types n n Bubble sort Insertion sort 16

Algorithm 3: Bubble sort One of the most simple sorting algorithms n Also one Algorithm 3: Bubble sort One of the most simple sorting algorithms n Also one of the least efficient It takes successive elements and “bubbles” them up the list procedure bubble_sort (a 1, a 2, …, an) for i : = 1 to n-1 for j : = 1 to n-i if aj > aj+1 then interchange aj and aj+1 { a 1, …, an are in increasing order } 17

Algorithm 3: Bubble sort An example using physical objects… 18 Algorithm 3: Bubble sort An example using physical objects… 18

Bubble Sort Quiz Bubble sort Trace Table. docx 19 Bubble Sort Quiz Bubble sort Trace Table. docx 19

Algorithm 4: Insertion sort Another simple (and inefficient) algorithm It starts with a list Algorithm 4: Insertion sort Another simple (and inefficient) algorithm It starts with a list with one element, and inserts new elements into their proper place in the sorted part of the list procedure insertion_sort (a 1, a 2, …, an) for j : = 2 to n take successive elements in the list begin i : = 1 find where that element should be while aj > ai in the sorted portion of the list i : = i +1 m : = aj move all elements in the sorted for k : = 0 to j-i-1 portion of the list that are greater aj-k : = aj-k-1 than the current element up by one ai : = m end { a 1, a 2, …, an are sorted } put the current element into it’s proper place in the sorted portion of the list 21

Algorithm 4: Insertion sort Yet an other example using physical objects… 22 Algorithm 4: Insertion sort Yet an other example using physical objects… 22