Answered step by step
Verified Expert Solution
Question
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
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 (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 I/ Move the smallest element in Ali +1.. nl to Ali by swaps for j ndown to i 1 do if Alj 1> Alj] then Swap Alj 1 and Alj return A Algorithm 5.20: The bubble-sort algorithm
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started