1 Lab Session-XI CSIT121 Spring 2002 w Sorting the arrays (array application) w Bubble Sort w Structures and Their Usage w Passing Struct Variables to.

Slides:



Advertisements
Similar presentations
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.
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.
Structures Spring 2013Programming and Data Structure1.
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
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
1 Lab Session-XI CSIT121 Fall 2000 b Arrays and Their Usage b Finding the largest element b Lab Exercise 11-A b Searching for some value b Lab Exercise.
Insertion Sorting Lecture 21. Insertion Sort Start from element 2 of list location 1 –In first iteration: Compare element 1 with all of its elements to.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
1 Lab Session-XII CSIT121 Spring 2002 b Arrays and Their Usage b Finding the largest element b Lab Exercise 12-A b Lab Demo (12-B) b Practice Problems.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
1 Lab Session-IX CSIT121 Fall 2000 w Arrays and Their Usage w Examples and Lab Exercises w Passing Arrays to Functions w Examples and Exercises w Sorting.
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.
1 Lab Session-12 CSIT121 Fall 2004 Structures and Their Usage Passing Struct Variables to Functions.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
Chapter 19: Searching and Sorting Algorithms
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Searching and Sorting Chapter Sorting Arrays.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Advanced Data types and Sorting
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Array, Structure and Union
Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
12. Sorting Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Sorting Many computer applications.
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.
CSCI 51 Introduction to Programming March 12, 2009.
Sorting an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be.
3 – SIMPLE SORTING ALGORITHMS
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Sorting Sorting takes an unordered array and makes it an ordered one
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
CSCI 51 Introduction to Programming March 10, 2009.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Bubble sort. Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Chapter 9: Sorting and Searching Arrays
Searching and Sorting 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.
Introduction to Programming
Review for Final Exam.
Principles of Computing – UFCFA3-30-1
Bubble sort.
Principles of Computing – UFCFA3-30-1
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
Presentation transcript:

1 Lab Session-XI CSIT121 Spring 2002 w Sorting the arrays (array application) w Bubble Sort w Structures and Their Usage w Passing Struct Variables to Functions w Examples and Exercises

2 Sorting Arrays w Arrays may contain numbers that need to be sorted in ascending or descending order w Sorting is a special technique and many algorithms are proposed for sorting w Look at orting-demo.html for a demo

3 Bubble Sort w Sorting of arrays can be done with many techniques w Look at the example of SELECTION SORT w SELECTION SORT chooses the smallest (or largest) value in the array and moves it to one end. w By repeatedly picking up smallest (or largest) value out of the remaining values, selection sort decreases the search space one at a time w Another sorting technique is called bubble sort

4 Bubble Sort w In bubble sort, the smallest value pops up. That is why it is called bubble sort. w Bubble Sort Algorithm is quite simple w It performs as many passes as the number of elements in the array minus one w On each pass, adjacent elements are compared and swapped if the current value is found greater than next. w Due to it simplicity, bubble sort is also inefficient because it makes too many comparisons

5 Bubble Sort w Let us develop the bubble sort algorithm as nested for loops: w for (outer=0; outer<SIZE-1; outer++) w for (inner=0; inner<SIZE-1; inner++) w if (array[inner] > array[inner+1]) w swap values (array[inner]<==array[inner+1] w and array[inner+1]<==array[inner]) w Please write a swap function separately

6 Structures and Their Usage w Arrays can only store data items of same data type under a common name w If we want to store student’s name and SS# together under a common name, it is not possible with arrays w We need to group together data items that are logically related to each other but these are of different data types

7 Structures and Their Usage w Examples of logical groups w Passenger Name, Flight Number, Seat Number and Status w Book Name, Author, Year published, Catalogue Number, Status w Student Name, SS#, Current courses

8 Structures and Their Usage w C++ provides struct data type w Using struct data type, we can collect many data items with different data types together under a same name w For example, if we wish to implement flight reservation system, we will need something similar to the following:

9 Structures and Their Usage w Struct flight w { w int SNo; w char name[40]; w char aisle; w int seat; w char status; w }

10 Structures and Their Usage w We have defined a data type by the name flight. w We can now declare variables of this data type w Example: flight AA474; w we can use AA474 in our program now

11 Structures and Their Usage w The data items grouped together under flight data type are called members of the structure. w Accessing members is done via dot notation w For Example: w AA474.seat=24; AA474.aisle=‘F’; w AA474.status=‘K’; (K means OK)

12 Structures and Their Usage w The members can be used just like any other variables are used w Example: w switch(AA474.status) w { w case ‘K’:cout<<“Confirmed\n”; break; w case ‘W’:cout<<“Waiting\n”;break; w case ‘C’:cout<<“Canceled”; break; w default: cout<<“Error in flight information\n”; break; w }

13 Passing Struct Variables to Functions w Struct variables can be assigned as below w flight AA475; AA475=AA474; w We can pass struct variables as arguments to functions. See example prototypes w By Value void print_info(flight); w By Reference void change_info(flight&); w Better pass by reference and add const if you wish to protect it

14 Lab Exercise 11-1 w Let us sort an array using the bubble sort technique. w As the name suggests, the array will be sorted in ascending order with smallest value popping up to the beginning of the array. w int schedule[10]={474, 282, 120, 765, 780, 23, 232, 75, 24, 812};

15 Lab Exercise 11-2 (DEMO) w Define a struct time that can hold hours, minutes and seconds as well as a char flag for am or PM. w struct time w { int hours; w int minutes; w int seconds; w char ampm; w } w time now; w now.ampm=‘a’;

16 Lab Exercise 11-2 (DEMO) w Develop a program to w print standard time with AM/PM notation w print military time without AM/PM notation w For example, if we have stored 14 hours, 20 minutes and 30 seconds w Standard time prints 02:20:30 PM w Military time prints14:20:30