16
Lecture 16: Searching and Sorting Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

Lecture 16: Searching and Sorting Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220

Embed Size (px)

Citation preview

Lecture 16: Searching and Sorting Arrays

Professor: Dr. Miguel Alonso Jr.

Fall 2008

CGS2423/COP1220

Outline

Introduction to Search Algorithms Case Study Introduction to Sorting Algorithms Case Study

Introduction to Search Algorithms

Concept: A search algorithm is a method of locating a specific item in a larger collection of data.

Linear Search Binary Search

Linear Search

Also called sequential search Uses a loop to sequentially step through the

array, starting with the first element Compares each element with the value being

searched for If the value is being searched for is not in the

array, the algorithm will unsuccessfully search until the end of the array

set found to falseset position to -1set index to 0while found is false and index < number of elements

if list[index] is equal to search valuefound = trueposition = index

end ifadd 1 to index

end whilereturn position

Pros and Cons Advantages

Simple Does not require data to be sorted

Disadvantages Inefficient: if the array contains 20,000 elements, the worst

case will be 20,000 loops and comparisons On average, an elements likelihood of being found at

the beginning is equal to the likelihood of being found at the end

For an array of N items, there will be N/2 comparisons, (on average)

Binary Search Much more efficient than the Linear search The array must be sorted in order Instead of testing the first element, the algorithm starts

with the element in the middle If that is the element the search is over if not, the value in the middle is either larger or smaller

than the value being searched for if greater, then the desired value is in the lower half if less, then the desired value is the upper half repeat for the upper/lower half

set first index to 0set last index to the last subscript of the arrayset found to falseset position = -1while found is not true and first is less than or equal to last

set middle to the subscript halfway between array[first] and array[last]if array[middle] equals the desired value

set found = trueset position = middle

else if array[middle] is greater than the desired valueset last = middle -1

elseset first to middle + 1

end ifend whilereturn position

The efficiency of the binary search

With every comparison, half of the array is eliminated!

How many comparisons are needed? Powers of 2 are used to compute Find the smallest power of 2 that is greater than

or equal to the number of elements in the array Example: For an array of 50,000 elements, 16

comparisons will be made because 216 = 65,536 and 216 = 32,768

Searching Case Study:

Introduction to Sorting

Concept: Sorting algorithms are used to arrange data into some order Bubble sort Selection Sort

Bubble Sort Easy way to arrange data in ascending or

descending order Ascending: from lowest to highest Descending: from highest to lowest

How it works: Repeatedly steps through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order

The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

First Pass:( 5 1 4 2 8 ) ( 1 5 4 2 8 ) Here, algorithm compares the first two elements, and swaps them.( 1 5 4 2 8 ) ( 1 4 5 2 8 )( 1 4 5 2 8 ) ( 1 4 2 5 8 )( 1 4 2 5 8 ) ( 1 4 2 5 8 ) Now, since these elements are already in order, algorithm does not swap them.Second Pass:( 1 4 2 5 8 ) ( 1 4 2 5 8 )( 1 4 2 5 8 ) ( 1 2 4 5 8 )( 1 2 4 5 8 ) ( 1 2 4 5 8 )( 1 2 4 5 8 ) ( 1 2 4 5 8 )Now, the array is already sorted, but our algorithm does not know if it is completed. Algorithm needs one whole pass without any swap to know it is sorted.Third Pass:( 1 2 4 5 8 ) ( 1 2 4 5 8 )( 1 2 4 5 8 ) ( 1 2 4 5 8 )( 1 2 4 5 8 ) ( 1 2 4 5 8 )( 1 2 4 5 8 ) ( 1 2 4 5 8 )Finally, the array is sorted, and the algorithm can terminate.

Doset swap flag to falsefor(int count = 0; count < (size -1); count++

if array[count] is greater than array[count+1]swapset swap flag to true

end ifend for

While swap flag is true

Selection Sort

Bubble sort is inefficient for large arrays Selection sort is more efficient and performs

fewer exchanges because it moves items immediately to their final position in the array

The smallest value is located and moved to element zero

The next smallest value is located and moved to element 1

and so on…

572891 172895172895 127895127895 125897125897 125798125798 125789

for (startScan = 0; startScan < (size - 1); startScan++)set minIndex variable to startScanset minValue variable to array[startScan]for(int index = startScan + 1; index < size; index++)

if array[index] is less than minValueset minValue to array[index]set minIndex to index

end ifend for

array[minIndex] to array[startScan]array[startScan] to minValue

Sorting Case Study