Consider a file of 9 elements with the keys ordered as ALGOR

Consider a file of 9 elements with the keys ordered as ALGORITHM. For each of selection sort and bubble sort, show the order of keys after each pass and also show the number of comparisons and the number of swaps performed in each pass.

Solution

Algorithm for BubbleSort:

function BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
return list

Example:   


Given list is 5 1 4 2 8
First Pass
5 1 4 2 8
1 5 4 2 8
1 4 5 2 8
1 4 2 5 8

Second Pass
1 4 2 5 8
1 4 2 5 8
1 2 4 5 8
1 2 4 5 8

Third Pass
1 2 4 5 8
1 2 4 5 8
1 2 4 5 8
1 2 4 5 8


Algorithm for selection Sort:
  
function selection_sort(list)
n = list.size()
for i = 1 to n - 1
min = i
for j = i+1 to n
if list[j] < list[min] then
min = j;
if indexMin != i then
swap(list[min], list[i])
  

Example:

Given list is 64 25 12 22 11

First Pass
11 64 25 12 22

Second Pass
11 12 64 25 22

Third Pass
11 12 22 64 25

Fourth Pass
11 12 22 25 64

 Consider a file of 9 elements with the keys ordered as ALGORITHM. For each of selection sort and bubble sort, show the order of keys after each pass and also s
 Consider a file of 9 elements with the keys ordered as ALGORITHM. For each of selection sort and bubble sort, show the order of keys after each pass and also s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site