Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Algorithm SelectionSort(A[0..n-1]) //input:An array A[0..n-1] //output:Array A[0..n-1] sorted in ascending order for i

Algorithm SelectionSort(A[0..n-1])

//input:An array A[0..n-1]

//output:Array A[0..n-1] sorted in ascending order

for i <-- 0 to n - 2 do

min <--- i

for j <----- i +1 to n-1 do

if A[j] < A[min] min<--- j

swap A[i] and A[min]

  1. Trace this algorithm with the following array. Imagine that you have a print statement after the swap statement in above algorithm. Show the array after each execution of the swap statement. Show your work in detail and neatly.

5 4 7 3 1

  1. How many comparisons in total you made i.e. how many times the comparison statement A[i] < A[min] is executed?

  1. How many swaps in total you made i.e. how many times the swap statement swap A[i] and A[min] is executed?

  1. Is selection sort stable? Apply the algorithm in the array of 2 2 1 3 5 and justify your answer.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions