Visual C++ Programming: Concepts and Projects

Slides:



Advertisements
Similar presentations
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.
Advertisements

Understanding the Need for Sorting Records
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
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)
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
CHAPTER 11 Sorting.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
Sorting Chapter 10.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Sorting Chapter 10. Chapter 10: Sorting2 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn how to implement.
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Sorting Course Lecture Slides 24 May 2010 “The real focus here is bringing.
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.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Chapter 16: Searching, Sorting, and the vector Type.
1 Arrays 2: Sorting and Searching Admin. §1) No class Thursday. §2) Will cover Strings next Tuesday. §3) Take in report. §4) Hand out program assignment.
Chapter 19: Searching and Sorting Algorithms
Searching and Sorting Chapter Sorting Arrays.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 19 Thanks for Lecture Slides:
Sorting Chapter 10. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
Chapter 7: Sorting Algorithms Insertion Sort. Sorting Algorithms  Insertion Sort  Shell Sort  Heap Sort  Merge Sort  Quick Sort 2.
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.
ECE 103 Engineering Programming Chapter 24 Sorting Herbert G. Mayer, PSU CS Status 6/2/2015 Initial content copied verbatim from ECE 103 material developed.
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.
Sorting. 2 contents 3 kinds of sorting methods – Selection, exchange, and insertion O(n 2 ) sorts – VERY inefficient, but OK for ≈ 10 elements – Simple.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
CSCI 51 Introduction to Programming March 12, 2009.
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
3 – SIMPLE SORTING ALGORITHMS
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting Sorting takes an unordered array and makes it an ordered one
Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Data Structures and Algorithms Lecture 17, 18 and 19 (Sorting) Instructor: Quratulain Date: 10, 13 and 17 November, 2009 Faculty of Computer Science, IBA.
An Object-Oriented Approach to Programming Logic and Design Chapter 8 Advanced Array Concepts.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
SORTING Chapter 8. Chapter Objectives  To learn how to use the standard sorting methods in the Java API  To learn how to implement the following sorting.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 8A Binary Search (Concepts)
Chapter 16: Searching, Sorting, and the vector Type.
Prof. U V THETE Dept. of Computer Science YMA
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
Searching and Sorting Algorithms
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Arrays 2.
Design and Analysis of Algorithms
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.
Bubble, Selection & Insertion sort
Searching and Sorting Arrays
Sorting Chapter 10.
Presentation transcript:

Visual C++ Programming: Concepts and Projects Chapter 9A: Sorting (Concepts)

Programming with Visual C++ Objectives In this chapter, you will: Create and use a Swap() method to exchange data values in arrays Use nested loops to sort elements in an array Implement a selection sort algorithm Learn about the bubble sort technique Programming with Visual C++

Objectives (continued) Learn about the insertion sort technique Analyze the complexity and efficiency of a sorting algorithm (big-O) Programming with Visual C++

Exchanging Data Values in an Array Two ways to put data into order Data ordered from lowest to highest values is in ascending order Data ordered from highest to lowest is in descending order To sort data, you must be able to exchange (swap) values Programming with Visual C++

Exchanging Data Values in an Array (continued) Programming with Visual C++

Exchanging Data Values in an Array (continued) Swapping requires a temporary variable and three steps Programming with Visual C++

Exchanging Data Values in an Array (continued) The code for a Swap() method Programming with Visual C++

Exchanging Data Values in an Array (continued) Programming with Visual C++

Exchanging Data Values in an Array (continued) Programming with Visual C++

Exchanging Data Values in an Array (continued) Programming with Visual C++

Exchanging Data Values in an Array (continued) Programming with Visual C++

Programming with Visual C++ Sorting Strategies This chapter investigates three methods of sorting data Selection sort Bubble sort Insertion sort All of these use nested loops Programming with Visual C++

Sorting Strategies (continued) Programming with Visual C++

Sorting Strategies (continued) All of the sorting methods Make a series of passes through the elements of an array After each pass, one value has been moved to its correct, final position The sorted portion of the array grows as the unsorted portion shrinks Programming with Visual C++

Sorting Strategies (continued) Programming with Visual C++

Programming with Visual C++ The Selection Sort Start with an unsorted array Find the smallest value in the unsorted portion of the array Swap the smallest value with the first value in the unsorted portion of the array Repeat the process until all of the values in the array have been processed Programming with Visual C++

The Selection Sort (continued) Programming with Visual C++

The Selection Sort (continued) The outer loop executes n-1 times Assign the location of the first element in the unsorted portion of the list to smallIndex Find the location of the smallest element in the unsorted array Swap the smallest element with the first unsorted value Programming with Visual C++

The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value Variables needed for the selection sort Programming with Visual C++

Programming with Visual C++ The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value (continued) The most important variable is smallIndex It stores the location (index value) of the smallest value identified up to that point smallIndex starts out set to 0, meaning that the smallest element so far is in array element [0] If another array element contains a value smaller than element [0], then smallIndex is set to the location of that element Programming with Visual C++

Programming with Visual C++ The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value (continued) The inner loop accesses each element in the unsorted portion of the array If the value stored in an element is less than the value stored in element [smallIndex], then assign the subscript to smallIndex Programming with Visual C++

Programming with Visual C++ The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value (continued) Programming with Visual C++

Programming with Visual C++ The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value (continued) When the inner loop is finished: Swap the following two values The value stored in the first element in the unsorted portion of the array The value stored in element [smallIndex] Programming with Visual C++

Programming with Visual C++ The Selection Sort’s Inner Loop: Locating the Smallest Unsorted Value (continued) Programming with Visual C++

Programming with Visual C++ The Bubble Sort This strategy uses multiple swaps within the inner loop It is analogous to the effervescence of bubbles in a glass Bubbling action seems to rise from the bottom to the top Data value exchanges decrease from the bottom of the array up as the sorting process executes Programming with Visual C++

The Bubble Sort’s Inner Loop: Exchanging Values in Adjacent Elements The outer loop executes n-1 times The inner loop compares the values stored in each adjacent pair of elements in the unsorted portion of the array If the value in the first element of the pair is greater than the value stored in the second element of the pair, then the two values are swapped Programming with Visual C++

Programming with Visual C++ The Bubble Sort’s Inner Loop: Exchanging Values in Adjacent Elements (continued) Programming with Visual C++

Programming with Visual C++ The Bubble Sort’s Inner Loop: Exchanging Values in Adjacent Elements (continued) Programming with Visual C++

Programming with Visual C++ The Bubble Sort’s Inner Loop: Exchanging Values in Adjacent Elements (continued) Bubble sort code Programming with Visual C++

Programming with Visual C++ The Insertion Sort Analogous to the method used to arrange cards in a hand Strategy: sort from left to right Look at the first value in the unsorted portion of the array Compare it to all values in the sorted portion of the array Insert the value into its correct position Programming with Visual C++

The Insertion Sort (continued) Programming with Visual C++

The Insertion Sort (continued) Algorithm for the insertion sort of cards Sorting moves from left to right Programming with Visual C++

Programming with Visual C++

The Insertion Sort (continued) Programming with Visual C++

Programming with Visual C++

Programming with Visual C++

The Insertion Sort (continued) Programming with Visual C++

The Insertion Sort (continued) Insertion sort algorithm Programming with Visual C++

The Insertion Sort’s Inner Loop: Shifting Data Through Reassignment The outer loop uses loop control variable i Select the first value in the unsorted portion of the array Temporarily “remove it from the list” by making a copy Programming with Visual C++

Programming with Visual C++ The Insertion Sort’s Inner Loop: Shifting Data Through Reassignment (continued) Array element from which the unsorted item was “removed” into temp can now be considered vacant Programming with Visual C++

Programming with Visual C++ The Insertion Sort’s Inner Loop: Shifting Data Through Reassignment (continued) The inner loop Start with the last element in the sorted portion of the array If the value in the sorted portion is greater than the one “removed from the list” (temp): Then assign the value to the next highest element in the array (slide it down the array, opening up a vacancy) Programming with Visual C++

Programming with Visual C++ The Insertion Sort’s Inner Loop: Shifting Data Through Reassignment (continued) The inner loop If the value in the sorted portion is NOT greater than the one “removed from the list” (temp) : Then assign the value to the vacancy data[vacant] = temp; Programming with Visual C++

Programming with Visual C++ The Insertion Sort’s Inner Loop: Shifting Data Through Reassignment (continued) Programming with Visual C++

Comparing Sorting Algorithms Worst-case scenario Selection sort Outer loop x inner loop is O(n2) comparisons Outer loop is O(n) exchanges Bubble sort Outer loop x inner loop is O(n2) exchanges Insertion sort Programming with Visual C++

Programming with Visual C++ Summary Data may be sorted in ascending or descending order Sorting frequently requires data value exchanges (swapping) Sorting strategies covered in this chapter Selection sort Bubble sort Insertion sort Programming with Visual C++

Programming with Visual C++ Summary (continued) The ascending order selection sort Searches for the smallest value in the unsorted portion of the array and swaps it with the first value The ascending order bubble sort Compares values stored in adjacent pairs of unsorted elements and swaps them if the first is larger than the second The ascending order insertion sort Compares each unsorted value to the sorted portion of the array and inserts it into position after sliding other elements down Programming with Visual C++