0% found this document useful (0 votes)
13 views

Searching and Sorting Arrays

Uploaded by

sheikhshessi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Searching and Sorting Arrays

Uploaded by

sheikhshessi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

CSC 102 Computer

Science II
SEARCHING AND SORTING ARRAYS
Searching Algorithms
• A search algorithm is a method of finding a specific item
that is stored in an array or other collection of data.
• Two search algorithms
• The linear search
• The binary search
The Linear Search Algorithm
• Also called a sequential search
• Uses a loop to try to find a specific item in an array. It compares the
item with the contents of the 1st array element, then the 2nd array
element, then the 3rd array element, and so on.
• When the item is found the linear search algorithm stops.
• If the item is not in the array, the linear search algorithm will search
to the end of the array.
Pseudocode for the Linear
Search Algorithm
Set found to FALSE
Set index to 0
Set position to -1
While found is FALSE and index is less than the array size
If array[index] equals search value then
set found to TRUE
set position to index
Else
increment index
End If
End While
Return position
Linear Search Algorithm Code
(p. 453)
Linear Search Algorithm Code (Alternative Methods)
Linear Search Example

calling linearSearch function here


Linear Search Algorithm -
Advantages
• Simple
• Easy to implement
• Easy to understand
• Does not require the data in the array to be in any particular order, i.e., data does not have to
be sorted.
• Very suitable for small arrays.
Linear Search Algorithm -
Disadvantages
• Can requires a lot of CPU time for very large arrays
• Maximum number of comparisons = ARRAY_SIZE
• Average number of comparisons = ARRAY_SIZE / 2
• If search item is not in the array, then the number of comparisons = ARRAY_SIZE
The Binary Search Algorithm
• Advantages
• Much more efficient than the linear search algorithm.
• Works well for searching very large arrays.
• Disadvantages
• Requires all array elements to be sorted in order.
The Binary Search Algorithm
search for value = 31

3 9 15 18 22 26 31 38 39 40 48 49 52 61 62 68 71 75 91
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

search for value = 50

3 9 15 18 22 26 31 38 39 40 48 49 52 61 62 68 71 75 91
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

If first > last, then


STOP – item is not in the array.
Return -1 to the calling function
The Binary Search Algorithm
search for value = 52

3 9 15 18 22 26 31 38 39 40 48 49 52 61 62 68 71 75 91
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

1. When the binary search algorithm completes, what will be the values of the following?
a) first
b) middle
c) last

2. How many comparisons will the binary search algorithm make?

answers
1. (a) first = 12, (b) middle = 12, (c) last = 13
2. 4
Student Practice Problem
search for value = 63

5 8 12 17 21 29 30 36 39 50 52 63 68 72 79 81 91 95
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

1. When the binary search algorithm completes, what will be the values of the following?
a) first
b) middle
c) last

2. How many comparisons will the binary search algorithm make?

answers
1. (a) first = 11, (b) middle = 11, (c) last = 12
2. 4
The Binary Search Algorithm
Pseudocode
Set position to -1
Set found to FALSE
Set first to 0
Set last to ARRAY_SIZE – 1
While Not found AND first <= last first > last implies that the item is not in the array
Set middle to (first + last) / 2
If array[middle] equals value
set found to TRUE
set position to middle
Else If array[middle] is less than value
set first to middle + 1
Else
set last to middle – 1
End If
End While
Return position
The Binary Search Algorithm
Code (p. 457)
The Binary Search Algorithm (Alternative method)
The Binary Search Example

calling binarySearch function here


Binary Search vs. Linear Search
• Array with 1,000 elements
• Linear Search  average of 500 comparisons
• Binary Search  no more than 10 comparisons (210 = 1,024)

• Array with 50, 000 elements


• Linear Search  average of 25,000 comparisons
• Binary Search  no more than 16 comparisons (216 = 65,536)

• Array with 100,000,000 elements


• Linear Search  average of 50,000,000 comparisons
• Binary Search  no more than 27 comparisons (227 = 134,217,728)
Sorting Algorithms
• sorting algorithm – a technique for scanning through an array and rearranging its contents in
some specific order.
• ascending order – values are arranged from smallest to largest
10 50 95 150 278 684

Abby Billy Gloria Jack Paul Zack

• descending order – values are arranged from largest to smallest


684 278 150 95 50 10

Zack Paul Jack Gloria Billy Abby


The Bubble Sort
5 4 6 3 2 9 1 8
0 1 2 3 4 5 6 7
The Bubble Sort
6 4 1 8 9 2 0 3 5
0 1 2 3 4 5 6 7 8

1. How many times will the bubble sort algorithm pass through the entire array?
answer
7

2. What will the array look like after the 3rd pass?
answer
1 4 2 0 3 5 6 8 9
Bubble Sort Pseudocode
Do
Set swap flag to false.
For index = 0 TO the next-to-last subscript
If array[index ] is greater than array[index + 1]
Swap the contents of array[index ] and array[index
+ 1].
Set swap flag to true.
End If.
End For.
While any elements have been swapped.
Bubble Sort Code
Bubble Sort Example

calling bubbleSort function here

How can we change the bubbleSort


function to make it sort in descending
order?
Bubble Sort Disadvantages
• Inefficient for large arrays because items only move by one element
at a time.
• The selection sort usually performs fewer swaps because it moves
some items immediately to their final position.
The Selection Sort
8 4 1 3 2

1 4 8 3 2

1 2 8 3 4

1 2 3 8 4

1 2 3 4 8
The Selection Sort
9 4 0 1 8 7 6 2 3

1. How many array-element swaps will the selection sort algorithm make to sort this array in ascending order?
answer
7

2. What will the array look like after the 4th array-element swap?
answer
0 1 2 3 8 7 6 9 4
Selection Sort Pseudocode
For count = 0 TO next-to-last subscript
Set minimum_value to array[count ].
For index = (count + 1) TO the last subscript
If array[index] is less than minimum_value
Set minimum_value to array[index].
Set minimum_index to index.
End If.
End For.
If minimum_value is NOT array[count]
Set array[minimum_index] to array[count ].
Set array[count ] to minimum_value .
End If.
End For.
Selection Sort Code
Selection Sort Example

calling selectionSort function here

You might also like