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