03 - Lab Exercise #3 - Sorting Algorithms
03 - Lab Exercise #3 - Sorting Algorithms
Laboratory Exercise #3
Sorting Algorithms
Objectives:
At the end of the exercise, the students should be able to:
• Convert the 3 simple sorting algorithms into their equivalent Java programs.
• Create another menu program with these algorithms as the selections
• Use Java methods in the program design.
Activity:
Write a program given the following steps:
1. Ask the user to enter the array size and enter the specified number of elements of that
array. Array size should be in a range of 5 to 15. Display error messages when invalid size
is encountered.
2. Display this menu to select what sorting algorithm to perform on the given array elements.
Sorting Algorithms
Menu
[ 1 ] Bubble Sort
[ 2 ] Selection Sort
[ 3 ] Insertion Sort
[ 4 ] Exit
3. If 4 is chosen, Step #4; if not one of the choices, display the corresponding error message.
If 1, 2 or, 3 is chosen, perform the sorting algorithms as expected displaying the array
elements for every iteration After displaying the sorted elements and you press any key to
continue, control should be back to the sorting algorithms menu. You can select another
option and perform the chosen sorting algorithm using the same array elements previously
entered. In other words, the same array elements will be used to perform the 3 sorting
algorithms. Sample output is shown for option 1 – Bubble Sort.
Laboratory Exercise #3 Page 1 of 3
PAMANTASAN NG LUNGSOD NG PASIG
COLLEGE OF COMPUTER STUDIES
Bubble Sort
Given Array Elements: 77 42 35 12 101 5
1. 42 35 12 77 5 101
2. 35 12 42 5 77 101
3. 12 35 5 42 77 101
4. 12 5 35 42 77 101
5. 5 12 35 42 77 101
The Sorted Array Elements: 5 12 35 42 77 101
Press any key to continue:
4. If option 4 - Exit is chosen, ask the user if the process will continue or not. If Yes, repeat
Step #1 and so on; if not, terminate the program. Be reminded that answer other than Y/N
should not be accepted, and no other operation should be done. Display “Error Messages”
once invalid answer is given.
Submission Requirements:
Students must adhere to the following requirements when submitting the laboratory
exercises.
1. Every program should follow this File Header requirement. The first lines of the program
to be submitted should include comments with the following information and format:
2. Naming Convention
a. Program Name or class name should be named following this notation:
IT2X_Group#_Lab# where X is your section and # is the group and laboratory
exercise #. For example, if you belong to BSIT-2A, group #1 and this is
laboratory exercise #2, the program name or class name is
IT2A_Group1_Lab2 For BSCS-2A class, use CS2A_Group1_Lab2.
Laboratory Exercise #3 Page 2 of 3
PAMANTASAN NG LUNGSOD NG PASIG
COLLEGE OF COMPUTER STUDIES
b. For the folder name: 2X-Group#-Lab# and following the example above, the
folder name is 2A-Group1-Lab2 and for BSCS-2A use 2CS-Group1-Lab2.
3. Test your program several times to make sure it is running correctly, and it is the
program you will submit. Only the source program needs to be submitted. Place it in
your folder, compress and, submit. The result should be
2A-Group1-Lab2.zip
4. Submit the zipped file using the “Submission Form – Data Structures” intended for your
class. Link is already sent/posted in your respective google classrooms and the same
form will be used for every program to be submitted. Only one submission per group is
required.
5. Two (2) laboratory schedules are allotted to develop this exercise. The submission date
will be announced in class.