sorting routines. objectives introduction bubble sort selection sort insertion sort quick sort merge...

34
SORTING ROUTINES

Upload: alicia-bradley

Post on 17-Dec-2015

266 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

SORTING ROUTINES

Page 2: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

OBJECTIVES

INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

Page 3: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

INTRODUCTION

What is sorting? Sorting simply means arranging items in

ascending or descending order. Two types of approaches to sorting are described

here: 1. The incremental approach 2. The divide-and-conquer approach (typically uses

recursion) Of the two, divide-and-conquer is by far the

fastest (in most cases)…but also the most complicated.

Page 4: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

BUBBLE SORT

The Bubble sort uses an incremental approach.

The following shows the sequence of steps in a Bubble Sort:

Page 5: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

BUBBLE SORT

After the first pass we notice that the largest value (5) has “bubbled” its way to the end of the list; however, the array is still not in order.

Continue to repeat this process until no swaps are made. Only then is the list in order.

On each subsequent pass the next largest value will “bubble” its way to its correct position near the end of the list.

Page 6: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

BUBBLE SORT

Very, very slow: This Bubble Sort is the slowest and most

inefficient of all the sorting routines. It should only be used if you have a very few

items to sort (say, 50 items or less).

Page 7: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

BUBBLE SORT

Page 8: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

SELECTION SORT

The Selection Sort uses an incremental approach.

During the first pass the smallest value is selected from the entire array and swapped with the first element.

On the second pass the smallest value is selected from the array beginning with the 2nd element and swapped with the second element, etc….the above description is for an ascending sort.

The following shows the sequence of steps in a Selection Sort:

Page 9: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

SELECTION SORT

Page 10: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

SELECTION SORT

Page 11: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

SELECTION SORT

Disadvantage: A disadvantage of the selection sort is that it will

not allow an early exit from the entire process if the list becomes ordered in an early pass.

Page 12: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

INSERTION SORT

The Insertion Sort uses an incremental approach.

It works similar to the way you might organize a hand of cards.

The unsorted cards begin face down on the table and are picked up one by one.

As each new unsorted card is picked up, it is inserted into the correct order in your organized hand of cards.

The following shows the sequence of steps in an Insertion Sort:

Page 13: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

INSERTION SORT

Page 14: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

INSERTION SORT

Page 15: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

INSERTION SORT

An advantage: The Insertion Sort has an advantage over the

Selection Sort since it takes advantage of a partially ordered list.

This is evidenced by the fact that in a best case, big O for an Insertion Sort is O(n), whereas for a Selection Sort, it is always O(n2).

Page 16: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

QUICK SORT

Two partitions: The Quick Sort uses a divide-and-conquer approach. It begins by breaking the original list into two partitions

(sections) based on the value of some “pivot value”. One partition will eventually contain all the elements with

values greater than the pivot value. The other will eventually contain all the elements with

values less than or equal to the pivot value. (This description is not always completely true, but close.)

Repeat this process on each partition. Notice the word partition above.

This is a salient feature of the Quick Sort. To identify this type of sort, look for the word “partition” (or equivalent term) in a rem or perhaps as a variable name.

Page 17: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

QUICK SORT

Two partitions: The Quick Sort uses a divide-and-conquer approach. It begins by breaking the original list into two partitions

(sections) based on the value of some “pivot value”. One partition will eventually contain all the elements with

values greater than the pivot value. The other will eventually contain all the elements with

values less than or equal to the pivot value. (This description is not always completely true, but close.)

Repeat this process on each partition. Notice the word partition above.

This is a salient feature of the Quick Sort. To identify this type of sort, look for the word “partition” (or equivalent term) in a rem or perhaps as a variable name.

Page 18: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

QUICK SORT

Page 19: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

QUICK SORT

Summary of how Quick Sort works: A “pivot value” is selected.

Usually this is the element at the center position of the array.

Elements in the array are moved such that all elements less than the pivot value are in one half (partition) and all elements larger than or equal to the pivot value are in the other half (partition).

This process is continually repeated on each partition.

The partitions become smaller until they each consist of just a single element.

At that point the array is ordered.

Page 20: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

MERGE SORT

The Merge Sort uses the divide-and-conquer approach.

It begins by placing each element into its own individual list.

Then each pair of adjacent lists is combined into one sorted list.

This continues until there is one big, final, sorted list. The process is illustrated below:

Page 21: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

MERGE SORT

Page 22: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

MERGE SORT

The merge sort is often implemented recursively as illustrated with the following code:

Page 23: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

BIG O SUMMARY

It will probably be easier to learn the Big O designation for each sorting and search routine when simultaneously viewing all of them in a table

Occasionally, “best case” is referred to as the most restrictive or fastest executing case.

Similarly, “worst case” is referred to as the least restrictive or slowest executing case.

Page 24: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

BIG O SUMMARY

Page 25: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

LESSON 41SORTING ROUTINES

Page 26: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

SORTING ROUTINESWHAT AM I?????

Page 27: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

WHAT AM I?

Page 28: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

WHAT AM I?

Page 29: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

WHAT AM I?

Page 30: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

WHAT AM I?

Page 31: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

WHAT AM I?

Page 32: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

WHAT AM I?

Page 33: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

WHAT AM I?

Page 34: SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT

SORTING ROUTINES