CMSC 2021 Searching and Sorting. CMSC 2022 Review of Searching Linear (sequential) search Linear...

Post on 14-Dec-2015

222 views 1 download

transcript

CMSC 202 1

Searching and Sorting

CMSC 202 2

Review of Searching

• Linear (sequential) search

• Linear search on an ordered list

• Binary search

CMSC 202 3

Linear Searchfig - 14 itemscantalope - minimum comparisons = 1pineapple - maximum comparisons = 13cherry - on average, 13/2 = 6 to 7 comparisonskiwilemonapple Not a very efficient search! So, do wewatermelon never use it?limemelonorangepearbananatangerine

CMSC 202 4

Linear Search on An Ordered List

apple -14 itemsbanana - minimum comparisons = 1 cantalope - maximum comparisons = 13cherry - on average, 13/2 = 6 to 7 comparisonsfig kiwi So why is a linear search more efficientlemon on an ordered list than an unordered list?limemelon So should we always choose to order a listorange before performing a linear search on it?pearpineappletangerinewatermelon

CMSC 202 5

Binary Search

applebananacantalopecherryfigkiwilemonlimemelonorangepearpineappletangerinewatermelon

CMSC 202 6

Binary Search (con’t)

• Pretty efficient!

• As the number of items doubles, the number of comparisons increases by only one!

• So, if it is so efficient, do we always choose to do a binary search?

CMSC 202 7

Sorting

• Bubble Sort

• Selection Sort

• Merge Sort

• Quicksort

CMSC 202 8

CPGATOB

Bubble Sort

CMSC 202 9

• How many loops will the bubble sort algorithm have?

• What will the loop(s) do?

Bubble Sort (con’t)

CMSC 202 10

void bubbleSort1(data array[], int numElements){ int pass, i; data temp;

for (pass = 1; pass <= (numElements-1); pass++) { for (i = 0; i <= (numElements-2); i++) { if (array[i] > array[i+1]) { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; } } }}

CMSC 202 11

void bubbleSort2(data array[], int numElements) {

int pass, i, swapMade; // swap flag added data temp;

pass = 1; swapMade = 1; // initialize to true while ((pass<=(numElements-1)) && (swapMade==1)) { swapMade = 0; // reset to false for (i = 0; i <= (numElements-2); i++) { if (array[i] > array[i+1]) { temp = array[i]; array[i] = array[i+1]; array[i+1] = temp; swapMade = 1; // set to true } } pass++; }}

CMSC 202 12

C

P

G

A

T

O

B

Selection Sort

CMSC 202 13

• How many loops will the selection sort algorithm have?

• What will the loop(s) do?

Selection Sort (con’t)

CMSC 202 14

// From on-line notesvoid selection_sort(data A[], index low, index high){ index i, j, min ; data temp ;

for (i = low ; i < high ; i++) { min = i ; for (j = i+1 ; j <= high ; j++) { if (A[j] < A[min]) min = j ; } temp = A[min] ; A[min] = A[i] ; A[i] = temp ; }}

CMSC 202 15

Merge Sort

40 20 10 80 60 50 7 30 100 90 70

CMSC 202 16

• Merge Sort is a “divide and conquer” algorithm

• The array is repeatedly divided in half, then the arrays are repeatedly merged

• What type of algorithm do you think would be appropriate?

Merge Sort (con’t)

CMSC 202 17

// From on-line notes

void mergesort(data A[], index low, index high){ index mid ; if (low >= high) return ; mid = (low + high) / 2 ; mergesort(A, low, mid) ; mergesort(A, mid + 1, high) ; merge(A, low, mid, mid + 1, high) ;}

CMSC 202 18

/* Global variable temp must be allocated the same amount of space as the array to be sorted */

data *temp ;

void merge(data A[], index low1, index high1, index low2, index high2){ index t, i1, i2 ;

/* Sanity check */ assert(low2 == high1 + 1) ;

CMSC 202 19

/* while there are elements in both halves, copy the lowest one */ i1 = low1 ; i2 = low2 ; t = 0 ; while (i1 <= high1 && i2 <= high2) { if (A[i1] < A[i2]) { temp[t] = A[i1] ; i1++ ; t++ ; } else { temp[t] = A[i2] ; i2++ ; t++ ; } } // end while

CMSC 202 20

/* copy any remaining elements */ while (i1 <= high1) { temp[t] = A[i1] ; t++ ; i1++ ; } while (i2 <= high2) { temp[t++] = A[i2++] ; }

/* copy the now-sorted elements back into the original array */ for (t = low1; t <= high2; t++) { A[t] = temp[t - low1] ; }} // end function

CMSC 202 21

40 20 10 80 60 50 7 30 100 90 70

Quicksort

CMSC 202 22

• Quicksort is also a “divide and conquer” algorithm

• The array is continually divided in half at the pivot point and rearranged

• Therefore, recursion is a wise choice

Quicksort (con’t)

CMSC 202 23

// From on-line notes

void quicksort(data A[], index low, index high){ index q ;

if (low >= high) return ; q = partition(A, low, high) ; assert(q < high) ;

quicksort(A, low, q) ; quicksort(A, q + 1, high) ;}

CMSC 202 24

index partition(data A[], index low, index high){ data x, temp ; index i, j ;

i = low - 1; j = high + 1; x = A[low] ;

while (1) { /* Find an element less than x */ do { j = j - 1; } while (A[j] > x) ; /* Find an element greater than x */ do { i = i + 1; } while (A[i] < x);

CMSC 202 25

if (i < j) { temp = A[j] ; A[j] = A[i] ; A[i] = temp ; } else { return j; } } // end while} // end function