# Lab Exercise 8b

Lab Exercise 8b
Purpose: Write and demonstrate a sorting algorithm to sort the elements in an array and [properly swap parallel array elements.
Instructions:
1. Download this modified version of the previous lab assignment, LabAssignment8b.cpp, and the same data file AirlineSafetyData2.dat.
2. Write a sort function that will take as parameters the five airline safety data arrays as a ‘string’ array, four ‘long long int’ arrays and an ‘int’ size parameter.
3. You may choose the type of sort you want to use, but if you accept the Challenge you must choose the Bubble Sort. The prototype for both sorts is:
void sortArrays( string [], long long int[], long long int[], long long int[], long long int[], int[] );
Note: Remember that when you swap elements in the array being sorted you also must swap the same elements in the parallel arrays to keep them synchronized.
4. The main program has a statement that will call the sort function and display the desired results.
Challenge: Use the Bubble Sort and add a feature to the sort that ends the sort algorithm early if the data is sorted before the algorithm is finished.
Work Products:
Submit your updated and properly indented/commented C++ program file (.cpp).
Be sure to fully test your program before turning it in.
If you run out if time turn in what you have.
Hints:
Add only the sort function. Don’t mess with anything in the main program unless there is an obvious error.
To help you debug the sort function I put a flag in the main program named displayData. You can set it to “true” to display the arrays both before and after the sort. If you change it be sure to set it back to “false” before you turn in your code.
The main program uses the recursive binary search function to verify the sorted data.
Challenge Hint:
Don’t overthink this. There is a very simple way to tell if the data is sorted before the algorithm finished. To discover this hand trace the algorithm through a short array that is already sorted and observe what happens.
Sample output: (Output provided by main program)
Enter airline safety data File name: File AirlineSafetyData2.dat open.
Enter airline safety data File name: AirlineSafetyData2.dat
File AirlineSafetyData2.dat open.
57 airlines read from data file.

Array synchronization test.
If the sort worked each item in all columns of the
following lists will be identical
——————————————————–
Aeroflot* 1197672318 6 1 88
Air_France 3004002661 6 2 337
Air_India* 869253552 4 1 158
Air_New_Zealand* 710174817 5 1 7
Aeroflot* 1197672318 6 1 88
American* 5228357340 17 3 416
China_Airlines 813216487 2 1 225
Delta_/_Northwest* 6525658894 24 2 51
Egyptair 557699891 4 1 14
Ethiopian_Airlines 488560643 5 2 92
Garuda_Indonesia 613356665 4 2 22
Gulf_Air 301379762 3 1 143
Kenya_Airways 277414794 2 2 283
Malaysia_Airlines 1039171244 3 2 537
Pakistan_International 348563137 10 2 46
Philippine_Airlines 413007158 2 1 1
SAS* 682971852 6 1 110
Singapore_Airlines 2376857805 2 1 83
TACA 259373346 1 1 3
TAM 1509195646 7 2 188
Philippine_Airlines 413007158 2 1 1
Turkish_Airlines 1946098294 8 2 84
United_/_Continental* 7139291291 14 2 109
US_Airways_/_America_West* 2455687887 11 2 23
——————————————————–
Aeroflot* 1197672318 6 1 88
Air_France 3004002661 6 2 337
Air_India* 869253552 4 1 158
Air_New_Zealand* 710174817 5 1 7
American* 5228357340 17 3 416
China_Airlines 813216487 2 1 225
Delta_/_Northwest* 6525658894 24 2 51
Egyptair 557699891 4 1 14
Ethiopian_Airlines 488560643 5 2 92
Garuda_Indonesia 613356665 4 2 22
Gulf_Air 301379762 3 1 143
Kenya_Airways 277414794 2 2 283
Malaysia_Airlines 1039171244 3 2 537
Pakistan_International 348563137 10 2 46
Philippine_Airlines 413007158 2 1 1
SAS* 682971852 6 1 110
Singapore_Airlines 2376857805 2 1 83
TACA 259373346 1 1 3
TAM 1509195646 7 2 188
Thai_Airways 1702802250 2 1 1
Turkish_Airlines 1946098294 8 2 84
United_/_Continental* 7139291291 14 2 109
US_Airways_/_America_West* 2455687887 11 2 23
——————————————————–

SeatKM array sort verification test:
Sort PASSED.

×