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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
Garfield AP Computer Science
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.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
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 10.
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.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
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.
CHAPTER 11 Sorting.
Introduction to Computing Dr. Nadeem A Khan. Lecture 17.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
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.
Chapter 8 Arrays and Strings
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Chapter 7 - Visual Basic Schneider
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 8 Arrays and Strings
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Computer Science Searching & Sorting.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
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 C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
Sha Tin Methodist College F.4 Computer Studies Pascal Programming.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
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.
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.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
Arrays and others. Annoucement Today’s office hour move to Friday 1:00PM to 3:00PM Today’s office hour move to Friday 1:00PM to 3:00PM Today Today  Call.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CSIT 208, Section Instructor: P eter C hen Introduction to Programming with QBasic to Visual Basic Lecture 8.
Sorting Sorting takes an unordered array and makes it an ordered one
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
Two-Dimensional Arrays. Two-dimensional arrays variables store the contents of tables or matrices. Example: Dim arrTable(1 to 5, 1 to 5) As Integer first.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Searching and Sorting Algorithms
Data Structures I (CPCS-204)
Chapter 7 Arrays.
CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays
Chapter 8 Arrays Objectives
Shell Sort and Merge Sort
Chapter 8 Arrays Objectives
Decision Maths Unit 7 Sorting Algorithms 3. Shell Sort.
Chapter 7 - VB.Net by Schneider
Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 28

Last Assignment has been posted

Two Dimensional Arrays

► Declaration formats: Dim arrayname(m1 To n1, m2 To n2) As varType ReDim arrayname(m1 To n1, m2 To n2) As varType => Scope (Global/ Form-level/ Local) established in the same way as for 1-D arrays 2-D Arrays

► Declaration format examples: Private Dim rm(1 To 4, 1 To 4) As String Public Dim rm( ) As String Dim rm( ) As String ReDim rm(1 To 4, 1 To 4) ReDim rm(1 To 4, 3 To 4) As String 2-D Arrays

► Value assignment: Dim student(1 To 2, 1 To 3) as String Let student(1, 3) = “Aslam” Let student(2, 1) = “Bushra” 2-D Arrays

► Example 1: Program to store and access data from Table 7.9 Data is in the data file DISTANCE.TXT 2-D Arrays

Example 1 (Contd.) Dim rm(1 To 4, 1 To 4) As Single ‘General Declaration Sub Form_Load Dim row As Integer, col As Integer Open “DISTANCE.TXT” For Input As #1 For row=1 To 4 For col=1 To 4 Input #1, rm(row, col) Next col Next row Close #1 End Sub 2-D Arrays

Example 1 (Contd.) Sub Command1_Click( ) Dim row As Integer, col As Integer Let row=Val(Text1.Text) Let col = Val(Text2.Text) If (row>=1 And row =1 And col =1 And row =1 And col<=4) Then Call ShowMileage(rm(), row, col) Else MsgBox ”Range 1 to 4 only”,, “Error” End If Text1.SetFocus End Sub 2-D Arrays

Example 1 (Contd.) Sub ShowMileage (rm( ) As Single, row As Integer, _ col As Integer) col As Integer)Picture1.Cls Picture1.Print “The road mileage is:” rm(row, col) End Sub 2-D 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

► In each pass compare items at a gap and swap if out of order swap if out of order ► Halve the gap in the next passes if no swap in the current pass Initial gap: half the array length Int (array length/2) Final gap: adjacent neighbours Sorting: Shell Sort

► First Pass: gap=2 Pebbles Pebbles Pebbles Pebbles Barney BarneyBarney Barney Wilma Wilma Wilma Dino Fred Fred Fred Fred Dino Dino Dino Wilma Shell Sorting

► Second Pass: gap=2 Pebbles Dino Dino Dino Barney BarneyBarney Barney Dino Pebbles Pebbles Pebbles Fred Fred Fred Fred Wilma WilmaWilma Wilma Shell Sorting

► Third Pass: gap=2 Dino Dino Dino Dino Barney BarneyBarney Barney Pebbles Pebbles Pebbles Pebbles Fred Fred Fred Fred Wilma WilmaWilma Wilma Shell Sorting

► Fourth Pass: gap=1 Dino Barney Barney Barney Barney Barney DinoDino Dino Dino Pebbles Pebbles Pebbles Fred Fred Fred Fred Fred Pebbles Pebbles Wilma WilmaWilma Wilma Wilma Shell Sorting

► Fifth Pass: gap=1 Barney Barney Barney Barney Barney Dino DinoDino Dino Dino Fred Fred Fred Fred Fred Pebbles Pebbles Pebbles Pebbles Pebbles Wilma WilmaWilma Wilma Wilma Shell Sort is complete (next gap=0) Shell Sorting

► Number of comparisons in this example Bubble Sortvs Shell Sort => Bubble sort outperformed ► Shell outperforms on long lists (30 or more items) Sorting

► Sequential search  Search sequentially element by element from start ► Binary search  Repeated divide into halves retaining the half where the quary may lie Searching in a Sorted List

End