| Course Home |
| Lab Home |
| Lab 1 |
| Lab 2 |
| Lab 3 |
| Lab 4 |
| Lab 5 |
| Lab 6 |
| Midterm |
| Lab 8 |
| Lab 9 |
| Lab 10 |
For this lab, download lab10.a2w to your Z-drive, or some other place on your hard drive. This is an incomplete file that you have to finish.
The first thing is to build a random list for the assigned values. There is an empty list declared in the file named movList. This list will hold the random amount for each guy to move backward. You job is to write code to fill this list with unique random numbers.
For example [2,4,0,5,1,3]. No repeating numbers.
Part of sorting a list requires the ability to swap the places of elements within a list. For example; [2,1,3] becomes [1,2,3] if we swap the positions of the first two elements. The swap method will take in two numbers representing the two elements in the list that you want to swap places. The final method will need to take in two element positions, a list of numbers and a list of objects. The method will swap the elements in the numbers list and then perform the same swap in the objects list. You job is to write code to complete the swap method already started in the supplied program.
For example: list = [2,4,0,5,1,3]; calling swap(1,2, list) will give list = [2,0,4,5,1,3].
Most examples of swap are shown using arrays.
For array A[]:
temp = A[i]
A[i] = A[i+1]
A[i+1] = temp
Alice has a bug in its array methods that does not allow setting an array element to a variable value. We are using lists with insert and remove instead of arrays with set value.
A sorting algorithm takes a list of numbers and sorts them into numerical order. Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which means the list is sorted.
Example: 5 1 4 2 8 - unsorted array; 1 4 2 5 8 - after one pass; 1 2 4 5 8 - sorted array
First pass at pseudo code for bubble sort;
Sort all the elements of a list
Loop through all the elements till sorted
Sort a single element
For a given element, if the next element is smaller swap places
Keep checking the element till it stops moving
Grab the next element and repeat
Second pass at pseudo code for bubble sort;
Boolean swapped = true // Need a variable to tell when the list is sorted.
bubbleSort Method;
while swapped // this will loop till all elements are sorted
set swapped to false // to stop loop if nothing gets swapped = list sorted
//sort a single element
for each element in the list
if the next element is smaller
have the guys on screen change positions // specific to this Alice problem
swap places of the elements in the list // call the swap method
set swapped to true to check again
end of if
end of for loop
end of while loop
end of method
| Last modified: | 8/19/09 |
| By: | David O'Gwynn |