Docsity

Programming - Unit 7: Search, sort and merge algorithms (slides), Diapositivas de Programación Informática

Vista previa parcial del texto

¡Descarga Programming - Unit 7: Search, sort and merge algorithms (slides) y más Diapositivas en PDF de Programación Informática solo en Docsity!

U N I T 7.

S E A R C H , S O R T

A N D M E R G E

A L G O R I T H M S

P R O G R A M M I N G Y E A R 2 0 2 0 - 2 0 2 1

CONTENTS:

  • 7.1. Search algorithms
    • Unsorted lists: Linear search
    • Sorted lists: Optimized linear search, Binary search
  • 7.2. Sorting algorithms
    • Basic sorting algorithms: Bubble, insertion, selection
    • Optimized sorting algorithms (only for reference)
    • Comparing performance of sorting algorithms
  • 7.3. Merge

7. 1. S E A R C H

SEARCH ALGORITHMS

  • Search = find the position of a given value in a list
    • First occurrence
  • Linear (or sequential) search
    • Examine each element starting from the first until we find the value sought
or the end of the list is reached

LINEAR SEARCH ALGORITHM 2/

% results – post check if found fprintf ('Target value found in position %i', i-1); else fprintf ('Target value not found'); end

SEARCH IN SORTED LISTS

  • Can be optimized
    • More efficient (as in a dictionary)
    • … although requires previous sort
  • Two algorithms
    • Optimized linear search
      • Search ends when the element is found or the search goes beyond the position in the list where the value should be found
    • Binary search
      • check the central element of the list.
        • If it's the element sought the search ends
        • If not, repeat using only the part of the list where the element should be
          • Until the sub-list is empty
OPTIMIZED LINEAL SEARCH (SORTED LIST) 1/

size=6; % Size of the vector list= randi(50,1,size) % create a vector of random integers list=sort(list)

i = 1 ; found = false; % flag to control if the value is found 0 (false) 1 (true) finished = false; % flag to control if search can be finished

value = input('enter value to search: '); % value to search

OPTIMIZED LINEAL SEARCH (SORTED LIST) ) 2/

while(~found) && (~finished) if (list(i)==value ) found = 1 ; else if ((list(i)>value )||(i==size) ) % i is beyond the position where value should be or i=end of the list finished= true ;% end search else i=i+1; end end end

B I N A RY S E A R C H

BINARY SEARCH - CONCEPT

28

56

31

right

left

left

left

right

right

middle

middle

middle

Target value =

BINARY SEARCH

5

1st iteration^5111422^28^3137565970

Target value= (not in the list)

Lefti=1 (^) (left+ right)/2=5^ middle= right= 10

v(middle) = 28  larger than target value, search continues in lower half of the list

left=1 middle = right = (izq+ right)/2 = 2

left=1 right =

middle = (left+ right)/2 = 0

v(middle) = 11  larger than target value, search continues in lower half of the list  move right to middle -

left > right  empty list, therefore the value is not in the list

5 11 14 22

v(middle) = 5  smaller than target value, search continues in upper half of the list  move left to middle + 1

left=2 right= 1  empty list 4th iteration:

2nd iteration

3rd iteration

middle=

BINARY SEARCH 1/

% Binary search (sorted lists)

size=6; % Size of the vector

list= randi(50,1,size) % create a vector of random integers

list=sort(list);

% initialization

found = false; % flag to control if the value is found false/true

position=-1; %position of the value sought, initialized to -

left = int16(1); % lower limit (left) of subsist

right = int16(size); % upper limit (right) of sub list

middle=idivide(left+right,2); % idivide = integer division

value = input('enter value to search: '); % value to search

BINARY SEARCH 3/

% results – post check
if found
fprintf ('Value found in position %i', position);
else
fprintf ('Value not found');
end

7. 2. S O R T