Question

In: Computer Science

Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Bubble sort, show work. Write...

Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Bubble sort, show work. Write the algorithm.

Solutions

Expert Solution

Algorithm:
-----------
begin BubbleSort(list)
   for all elements of list
      if list[i] > list[i+1]
         swap(list[i], list[i+1])
      end if
   end for
   return list
end BubbleSort

Sorting:
---------
Bubble sort
Original list is [33, 77, 22, 11, 34, 21, 88, 90, 42]
Iteration: 1
   > Swap 77 and 22, since they are not in correct order. Now, the list becomes [33, 22, 77, 11, 34, 21, 88, 90, 42]
   > Swap 77 and 11, since they are not in correct order. Now, the list becomes [33, 22, 11, 77, 34, 21, 88, 90, 42]
   > Swap 77 and 34, since they are not in correct order. Now, the list becomes [33, 22, 11, 34, 77, 21, 88, 90, 42]
   > Swap 77 and 21, since they are not in correct order. Now, the list becomes [33, 22, 11, 34, 21, 77, 88, 90, 42]
   > Swap 90 and 42, since they are not in correct order. Now, the list becomes [33, 22, 11, 34, 21, 77, 88, 42, 90]
   > 5 swaps happened in this iteration
   > List after iteration 1 is [33, 22, 11, 34, 21, 77, 88, 42, 90]

Iteration: 2
   > Swap 33 and 22, since they are not in correct order. Now, the list becomes [22, 33, 11, 34, 21, 77, 88, 42, 90]
   > Swap 33 and 11, since they are not in correct order. Now, the list becomes [22, 11, 33, 34, 21, 77, 88, 42, 90]
   > Swap 34 and 21, since they are not in correct order. Now, the list becomes [22, 11, 33, 21, 34, 77, 88, 42, 90]
   > Swap 88 and 42, since they are not in correct order. Now, the list becomes [22, 11, 33, 21, 34, 77, 42, 88, 90]
   > 4 swaps happened in this iteration
   > List after iteration 2 is [22, 11, 33, 21, 34, 77, 42, 88, 90]

Iteration: 3
   > Swap 22 and 11, since they are not in correct order. Now, the list becomes [11, 22, 33, 21, 34, 77, 42, 88, 90]
   > Swap 33 and 21, since they are not in correct order. Now, the list becomes [11, 22, 21, 33, 34, 77, 42, 88, 90]
   > Swap 77 and 42, since they are not in correct order. Now, the list becomes [11, 22, 21, 33, 34, 42, 77, 88, 90]
   > 3 swaps happened in this iteration
   > List after iteration 3 is [11, 22, 21, 33, 34, 42, 77, 88, 90]

Iteration: 4
   > Swap 22 and 21, since they are not in correct order. Now, the list becomes [11, 21, 22, 33, 34, 42, 77, 88, 90]
   > 1 swaps happened in this iteration
   > List after iteration 4 is [11, 21, 22, 33, 34, 42, 77, 88, 90]

Iteration: 5
   > 0 swaps happened in this iteration
   > List after iteration 5 is [11, 21, 22, 33, 34, 42, 77, 88, 90]

Sorted list is [11, 21, 22, 33, 34, 42, 77, 88, 90]

Related Solutions

Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm....
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm. show work
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Selection sort and shell sort....
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Selection sort and shell sort. Write the algorithms. show work. please
Bubble sort for 12, 2, 3, 21, 11, 10,8
Bubble sort for 12, 2, 3, 21, 11, 10,8
Bubble sort for 12, 2, 3, 21, 11, 10,8
Bubble sort for 12, 2, 3, 21, 11, 10,8
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
1. Insertion sort for 12, 2, 3, 21, 11, 10,8 2. Bubble sort for 12, 2,...
1. Insertion sort for 12, 2, 3, 21, 11, 10,8 2. Bubble sort for 12, 2, 3, 21, 11, 10,8 3. selection sort for 12, 2, 3, 21, 11, 10,8 analysis of algorithm
For the given array, simulate the working operation of Bubble Sort. Show your work at each...
For the given array, simulate the working operation of Bubble Sort. Show your work at each step. Make sure to show the status of the array after every swap. [ 28, 13, 22, 7, 34, 2 ]
Analysis of Algorithims Bubble sort for 12, 2, 3, 21, 11, 10,8 Binary search for K=12...
Analysis of Algorithims Bubble sort for 12, 2, 3, 21, 11, 10,8 Binary search for K=12 in the array A={2, 3, 5, 7,11,15, 16,18,19} selection sort for 12, 2, 3, 21, 11, 10,8
SHOW WORK Sort the given keys using Counting sort algorithm. Also write the algorithm.          5, 2,...
SHOW WORK Sort the given keys using Counting sort algorithm. Also write the algorithm.          5, 2, 3, 1, 0, 2, 1, 5, 0                                                                  SHOW WORK!
Implement the bubble sort in ML using pattern matching and local function definitions. Show that it...
Implement the bubble sort in ML using pattern matching and local function definitions. Show that it works with a few simple examples. A nice description of the bubble sort can be found on Wikipedia. A helpful function for the bubble sort is: (* General function that checks if an int list is sorted *) fun issorted [] = true   | issorted [x] = true | issorted (x::y::t) = x <= y andalso issorted(y::t); Test your solution and show that it...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT