Lecture 08 Sorting. Sorts Many programs will execute more efficiently if the data they process is sorted before processing begins. – We first looked at.

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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Sorting Sorting is the process of arranging a list of items in a particular order The sorting process is based on specific value(s) Sorting a list of test.
 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
HST 952 Computing for Biomedical Scientists Lecture 9.
Simple Sorting Algorithms
Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Algorithm Efficiency and Sorting
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
Selection Sort, Insertion Sort, Bubble, & Shellsort
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.
Programming Logic and Design Fourth Edition, Comprehensive
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Simple Sort Algorithms Selection Sort Bubble Sort Insertion Sort.
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.
CSC 211 Data Structures Lecture 15
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 19: Searching and Sorting Algorithms
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
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.
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
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.
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
3 – SIMPLE SORTING ALGORITHMS
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
SORTING ALGORITHMS King Saud University College of Applied studies and Community Service CSC 1101 By: Nada Alhirabi 1.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
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.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
The Sorting Methods Lecture Notes 10. Sorts Many programs will execute more efficiently if the data they process is sorted before processing begins. –
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Data Structures Arrays and Lists Part 2 More List Operations.
Sequential (Linear) Binary Selection** Insertion** Merge.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
The Linear and Binary Search and more Lecture Notes 9.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Chapter 9: Sorting and Searching Arrays
19 Searching and Sorting.
Searching and Sorting Algorithms
Lecture 14 Searching and Sorting Richard Gesick.
Simple Sorting Algorithms
Searching and Sorting Linear Search Binary Search ; Reading p
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.
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting 1-D Arrays
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
Searching and Sorting Arrays
Simple Sorting Algorithms
Chapter 19 Searching, Sorting and Big O
Simple Sorting Algorithms
Applications of Arrays
Presentation transcript:

Lecture 08 Sorting

Sorts Many programs will execute more efficiently if the data they process is sorted before processing begins. – We first looked at a linear search it doesn’t care whether the data is sorted or not the algorithm starts at the first element in the vector, and looks at every element in the vector, in sequence, until it finds what it is looking for, or comes to the end of the vector – With small data sets this algorithm performs acceptably – If the data sets are of significant size, than performance can become unacceptable

Sorts If we know that a data set is sorted in some order (lowest to greatest, largest to smallest, highest priority to lowest priority), then we can write searches to take advantage of this fact. – If you have misplaced your calculator in the library in the afternoon, you do not have to retrace your steps from when you arrived on campus in the morning to find it – You start your search in the library – When looking up a phone number in the phone book, you do not start at page 1 and scan each page in sequence, until you find the name you are looking for

Sorting Comparison There are all kinds of sorts For our purposes a sort is a rearrangement of data into either ascending or descending order Fast SortsversusSlow Sorts O (N log 2 N)O (N 2 ) slow sorts are easy to code and sufficient when the amount of data is small

Bubble Sort - N 2 Sort Strategy – ‘bubble’ the smallest item to the left (slot 0) – ‘bubble’ the next smallest item to slot 1 – ‘bubble’ the third smallest item to slot 2 algorithm (for one pass) walk through the Vector if this item (in spot j) is smaller than the item in spot j-1 swap item in spot j with the item in spot j-1 code for (int j = count-1; j >= 0; j--) { if (nums[j] < nums[j-1]) { int temp = nums[j]; nums[j] = nums[j-1]; nums[j-1] = temp; }

Bubble Sort (con’t) This code for one pass finds the smallest element in the vector and puts it into slot 0 Now wrap this code in a loop that will find succeeding smaller elements and put them into the proper position in the Vector // outer for loop controls the spot that gets the appropriate smallest value for (int I=0; I<size-1;I++) for (int j = count-1; j >= 0; j--) { if (nums[j] < nums[j-1]) { int temp = nums[j]; nums[j] = nums[j-1]; nums[j-1] = temp; }

Selection Sort We have noticed that the most “expensive” part of the bubble sort is swapping (three lines of code to execute) A selection sort reduces the number of swaps until the end of each pass, when we know what the smallest remaining value is, and placing it appropriately Strategy – find the smallest item, and swap it with slot 0 – find the next smallest item and swap it with slot 1 – find the third smallest item and swap it with slot 2 algorithm (for one pass) walk through the Vector the first item is labeled the smallest Every other element is compared to the smallest if it is smaller, than it is labeled the smallest At the end of the walkthrough, the first is swapped with the smallest

Selection Sort (con’t) The algorithm for one pass finds the smallest element in the vector and puts it into slot 0 Now wrap this code in a loop that will find succeeding smaller elements and put them into the proper position in the Vector // outer for loop controls the spot that gets the appropriate smallest // value (same as bubble sort) for (int left = 0; left < count - 1; left++) { // last one will be correct int smallest = left;// we will keep the index to the smallest for (int j = left + 1; j < count; j++) if (nums[j] < nums[smallest] { smallest = j; } if (smallest != left) // no sense swapping if left is smallest { int temp = nums[smallest]; nums[smallest] = nums[left]; nums[left] = temp; }

Use FindSmallest() routine with SelectionSort() We have spent a lot of time looking at FindSmallest() routines… you should know how to do that Incorporate that knowledge into selection sort // outer for loop controls the spot that gets the appropriate smallest // value (same as bubble sort) for (int left = 0; left < count - 1; left++) { int smallest = findSmallest(nums, count, left); // use what we already know // pass the starting point of the // remainder of the vector to look at if (smallest != left ) { int temp = nums[smallest]; nums[smallest] = nums[left]; nums[left] = temp; }

findSmallest(const int nums[], int count, int left); int findSmallest(const int nums[], int count, int left) { int smallest = left; // start with first index as the smallest for (int j = left + 1; j < count; j++) if (nums[j] < nums[smallest] { smallest = j; } return smallest; }