Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
 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
SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT.
CPS120: Introduction to Computer Science Searching and Sorting.
CSE 373: Data Structures and Algorithms
Simple Sorting Algorithms
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
Lecture 14: A Programming Example: Sorting Yoni Fridman 7/24/01 7/24/01.
Algorithm Efficiency and Sorting
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Analysis of Algorithms CS 477/677
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
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.
CSE 1301 J Lecture 13 Sorting Richard Gesick. CSE 1301 J 2 of 30 Sorting an Array When an array's elements are in random order, our Sequential Search.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
ITEC 2620A Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 2620a.htm Office: TEL 3049.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
CSE 373 Data Structures and Algorithms
1 Data Structures and Algorithms Sorting I Gal A. Kaminka Computer Science Department.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
Sorting. 2 contents 3 kinds of sorting methods – Selection, exchange, and insertion O(n 2 ) sorts – VERY inefficient, but OK for ≈ 10 elements – Simple.
Bubble Sort 18 th December 2014 With Mrs
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
CSCI 51 Introduction to Programming March 12, 2009.
Bubble Sort.
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
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.
Sorting Sorting takes an unordered array and makes it an ordered one
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Computer Science 101 A Survey of Computer Science Sorting.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CPS120: Introduction to Computer Science Sorting.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: 20m.htm Office: TEL 3049.
Searching and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 9: Searching, Sorting, and Algorithm Analysis
Algorithm Analysis CSE 2011 Winter September 2018.
Bubble, Selection & Insertion sort
Selection Sort Sorted Unsorted Swap
Analysis of Bubble Sort and Loop Invariant
ITEC 2620M Introduction to Data Structures
Computer Science 111 Fundamentals of Computer Programming I
And now for something completely different . . .
Lecture 6 Efficiency of Algorithms (2) (S&G, ch.3)
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Sorting … and Insertion Sort.
IT 4043 Data Structures and Algorithms
Searching and Sorting Arrays
Simple Sorting Algorithms
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Analysis of Algorithms
Simple Sorting Algorithms
Simple Sorting Algorithms
Applications of Arrays
Presentation transcript:

Computer Science 101 Introduction to Sorting

Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical order for purposes of –Reports by category –Summarizing data –Searching data

Sorting We’re given a list of data in random order At the end of the sorting, each datum is less than or equal to its successor in the list For each i from 1 to N - 1, A(i) <= A(i + 1)

Visualizing the Results Sorting Algorithm List A Size = Sorting algorithms usually move data around in the original list

First Algorithm: Selection Sort Strategy: –Find the largest item in the list and exchange it with the last item in the list –Find the next largest item in the list and exchange it with the next to the last item in the list –Etc LargestUpper

First Algorithm: Selection Sort Strategy: –Find the largest item in the list and exchange it with the last item in the list –Find the next largest item in the list and exchange it with the next to the last item in the list –Etc LargestUpper

First Algorithm: Selection Sort Strategy: –Find the largest item in the list and exchange it with the last item in the list –Find the next largest item in the list and exchange it with the next to the last item in the list –Etc LargestUpper

First Algorithm: Selection Sort Strategy: –Find the largest item in the list and exchange it with the last item in the list –Find the next largest item in the list and exchange it with the next to the last item in the list –Etc LargestUpper

First Algorithm: Selection Sort Strategy: –Find the largest item in the list and exchange it with the last item in the list –Find the next largest item in the list and exchange it with the next to the last item in the list –Etc LargestUpper

First Algorithm: Selection Sort Strategy: –Find the largest item in the list and exchange it with the last item in the list –Find the next largest item in the list and exchange it with the next to the last item in the list –Etc LargestUpper

First Algorithm: Selection Sort Strategy: –Find the largest item in the list and exchange it with the last item in the list –Find the next largest item in the list and exchange it with the next to the last item in the list –Etc LargestUpper

First Algorithm: Selection Sort Strategy: –Find the largest item in the list and exchange it with the last item in the list –Find the next largest item in the list and exchange it with the next to the last item in the list –Etc LargestUpper

First Algorithm: Selection Sort Uses an algorithm we’ve already seen, search for the largest, as a component A The list N The size of the list Upper The end of the unsorted portion of the list Largest The position of the largest item so far Current Used to traverse the list during a search

The Selection Sort Algorithm set Upper to N while Upper > 1 do set Largest to the position of the largest item between positions 1 and Upper exchange A(Largest) and A(Upper) decrement Upper The component in red is the search for largest algorithm We’ll expand that into detailed form next Note for now that the code in red executes N – 1 times.

The Selection Sort Algorithm set Upper to N while Upper > 1 do set Largest 1 set Current to 2 while Current <= Upper do if A(Current) > A(Largest) then set Largest to Current increment current exchange A(Largest) and A(Upper) decrement Upper The code in red is the complete search for largest algorithm Note that the first search requires N – 1 comparisons and each remaining search requires one less comparison

The Selection Sort Algorithm set Upper to N while Upper > 1 do set Largest 1 set Current to 2 while Current <= Upper do if A(Current) > A(Largest) then set Largest to Current increment current exchange A(Largest) and A(Upper) decrement Upper The total number of comparisons = (N 2 – N) / 2 The total number of exchanges = N - 1

Improving Selection Sort set Upper to N while Upper > 1 do set Largest 1 set Current to 2 while Current <= Upper do if A(Current) > A(Largest) then set Largest to Current increment current if Largest  Upper then exchange A(Largest) and A(Upper) decrement Upper Some exchanges might not be necessary What is the best case? the worst case?

2nd Algorithm: Bubble Sort Strategy: –Pass through the list from left to right –Compare each element with the one to its left –Swap them if they are out of order –Such a pass will put largest at the right end –Continue making these passes until sorted Current

2nd Algorithm: Bubble Sort Strategy: –Pass through the list from left to right –Compare each element with the one to its left –Swap them if they are out of order –Such a pass will put largest at the right end –Continue making these passes until sorted Current

2nd Algorithm: Bubble Sort Strategy: –Pass through the list from left to right –Compare each element with the one to its left –Swap them if they are out of order –Such a pass will put largest at the right end –Continue making these passes until sorted Current

2nd Algorithm: Bubble Sort Strategy: –Pass through the list from left to right –Compare each element with the one to its left –Swap them if they are out of order –Such a pass will put largest at the right end –Continue making these passes until sorted Current

2nd Algorithm: Bubble Sort Strategy: –Pass through the list from left to right –Compare each element with the one to its left –Swap them if they are out of order –Such a pass will put largest at the right end –Continue making these passes until sorted Current

2nd Algorithm: Bubble Sort Strategy: –Pass through the list from left to right –Compare each element with the one to its left –Swap them if they are out of order –Such a pass will put largest at the right end –Continue making these passes until sorted Current

2nd Algorithm: Bubble Sort Strategy: –Pass through the list from left to right –Compare each element with the one to its left –Swap them if they are out of order –Such a pass will put largest at the right end –Continue making these passes until sorted Current

2nd Algorithm: Bubble Sort Strategy: –Pass through the list from left to right –Compare each element with the one to its left –Swap them if they are out of order –Such a pass will put largest at the right end –Continue making these passes until sorted Current

The Bubble Sort Algorithm set Upper to N while Upper > 1 do bubble the largest item down to upper decrement Upper Note that the bubble process is executed N – 1 times

The Bubble Sort Algorithm set Upper to N while Upper > 1 do set Current to 2 while Current <= Upper do if A(Current) < A(Current – 1) then exchange A(Current) and A(Current – 1) increment Current decrement Upper Note that the bubble process is executed N – 1 times Note that the first bubble process requires N – 1 comparisons and each remaining bubble process needs one less comparison How many total comparisons? Exchanges?

Improving Bubble Sort set Upper to N while Upper > 1 do set Current to 2 while Current <= Upper do if A(Current) < A(Current – 1) then exchange A(Current) and A(Current – 1) increment Current decrement Upper If no exchanges are performed during a bubble process, then the list is sorted Perhaps we can quit the outer loop when this is the case

Improving Bubble Sort set Upper to N Set Sorted to false while Upper > 1 and not Sorted do set Current to 2 set Sorted to true while Current <= Upper do if A(Current) < A(Current – 1) then set Sorted to false exchange A(Current) and A(Current – 1) increment Current decrement Upper We assume that the list is sorted before each bubble process and prove that it’s not sorted when an exchange is made What is the best case # of comparisons now?