lab 6: searching and sortingEN47/COMP9, Fall 2009 the problemIn this lab, you will write a program to animate searching in and sorting of
an array. The user will enter 10 integers between 0 and 20 in a random order.
Your program should sort the integers into ascending order using the bubble
sort algorithm and print the sorted array. You should also enable the user
to search for a particular number in the array. Your program should use
linear search and report the position in the array where the number was
found (the array index). As in Lab 5, you will use LEDA to
animate your algorithms. You should visualize your array as a histogram
(bar chart) in which the height of each bar corresponds to the value of an
element in the array.
part 1: bubble sortFor the first part of this assignment, your program interaction should look something like below:
This program will first sort an array of 10 integers and then allow
the user to search for values within the array.
Please enter 10 integers between 0 and 20, followed by RETURN:
4 2 7 10 5 9 5 8 1 2
Sorted data: [1 2 2 4 5 5 7 8 9 10 ]
Below is a generic outline of the bubble sort algorithm. Refer to your class notes for specific implementation details.
part 2: linear searchFor the second part of the assignment, you should enable the user to search for numbers in the sorted array. For example:
Enter a number to search for: 8
8 was found at position 7.
Search for another number? (y/n) y
Enter a number to search for: 6
6 was not found.
Search for another number? (y/n) n
Below is an outline of the linear search algorithm. Note that this is slightly different from (but equivalent to) the one in the class notes.
the programAs the focus of this lab is on the searching and sorting algorithms, we are providing some working code so that you will only have to implement the following two functions:
getting started
visualizationThere is one new LEDA function that you will use to do all the visualization for this lab: void draw_histogram(int A[], int size, int current) This function draws a histogram representing the values of the
integers in array testing & handing inTest your code with a variety of inputs. Try an array that is already
sorted and try an array that is sorted in reverse order. Test your search
function as well. Does it always find an answer, even if the target is the
first or last element in the array?
You can run the solution program ( provide comp9 lab6 bubbles.cpp extra creditAfter one iteration of the bubble sort algorithm, we know that the largest element will have bubbled its way to the last position in the array. Likewise, after the second iteration, we know that the second largest element will occupy the second to last position. In general, after every iteration, we can safely ignore another element in the array. For 10% extra credit, modify your bubble sort to be more efficient. Instead of sorting the entire array every time, change your algorithm so that it sorts one less element every iteration. For an additional 10% of extra credit, modify your bubble sort algorithm so
that it keeps track of the total number of comparisons and swaps necessary to
sort the array. Your
Total comparisons: 45
Total swaps: 24
Make any extra credit changes in a copy of your code:
When you are confident that your extra credit program
works correctly, submit it using provide comp9 lab6ec bubbles_extra.cpp |