Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

C-5.1 Consider a modification of the selection-sort algorithm where instead of finding the smallest element in the unsorted part of the array and swapping it

image text in transcribed

C-5.1 Consider a modification of the selection-sort algorithm where instead of finding the smallest element in the unsorted part of the array and swapping it to be in its proper place, as given in Algorithm 5.3, we instead perform a sequence of swaps to move the smallest element to its proper location, as shown in Algorithm 5.20 Show that this algorithm, which is known as bubble-sort, runs in e(n2) time. Algorithm BubbleSort(A): Input: An array A of n comparable elements, indexed from 1 to n Output: An ordering of A so that its elements are in nondecreasing order. fori1 to n 1 do // Move the smallest element in Ai1.. n to Ai by swaps for j n down toi+1 do if Alj -1> Aljl then Swap Aj 1 and A] return A Algorithm 5.20: The bubble-sort algorithm

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions