Linear Search

It begins by comparing x and a1.

When x = a1, the solution is the location of a1.

When xa1, compare x with a2. If x = a2, the solution is the location of a2.

 

Continue this process, comparing x successively with each term of the list until a match is found, where the solution is the location of that term unless no match occurs.

 

This algorithm is also called the sequential search.

 

# Pseudocode

x: target integer
a1, a2, ... , an: distint integers stored in an array
location: index of x found in the array

procedure()
	i := 1
    while (i is less than or equal to n) and (x is not equivalent to ai) do:
    	i := i + 1
        
    /* if x is found */
    if i is less than or equal to n
    	location := i
    /* if x is not found */
    else 
    	location := 0
return location

 

Binary Search

It proceeds by comparing the element to be located to the middle term of the list.

The list is split into two smaller sublists of the same size, or where one of them has one fewer term than the other.

 

The middle term of the list is floor(n / 2), where n is the number of the elements in the list.

 

Binary Search is much more efficient than Linear Search.

Note that, however, Binary Search can be used when the list has terms occurring in order of increasing size!

 

# Example

Search for 19 in the following list

[ 1, 2, 3, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22 ];

  1. Split this list into two smaller lists with eight terms each:
    list_left = [ 1, 2, 3, 5, 6, 7, 8, 10 ]
    list_right = [ 12, 13, 15, 16, 18, 19, 20, 22 ]
  2. Largest term in list_left: 10
    10 < 19, so the search for 19 can be restricted to the list containing the 9th through the 16th terms of the original list.
  3. Split list_right:
    list_left = [ 12, 13, 15, 16 ]
    list_right = [ 18, 19, 20, 22 ]
  4. Largest term in list_left: 16
    16 < 19, so list_right can be considered only.
  5. Split list_right:
    list_left = [ 18, 19 ]
    list_right = [ 20, 22 ]
  6. Because 19 of list_list is not greater than the target value 19, the search is restricted to list_left
  7. Split list_left:
    list_left = [ 18 ]
    list_right = [ 19 ]
  8. 18 < 19, so the search is restricted to list_right.
  9. Now the search has been narrowed down to one term and 19 is located as the 14th term in the original list.

 

# Pseudocode

arr: array
x: target integer
a1, a2, ... , an: increasing intergers stored in an array
m: midpoint

procedure()
    i := 1
    j := arr.size()
    while i is less than j do:
    	m := floor((i + j) / 2)
        if x is greater than am
        	i := m + 1
        else
        	j := m
    end while

/* if x is found */
if x is equal to ai
	location := i
/* if x is not found */
else
	location := 0
return location

The basic idea is as follows:

  1. Select successive elements in ascending order and place them into their proper position.
  2. Find the smallest element from the array, and exchange it with the first element.
    Find the second smallest element, and exchange it with the second element;
    and so on.
  3. Repeat these procedures until the data is sorted in the proper order.

 

# Example

array = { 170, 45, 75, 90, 802, 24, 2, 66 };

  1. Smallest value 2:
    170 ↔ 2 { 2, 45, 75, 90, 802, 24, 170, 66 }
  2. Smallest value 24:
    45 ↔ 24 { 2, 24, 75, 90, 802, 45, 170, 66 }
  3. Smallest value 45:
    75 ↔ 45 { 2, 24, 45, 90, 802, 75, 170, 66 }
  4. Smallest value 66:
    90 ↔ 66 { 2, 24, 45, 66, 802, 75, 170, 90 }
  5. Smallest value 75:
    802 ↔ 75 { 2, 24, 45, 66, 75, 802, 170, 90 }
  6. Smallest value 90:
    802 ↔ 90 { 2, 24, 45, 66, 75, 90, 170, 802 }
  7. Smallest value 170:
    170 ↔ 170 { 2, 24, 45, 66, 75, 90, 170, 802 }
  8. Smallest value 802:
    802 ↔ 802 { 2, 24, 45, 66, 75, 90, 170, 802 }

result = { 2, 24, 45, 66, 75, 90, 170, 802 };

 

 

# Pseudocode

arr: array
min: index of minimum value (target)
tmp: temp value

test()
	for i is 0 to arr.size() - 1 do:
    	min := i        
        for j is i + 1 to arr.size() - 1 do:
        	if arr[j] is less than arr[min]
            	min := j
         end for
         
         tmp := arr[min]
         arr[min] := arr[i]
         arr[i] := tmp
         
    end for    
end

 

# Computation in C

 

 

# Time Complexity

  • Worst complexity: O(n²)
  • Average complexity: O(n²)
  • Best complexity: O(n²)

# Space complexity

  • O(1)

Insertion Sort is almost as simple as Selection Sort, but a little more flexible!

 

The basic idea is to sort a collection of records by inserting them into an already sorted file. The process can be described as follows:

  1. Take elements one by one
  2. Insert the element in its proper position among those already taken and sorted in a new collection
  3. Repeat these processes until all elements are taken and sorted in the proper order

 

# Example

array = { 12, 11, 13, 5, 6 };

  1. Current value 11:
    12 > 11 { 11, 12, 13, 5, 6 }
  2. Current value 13:
    12 < 13 and 11 < 13
  3. Current value 5:
    13 > 5 { 11, 12, 13, 13, 6 },
    12 > 5 { 11, 12, 12, 13, 6 },
    11 > 5 { 11, 11, 12, 13, 6 }, and
    assign the current value to the first one { 5, 11, 12, 13, 6 }
  4. Current value 6:
    13 > 6 { 5, 11, 12, 13, 13 },
    12 > 6 { 5, 11, 12, 12, 13 },
    11 > 6 { 5, 11, 11, 12, 13 },
    5 < 6 { 5, 11, 11, 12, 13 }, and
    assign the current value to the second one { 5, 6, 11, 12, 13 }

result = { 5, 6, 11, 12, 13 };

 

 

# Pseudocode

buf: array
tmp: temp value

for i is 1 to buf.size() - 1 do:
	tmp := buf[i]
    
    for j is i - 1 to 0 when buf[j] > tmp && j > -1 do:
    	buf[j + 1] := buf[j]
    end for
    
    buf[j + 1] := tmp
end

 

# Computation in C

 

# Time Complexity

  • Worst complexity: O(n²)
  • Average complexity: O(n²)
  • Best complexity: O(n)

# Space complexity

  • O(1)

'Algorithms' 카테고리의 다른 글

[Search] Linear vs. Binary 선형/이진 탐색  (0) 2021.08.24
[Sorting] Selection Sort 선택정렬  (0) 2021.08.24
[Sorting] Bubble Sort 버블정렬  (0) 2021.08.24
[Sorting] Quick Sort 퀵 정렬  (0) 2021.08.24
[Sorting] Heap Sort 힙 정렬  (0) 2021.08.24

The basic idea of Bubble Sort is to let greater elements bubble to the right-hand side of the sorted file (or array). The sorting process is as below:

  1. Compare two elements in the neighbour. If they are not in the proper order, exchange them. Therefore, the greatest element bubbles to the most right.
  2. Compare two elements in the neighbour, except the greatest ones that have already been sorted on the right. If they are not in order, swap them, so that a greater element bubbles to the right.
  3. Repeat the above processes until all elements are sorted in the proper order.

# Example

array = { 170, 45, 75, 90, 802, 24, 2, 66 };

  1. 170 ↔ 45 : { 45, 170, 75, 90, 24, 2, 66, 802 }
    170 ↔ 75 : { 45, 75, 170, 90, 24, 2, 66, 802 }
    170 ↔ 90 : { 45, 75, 90, 170, 24, 2, 66, 802 }
    170 ↔ 24 : { 45, 75, 90, 24, 170, 2, 66, 802 }
    170 ↔ 2 : { 45, 75, 90, 24, 2, 170, 66, 802 }
    170 ↔ 66 : { 45, 75, 90, 24, 2, 66, 170, 802 }
    170 and 802 are not swapped since 802 is greater than 170.
  2. 90 ↔ 24 : { 45, 75, 24, 90, 2, 66, 170, 802 }
    90 ↔ 2 : { 45, 75, 24, 2, 90, 66, 170, 802 }
    90 ↔ 66 : { 45, 75, 24, 2, 66, 90, 170, 802 }
  3. 75 ↔ 24 : { 45, 24, 75, 2, 66, 90, 170, 802 }
    75 ↔ 2 : { 45, 24, 2, 75, 66, 90, 170, 802 }
    75 ↔ 66 : { 45, 24, 2, 66, 75, 90, 170, 802 }
  4. 45 ↔ 24 : { 24, 45, 2, 66, 75, 90, 170, 802 }
    45 ↔ 2 : { 24, 2, 45, 66, 75, 90, 170, 802 }
  5. 24 ↔ 2 : { 2, 24, 45, 66, 75, 90, 170, 802 }

result = { 2, 24, 45, 66, 75, 90, 170, 802 };

 

# Pseudocode

arr: array
tmp: temp value

test()
    for i is arr.size() to 1 do:
    	for j is 1 to i - 1 do:
            if arr[j - 1] is greater than arr[j]
                tmp := arr[j - 1]
                arr[j - 1] := arr[j]
                arr[j] := tmp
         end for
     end for
end

 

# Computation in C

 

# Time Complexity

  • Worst complexity: O(n²)
  • Average complexity: O(n²)
  • Best complexity: O(n)

# Space complexity

  • O(1)

'Algorithms' 카테고리의 다른 글

[Sorting] Selection Sort 선택정렬  (0) 2021.08.24
[Sorting] Insertion Sort 삽입정렬  (0) 2021.08.24
[Sorting] Quick Sort 퀵 정렬  (0) 2021.08.24
[Sorting] Heap Sort 힙 정렬  (0) 2021.08.24
[Sorting] Merge Sort 합병 정렬  (0) 2021.08.24

+ Recent posts