26
Searching and Sorting

Searching and Sorting. Overview Search Analysis of search algorithms Sorting Analysis of sort algorithms Recursion p. 2 of 26

  • View
    235

  • Download
    0

Embed Size (px)

Citation preview

Searching and Sorting

OverviewSearchAnalysis of search algorithmsSortingAnalysis of sort algorithms

Recursion p. 2 of 26

Linear SearchConsider the code on p. 212 of your text.Explain the first line of the method. What

does each symbol mean?Step by step, what would happen when

this code searches an array of 100 items?What could we do to make this a non-

static method?

Searching and sorting p. 3 of 26

Linear search: analysisThe time complexity of linear search is:A. O(1)B. O(log2n)

C. O(n)D. O(2n)E. None of the above

Searching and sorting p. 4 of 26

Binary searchConsider the code on p. 215 of your text. Walk through the first line of the method.

What does each symbol mean? What happens if there’s an odd number of

items in the list being searched? What could we do to make this a non-static

method?

Searching and sorting p. 5 of 26

Binary Search: exampleWhat items would be considered (in order) if we

were using binary search to locate the number 86 in the following list?

1 3 5 7 8 10 15 20 27 31 37 41 43 47 53 59 61 67 71 73 86

Searching and sorting p. 6 of 26

Binary search: analysisThe time complexity of binary search is:A. O(1)B. O(log2n)

C. O(n)D. O(2n)E. None of the above

Searching and sorting p. 7 of 26

Sorting: selection sortpublic void selectionSort(){ //NOT static int min; T temp; for (int i=0;i<data.length-1;i++){ min=i; for (int j=i+1;j<data.length;j++){ if(data[j].compareTo(data[min])<0){ min=j; } } temp=data[min]; data[min]=data[i]; data[i]=temp; }} Searching and sorting p. 8 of 26

Tracing selection sortTrace how selection sort would sort the following

list: 4 1 3 2

Searching and sorting p. 9 of 26

Just before the value of i is

The list contains (in order)

0 4 1 3 2

1

2

3

4

Tracing selection sort (2)Trace how selection sort would sort the following

list: 4 1 3 2

Searching and sorting p. 10 of 26

Just before the value of i is

The list contains (in order)

0 4 1 3 2

1 1 4 2 3

2 1 2 4 3

3 1 2 3 4

4 No change – fails loop test

Selection sort: analysisThe time complexity of selection sort is:A. O(1)B. O(log2n)

C. O(n)D. O(n2)E. None of the above

Searching and sorting p. 11 of 26

Tracing insertion sortConsider the code p. 223 (section 8.2) Trace how

insertion sort would sort the following list: 4 1 3 2

Searching and sorting p. 12 of 26

Just before the value of i is

The list contains (in order)

0 4 1 3 2

1

2

3

4

Tracing insertion sort (2)Trace how insertion sort would sort the following

list: 4 1 3 2

Searching and sorting p. 13 of 26

Just before the value of i is

The list contains (in order)

0 4 1 3 2

1 4 1 3 2

2 1 4 3 2

3 1 3 4 2

4 1 2 3 4

5 No change – fails loop test

Insertion sort: analysisThe time complexity of insertion sort is:A. O(1)B. O(log2n)

C. O(n)D. O(n2)E. None of the above

Searching and sorting p. 14 of 26

Sorting: quicksortConsider the code p. 227 (still section 8.2). You try:Sort the list 10 1 9 2 8 3 7 4 6 5using quicksort. Trace the sort, and also draw a diagram showing

the recursive calls.

Searching and sorting p. 15 of 26

Characteristics of sorting algorithmsBig-OhRecursive?Stable?

Searching and sorting p. 16 of 26

Quicksort: analysisThe time complexity of quicksort is:A. O(1)B. O(log2n)

C. O(n)D. O(n2)E. None of the above

Searching and sorting p. 17 of 26

Sorting: mergesortConsider the code p. 230 (still section 8.2). Hand-trace sorting the list

10 1 9 2 8 3 7 4 6 5

using mergesort.Trace the sort, and also draw a diagram showing

the recursive calls.

Searching and sorting p. 18 of 26

Mergesort: analysisThe time complexity of mergesort is:A. O(1)B. O(log2n)

C. O(n)D. O(n2)E. None of the above

Searching and sorting p. 19 of 26

Sorting: radix sortConsider the code pp. 233-4 (Section 8.3). Try sorting

the list: 455 503 312 101 201 305 325 150 423 221using radix sort. Trace the sort, and also draw a

diagram showing the queues involved (similar to Figure 8.7).

Searching and sorting p. 20 of 26

Radix sort: analysisThe time complexity of radix sort is:A. O(1)B. O(log2n)

C. O(n)D. O(n2)E. None of the above

Searching and sorting p. 21 of 26

Time complexity of sorting

Searching and sorting p. 22 of 26

Algorithm Time complexity

Selection sort O(n2)

Insertion sort O(n2)

Quicksort O(n2), but O(n log n) on average

Mergesort O(n log n)

Radix sort O(kn), where k is the # digits

Now see them runFor animations of several common sorting

algorithms, see http://www.sorting-algorithms.com

Searching and sorting p. 23 of 26

Some built-in Java sortsFor information about how this is done in Java:

http://download.oracle.com/javase/tutorial/collections/algorithms/index.html#sorting

Java uses a “stable” sort – what does that mean? Give an example.

Searching and sorting p. 24 of 26

Searching and sorting summaryKnow the algorithms, their complexity, and

some pros and cons of:Linear vs. binary searchInsertion sort, selection sort, quicksort,

mergesort, radix sort With regard to ease of coding, time

requirements, space requirements

Searching and sorting p. 25 of 26

Coming attractionsNext time, we’ll begin looking at a new data

structure, trees, and a new Abstract Data Type, Dictionary.

Homework: read chapter 9 (or the equivalent in the earlier edition).

Searching and sorting p. 26 of 26