Pascal Programming
Task 18 Telephone Directory (Bubble Sort)
Bubble Sort
Part I
The algorithm for Bubble Sort in ascending order (1st version)
Repeat each pass
For a list of N elements, do
Compare each pair of
adjacent elements in the list
If the first element is
greater then that of the second element, then swap the values
until N passes
Bubble1.pas is the simplest version of Bubble Sort (1st version)(simplest version).
The following are the sample outputs the sample program bubble1.pas.
Sample Output 1 |
Sample Output 2 |
||
| <<
Bubble Sort 1 >> Enter 5 integers : Original list 4 5 1 3 2 Pass 1 Sorted List 1 2 3 4 5 |
<<
Bubble Sort 1 >> Enter 5 integers : Original list 1 2 3 4 5 Pass 1 Sorted List 1 2 3 4 5 |
The algorithm for Bubble Sort in ascending order (2nd version)
For each pass
Repeat
For each pair of
adjacent elements in the working list
Repeat
Swap
them if they are not in the proper order
until
the end of working list
until (N-1) passes have been completed
Bubble2.pas is the modified version of Bubble Sort (2nd version)(better version).
The following are the sample outputs the sample program bubble2.pas.
Sample Output 1 |
Sample Output 2 |
||
| <<
Bubble Sort 2 >> Enter 5 integers : Original list 4 5 1 3 2 Pass 1 Sorted List 1 2 3 4 5 |
<<
Bubble Sort 2 >> Enter 5 integers : Original list 1 2 3 4 5 Pass 1 Sorted List 1 2 3 4 5 |
The algorithm for Bubble Sort in ascending order (3rd version)
For each pass
Repeat
For each pair of
adjacent elements in the working list
Repeat
Swap
them if they are not in the proper order
until
the end of working list
until ((N-1) passes have been completed)
or
(no
swapping throughout the comparison of the working list)
This is the last modified version of Bubble Sort (3rd version)(best version).
The following are the sample outputs of the 3rd version of Bubble Sort.
Sample Output 1 |
Sample Output 2 |
||
| <<
Bubble Sort 3 >> Enter 5 integers : Original list 4 5 1 3 2 Pass 1 Sorted List 1 2 3 4 5 |
<<
Bubble Sort 3 >> Enter 5 integers : Original list 1 2 3 4 5 Pass 1 Sorted List 1 2 3 4 5
|
Part II
Using the 3rd version of Bubble Sort to sort the telephone directory of a class in alphabetic order.
The follwoing is the content of part of a telephone directory of a class.
4Aphone.dat |
Description |
||
| CHAN TAI MAN 852-10256822 POON BO BO 852-13256888 CHEUNG DO DO 852-36254170 |
Student's Name Telephone Number Student's Name Telphone Number Student's Name Telephone Number |
Below are the sample outputs of a Pascal program. In these outputs, the information following the colon are entered through the keyboard by the user. All other items are output from the program.
Sample Output |
|
| Class : 4A Telephone Directory in
alphabetic order Chan
Tai Man 852-10256822 |
4Aphone.dat (updated) |
|
| CHAN TAI MAN 852-10256822 CHEUNG DO DO 852-36254170 POON BO BO 852-13256888 |
Main Program
Sub-program (procedure)