chapter 8 sorting and searching goals: 1.java implementation of sorting algorithms 2.selection and...

8
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort 4.Sequential search and binary search.

Upload: virginia-dennis

Post on 17-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort

Chapter 8 Sorting and Searching

Goals:1. Java implementation of sorting algorithms2. Selection and Insertion Sorts3. Recursive Sorts: Mergesort and Quicksort4. Sequential search and binary search.

Page 2: Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort

Selection Sort• Search and swap algorithm– Algorithm• Find smallest element• Exchange it with the first position• Repeat the above steps for the second, third, and

other positions until array sorted.

• Example:– 8 1 4 6 – 1 8 4 6 //1st pass– 1 4 8 6 //2nd pass– 1 4 6 8 // 3rd pass

Page 3: Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort

Selection Sort• For an array of n elements, the array is

sorted after n – 1 passes. • Best case scenario array is sorted• Worst case scenario all elements has to be

changed or swapped. • Inefficient for very large number arrays

Page 4: Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort

Insertion• Algorithm– Think of the 1st element being sorted.– Compare the next element with the sorted one – Insert it if need causing all elements to move to

the open empty space. – Repeat process for the next element by thinking

of the first two elements being sorted. Insert if necessary.

Page 5: Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort

Example Insertion Sort8 1 4 6 //Start by saying 8 is sorted1 8 4 6 //first pass Insert 1 by comparing to 8

1 4 8 6 //second pass Insert 4 by comparing to 1 & 8

1 4 6 8 //third pass Insert 6Notes:1. For an array of n elements, the array is sorted after n-1 runs.2. After the kth pass, a[0],a[1],….,a[k] are sorted with respect

to each other but not necessarily in their final position3. The worst case for insertion sort occurs if the array is

initially sorted in reverse order, since this will lead to the max possible number of comparisons and moves.

4. The Best case for insertion sort occurs if the array is already sorted.

Page 6: Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort

Merge Sort – Recursive Sort• Divide and conquer more efficient than

Insertion and Selection sorts.• Merge Sort Algorithm– Break Array in to two halves– Mergesort the left half and MergeSort the right

half by breaking them in half and keep repeating process until down to single element

– Merge the single elements to form subarrays and merge subarrays until you get back to single Array

Page 7: Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort

Merge Sort Example5 -3 2 4 0 6

5 -3 2 4 0 6

5 -3 2 4 0 6

5 -3 4 0

-3 5 0 4

-3 2 5 0 4 6

-3 0 2 4 5 6

Page 8: Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort

Analysis of MergeSort

• The major disadvantage of mergeSort is that it needs temporary array that is as large as the original array to be sorted.

• Mergesort is not affected by the initial ordering of the elements. Thus best, worst and average cases have similar run times.