Sorting and Complexity

Slides:



Advertisements
Similar presentations
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Advertisements

Sorting I Chapter 8 Kruse and Ryba. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores –Words of.
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Chapter 9: Searching, Sorting, and Algorithm Analysis
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Visual C++ Programming: Concepts and Projects
Data Structures Chapter 8 Sorting Andreas Savva. 2 Sorting Smith Sanchez Roberts Kennedy Jones Johnson Jackson Brown George Brown 32 Cyprus Road Good.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Sorting 2 An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There.
Searching and Sorting Arrays
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
CSCI 51 Introduction to Programming March 12, 2009.
3 – SIMPLE SORTING ALGORITHMS
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014 Curt Hill Algorithm Analysis How Do We Determine the Complexity of Algorithms.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
Searching and Sorting Searching algorithms with simple arrays
Prof. U V THETE Dept. of Computer Science YMA
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
The Bubble Sort Mr. Dave Clausen La Cañada High School
Chapter 9: Sorting and Searching Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
Sorting Mr. Jacobs.
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
Introduction to Search Algorithms
Arrays 2.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Design and Analysis of Algorithms
10.3 Bubble Sort Chapter 10 - Sorting.
Linear and Binary Search
Algorithm Efficiency and Sorting
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Describing algorithms in pseudo code
Bubble, Selection & Insertion sort
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Search,Sort,Recursion.
Principles of Computing – UFCFA3-30-1
Searching.
Searching and Sorting Arrays
Search,Sort,Recursion.
Principles of Computing – UFCFA3-30-1
Sorting.
CHAPTER 9 SORTING & SEARCHING.
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
10.3 Bubble Sort Chapter 10 - Sorting.
Presentation transcript:

Sorting and Complexity Sorting is to arrange an array into some order alphabetical (for strings and char) ascending/descending for integer and real It is a common problem with many applications data records (phone company, hospital, classes) It fits in nicely with the fact that when an array is ordered, it can be searched efficiently .....the difference between O(n) and O(log2 n) ....so, if arrays can be sorted without too much difficulty, then we can use efficient binary search

Sorting Algorithms Since sorting has such important applications, there has been much effort in developing sorting techniques There are many known sorting algorithms Our text mentions four: selection sort bubble sort insertion sort quick sort (done recursively in our book, and so we will postpone discussion until the recursion chapter)

Selection Sort Go through the list from 1 to max and find the smallest (say it was in location i) Swap the contents of location 1 with location i Go through the list from 2 to max and find the smallest (say it was in location j) Swap the contents of location 2 with location j repeat until all locations are checked. (stop after you have gone through the list from max-1 to max, finding the smallest)

Selection Sort for j := 1 to max-1 do {find the minimum max-1 times} begin where := j; for k:=j+1 to max do if List[k]<List[where] then where:=k; {find where the smallest item is} if where <> j then {if necessary, swap items} temp := List[where]; List[where] := List[j]; List[j] := temp end end;

Selection Sort Algorithm INPUT : Data: IntegerArray OUTPUT: Data -- sorted in ascending order FOR j := 1 TO max - 1 DO Find where such that Data[where] is the smallest between Data[First] and Data[max]; Exchange Data[First] and Data[where]; Performance Analysis 1. How many repetitions of the FOR loop will occur? 2. How many steps needed for each value of the FOR loop’s variable ? 3. How much work is done on each of these steps?

3. How much work is done in each step? Time Complexity: 1. How many repetitions will take place in the FOR loop ? max - 1 2. How many steps needed for each value of the FOR loop? (max - 1) / 2 (on average) 3. How much work is done in each step? about 2 things, depending on whether a swap is needed So, we have (max-1)([(max-1)/2]*2) = max2 - 2max + 1 Therefore, the time needed is proportional to max2 I.e.,(letting n be the size of the array) the algorithm is O(n2)

Bubble (Sink) Sort Always compare adjacent members of the array, swapping them if that pair are not in (relative) order with one another. Keep doing this until no more swaps are necessary. If we start at the bottom of the array, then at each pass through the array, the smallest item will “bubble up” to the top. (If we start at the top of the array, then at each pass through the array, the largest item will “sink down” to the bottom...ought to be called a Sink Sort).

Sink Sort (as in the text) latest := max-1; Repeat changed := false; {boolean variable, saying whether we’ve} for j := 1 to latest do {made a swap or not} if List[j] > List[j+1] then begin {swap values, and mark changed = true} temp := List[j]; List[j] := List[j+1]; List[j+1] := temp; changed := true end; latest := latest - 1 {decrement length} Until (not changed) or (latest = 1);

Bubble Sort Algorithm INPUT : Data: ARRAY OF INTEGER OUTPUT: Data -- sorted in descending order Method REPEAT (latest from max-1 downto 1) FOR j := 1 TO latest DO IF Data[j] > Data[j + 1] THEN swap Data[j] and Data[j+1] 1. How many repetitions will take place in the outer Repeat loop ? 2. For each repetition of the outer Repeat loop, how many steps will occur in the inner FOR loop? 3. For each instance of the inner For loop, how much work needs to be done?

Bubble sort efficiency 1. How many repetitions will take place in the outer Repeat loop ? in best case, only 1. in worst case max-1. average = ?? 2. For each repetition of the outer Repeat loop, how many steps will occur in the inner FOR loop? on average, (max-1)/2 3. For each instance of the inner For loop, how much work needs to be done? depending on how out of order the list is, maybe 2 on average So, we have (max-1)([(max-1)/2]*2) = max2 - 2max + 1 Therefore, the time needed is proportional to max2 I.e.,(letting n be the size of the array) the algorithm is O(n2)

Insertion Sort Given an array with the first k elements in order relative to each other (k will start out at 1) take the k+1st element and find where it would fit into, in the first k elements. call this location j move the stuff between j+1 and k “down” one notch in the array stick the old k+1 element into the j position Do the above for all the values of k up to max-1

Insertion Sort For newguy := 2 to max do begin temp:=List[newguy]; k:=newguy; finished:=false; {a boolean to say whether we’re finished} While (k>1) and (not finished) do { looking where to put} if temp < List[k-1] then {the newguy} List[k]:=List[k-1]; k := k-1 end else finished:= true; {we foundwhere to put the newguy} List[k] := temp end;

Insertion Sort Algorithm INPUT : Data: ARRAY OF INTEGER OUTPUT: Data -- sorted in ascending order FOR newguy := 2 TO max DO begin k := Newguy ; WHILE ( k > 1) AND (not finished) do if (Data[k ] < Data[k-1] ) DO begin move Data[k ] to the k -1st position k := k -1 end else finished:=true; Data[k] := Data[Newguy] end; 1. How many repetitions will take place in the FOR loop ? 2. How many things happen during each step of the FOR loop? 3. How many things happen during each step of the While loop?

Time Complexity of Insertion Sort 1. How many repetitions will take place in the FOR loop ? max-1 2. How many things happen during each step of the FOR loop? 1 or 2, plus the while loop. the while loop takes 1 step in the best case. Takes on average (max-1)/2 in the worst case. Average is maybe (max-1)/4 ?? 3. How many things happen during each step of the While loop? 1 movement So....(max-1)(max-1)/2 in worst case = .(max2 - 2max + 2)/2 Hence, if there are n items in the array, this is an O(n2) algorithm