Introduction to Computing Dr. Nadeem A Khan. Lecture 10.

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

Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set. 2.Compare the first two elements,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Visual C++ Programming: Concepts and Projects
Match-and-Stop Search Will find FIRST match Use Boolean variable to denote whether a match has been found or not Found initially False If a match is found,
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Introduction to Computing Dr. Nadeem A Khan. Lecture 23.
Introduction to Computing Dr. Nadeem A Khan. Lecture 22.
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
Introduction to Computing Dr. Nadeem A Khan. Lecture 27.
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Introduction to Arrays Chapter 7 Why use arrays? To store & represent lists of homogeneous values To simplify program code To eliminate the need to reread.
Introduction to Computing Dr. Nadeem A Khan. Lecture
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Introduction to Computing Dr. Nadeem A Khan. Lecture 17.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Introduction to Computing Dr. Nadeem A Khan. Lecture 28.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Introduction to Computing Dr. Nadeem A Khan. Lecture 18.
Introduction to Programming with C++ Fourth Edition
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which.
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
Chapter 7 - Visual Basic Schneider
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will –Learn about arrays One-dimensional arrays Two-dimensional arrays –Learn about searching.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
CSC220 Data Structure Winter
INFO Problem Solving and Programming Logic INFO Problem Solving and Programming Logic Arrays Sorting.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Chapter 6: Arrays: Lists and Tables
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
Decision Maths 1 Sorting Algorithm Shuttle Sort A V Ali : 1.Compare items 1 and 2; swap them if necessary 2.Compare 2 and 3; swap.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
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.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Sorting Sorting takes an unordered array and makes it an ordered one
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])
Bubble Sort Notes David Beard CIS220. Bubble Sort for Strings Double pass algorithm to sort a single dimensional array. Inner loop “bubbles” largest element.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
The Bubble Sort Mr. Dave Clausen La Cañada High School
Chapter 9: Sorting and Searching Arrays
Searching and Sorting Algorithms
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 7 Arrays.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Chapter 8 Arrays Objectives
Bubble Sort Key Revision Points.
Chapter 8 Arrays Objectives
Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 10

► Ordered array vsUnordered array E.g:  Ascending order: [each element]<=[next element] Using Arrays: Ordered Arrays

► Ordered array vsUnordered array => Advantage: Efficient Searching Using Arrays: Ordered Arrays

► Example: Request a name and inform if in the ordered list Using Arrays: Ordered Arrays

Dim nom(1 To 5) As String ‘General Declaration Sub Form_Load Rem Place the names in the array in ascending order Let nom(1) =“AKBAR” Let nom(2) =“ASLAM” Let nom(3) =“BUSHRA” Let nom(4) =“TONY” Let nom(5) =“ZAID” End Sub Using Arrays: Ordered Arrays

Sub Command1_Click Dim n As Integer, name2Find As String Let name2Find = Ucase(Trim(Text1.Text)) Let n=0 Do Let n=n+1 Loop Until (nom(n) >= name2Find) Or (n=5) If nom(n) =name2Find Then Picture1.Print “Found” Else Picture1.Print “Not found” End If End Sub

=> Average search was half the array dimension Using Arrays: Ordered Arrays

Sorting

► Sorting: Ordering an unordered array like the following: PebblesBarneyWilmaFredDino Sorting

► Compare adjacent items and swap if out of order ► In n-1 passes the array of n elements will get sorted =>How will you swap values stored in a pair of variables? Bubble Sorting Algorithm

► First Pass Pebbles Barney Barney Braney Barney Barney Pebbles Pebbles Pebbles Pebbles Wilma Wilma Wilma Fred Fred Fred Fred Fred Wilma Dino Dino Dino Dino DinoWilma Bubble Sorting

► Second Pass Barney Barney Barney Braney Pebbles Pebbles Fred Fred FredFred Pebbles Dino Dino Dino Dino Pebbles Wilma Wilma Wilma Wilma Bubble Sorting

► Third Pass Barney Barney Barney FredFred Dino DinoDino Fred Pebbles Pebbles Pebbles Wilma Wilma Wilma Bubble Sorting

► Fourth Pass Barney Barney DinoDino FredFred Pebbles Pebbles Wilma Wilma Bubble Sorting