COP3502_26_N-SquaredSorts

COP3502_26_N-SquaredSorts - Sorting: 2 O(n ) Algorithms...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Computer Science Department University of Central Florida Sorting: O(n 2 ) Algorithms COP 3502 – Computer Science I
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sorting: O(n 2 ) Algorithms page 2 Sorting: O(n 2 ) Algorithms Sorting Algorithms: Fundamental problem in Computer Science Sorting is done to make searching easier Most programs do this: Excel, Access, and others.
Background image of page 2
Sorting: O(n 2 ) Algorithms page 3 Sorting: O(n 2 ) Algorithms Sorting Algorithms: We will study several sorting algorithms in this class Some are clearly much faster than others For today, we will go over the “simple sorts” These “simple sorts” all run in O(n 2 ) time Selection Sort Insertion Sort Bubble Sort We will assume that the input to the algorithm is an array of values (sorted or not)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sorting: O(n 2 ) Algorithms page 4 Sorting: O(n 2 ) Algorithms Selection Sort: Given: an array of n unsorted items The algorithm to sort n numbers is as follows: 1) Find the minimum value in the list of n elements Search from index 0 to index n-1 2) Swap that minimum value with the value in the first position At index 0 3) Repeat steps 1 and 2 for the remainder of the list Example: We now start at the 2 nd position (index 1). Find minimum value from index 1 to index n-1 Swap that minimum value with the value at index 1
Background image of page 4
Sorting: O(n 2 ) Algorithms page 5 Sorting: O(n 2 ) Algorithms Selection Sort: The algorithm to sort n numbers is as follows: There is a FOR loop that iterates from i = 0 to i = n-1 FOR the i th element (as i ranges from 0 to n-1) 1) Determine the smallest element in the rest of the array To the right of the i th element 2) Swap the current i th element with the element identified in part (1) above (the smallest element) Essentially: The algorithm first picks the smallest element and swaps it into the first location. Then it picks the next smallest element and swaps it into the next location, etc.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sorting: O(n 2 ) Algorithms page 6 Selection Sort: Example: Here is an array of 5 integers Remember, we have a for loop FOR i = 0 to n – 1 { Find the minimum value in the range from i to n-1 SWAP this minimum value with the Sorting: O(n 2 ) Algorithms 0 1 2 3 4 20 8 5 10 7 i = 0
Background image of page 6
Sorting: O(n 2 ) Algorithms page 7 Selection Sort: Example: Here is an array of 5 integers 5 (at index 2) is the smallest element from the range i = 0 to 4 So SWAP the value at index 2 with the value at index 0 SWAP the 5 and the 20 Sorting: O(n 2 ) Algorithms 0 1 2 3 4 20 8 5 10 7 0 1 2 3 4 5 8 20 10 7 i = 0
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sorting: O(n 2 ) Algorithms page 8 Selection Sort: Example: Here is an array of 5 integers 7 (at index 4) is the smallest element from the range i = 1 to 4 So SWAP the value at index 4 with the value at index 1 SWAP the 7 and the 8 Sorting: O(n 2 ) Algorithms 0 1 2 3 4 5 8 20 10 7 i = 1 0 1 2 3 4 5 7 20 10 8
Background image of page 8
Sorting: O(n 2 ) Algorithms page 9 Selection Sort:
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/21/2011 for the course COP 3330 taught by Professor Staff during the Spring '08 term at University of Central Florida.

Page1 / 43

COP3502_26_N-SquaredSorts - Sorting: 2 O(n ) Algorithms...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online