bubble sort program help

Hi, i am new to C++ programming. This is not my major subjects but, i need help. My professor gave me a program that is so hard for me to solve it. Can you please help me out? There are 2 parts. Thanks.


Part A)
In the bubble sort algorithm, smaller values gradually “bubble” their way upward to the top of the array like air bubbles rising in water, while the larger values sink to the bottom. The bubble sort makes several passes through the array. On each pass, successive pairs of elements are compared. If a pair is in increasing order (or the values are identical), we leave the values as they are. If a pair is in decreasing order, their values are swapped in the array. Write a program that sorts an array of 10 integers using bubble sort. Your program should output the unsorted array, the number of swap operation and the sorted array.
I have to use at least four functions in your program:
int bubleSortArray(int array[]);
void swap(int & value1, int & value2);
void printArray(int array[]);
void inputArray(int array[]);
The first function will receive an unsorted array, sort it, and return the number of swap operation done in the unsorted array. The second function will swap two position in the array. The following two function will print and input the arrays elements respectively.

Part B) – (Bubble Sort Modification)
This bubble sort algorithm is inefficient for large arrays. Make the following simple modifications to improve the performance of the bubble sort:
a) After the first pass, the largest number is guaranteed to be in the highest-numbered element of the array; after the second pass, the two highest numbers are “in place,” and so on. Instead of making nine comparisons on every pass, modify the bubble sort to make eight comparisons on the second pass, seven on the third pass, and so on.
b) The data in the array may already be in the proper order or near-proper order, so why make nine passes if fewer will suffice? Modify the sort to check at the end of each pass if any swaps have been made. If none have been made, then the data must already be in the proper order, so the program should terminate. If swaps have been made, then at least one more pass is needed.
Implement these modifications in the following function:
int modifiedBubleSortArray(int array[]);
What have you tried so far?
Have you searched for information on a Bubble sort on the web. There is a wealth of information out there.

Generally a bubble sort is made up of a pair of nested loops in which you will compare the position of the outer loop to the inner loop's position. Depending on your criteria, like being one being less than the other, you will preform a slot between the two position's values.

Assuming your code will be dealing with one array, swap will only concern itself with the two positions for what is know as Value1 and Value2.

the second part is scanning for the largest number and placing it at the top of the list. Since it is swapped to the first position. You will only need to start the scan a the second position for the next.

Again this is a nested loop with the first loop being used to indicate the starting position of the second one, and the position to swap to.

Last edited on
Topic archived. No new replies allowed.