Vous êtes sur la page 1sur 20

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Sorting Algorithms and Complexity


A. Barbaro
UCLA

PIC 10B

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Outline for Today: Sorting (Chapter 11 of Big C++)

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Selection Sort Algorithm

The main idea: Move the smallest element to the top, repeat on subvector

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Selection Sort Algorithm

The main idea: Move the smallest element to the top, repeat on subvector
1 2 3

Find minimum value in the vector Swap with the value in the rst position Repeat the previous steps on the subvector which excludes the top (minimum) element Repeat the whole process until the subvector is only one element long

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Bubble Sort
The main idea: elements are compared pairwise, largest element sinks to the bottom, smaller elements bubble up to the top

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Bubble Sort
The main idea: elements are compared pairwise, largest element sinks to the bottom, smaller elements bubble up to the top
1

Consider the rst two items; if they are out of order, swap them Consider the second and third item; if they are out of order, swap them Repeat until the second-to-last and last item; the largest element will now be at the bottom Repeat the previous steps, but on the subvector which excludes the last (largest) element. Repeat the whole process until the subvector is only one element long

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Insertion Sort

The main idea: A list is created one element at a time; new items are inserted into the correct place in the list

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Insertion Sort

The main idea: A list is created one element at a time; new items are inserted into the correct place in the list
1 2

Begin with the rst element; this is the new sorted list Consider the second element; if its smaller than the rst, insert it ahead of the rst element, if not, leave it where it is These two elements now form the new sorted list Consider the third element and insert it where it belongs in the list. These three elements are now the sorted list Continue with the fourth element, etc.

3 4

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Merge Sort

The main idea: split the list in half, sort each half (using recursion), merge them together

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Merge Sort

The main idea: split the list in half, sort each half (using recursion), merge them together
1

Recursive function (merge_sort()) takes in a vector, an index at which to begin sorting, and an index to end sorting Split the list in half Call merge_sort( ) on the rst half and then on the second half Merge the two halves together with another function (merge())

2 3

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Quicksort

The main idea: pick a pivot element from the list, divide the list into two sublists, use recursion to sort each sublist and concatenate the two lists, with the pivot in between

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Quicksort

The main idea: pick a pivot element from the list, divide the list into two sublists, use recursion to sort each sublist and concatenate the two lists, with the pivot in between
1 2

Pick a pivot element (rst, last, or random) Create a list of elements smaller than the pivot and another list of elements larger than the element Sort each of the sublists (this will be a recursive function call) Concatenate the sorted list of smaller elements, the pivot, and the sorted list of larger elements; this will be a sorted vector

3 4

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

How can we compare sorting algorithms?

Once we have several algorithms, how can we compare them?

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

How can we compare sorting algorithms?

Once we have several algorithms, how can we compare them? Amount of memory

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

How can we compare sorting algorithms?

Once we have several algorithms, how can we compare them? Amount of memory Speed of the algorithm
Best-case scenario Worst-case scenario Random scenario

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Analyzing the Complexity

The amount of memory and speed of the algorithm are dependent on the length of the list being sorted

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Analyzing the Complexity

The amount of memory and speed of the algorithm are dependent on the length of the list being sorted They also dependent on how the algorithm has been implemented, and in what language

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Analyzing the Complexity

The amount of memory and speed of the algorithm are dependent on the length of the list being sorted They also dependent on how the algorithm has been implemented, and in what language The important thing about the algorithm is how it scales with the size of the list

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Analyzing the Complexity

The amount of memory and speed of the algorithm are dependent on the length of the list being sorted They also dependent on how the algorithm has been implemented, and in what language The important thing about the algorithm is how it scales with the size of the list To analyze this, we should time runs for many dierent sizes of vector and plot the size of the vector against the amount of time it takes to run the algorithm

Sorting Algorithms

Divide and Conquer Algorithms

Complexity of Sorting Algorithms

Analyzing the Complexity

The amount of memory and speed of the algorithm are dependent on the length of the list being sorted They also dependent on how the algorithm has been implemented, and in what language The important thing about the algorithm is how it scales with the size of the list To analyze this, we should time runs for many dierent sizes of vector and plot the size of the vector against the amount of time it takes to run the algorithm We will discuss how to formalize this complexity analysis next time

Vous aimerez peut-être aussi