Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

first part def selectionSort( theSeq ): n = len( theSeq ) for i in range( n - 1 ): # Assume the ith element is

image text in transcribed

first part

def selectionSort( theSeq ): n = len( theSeq ) for i in range( n - 1 ):

 # Assume the ith element is the smallest. 

smallNdx = i

 # Determine if any other element contains a smaller value. 

for j in range( i + 1, n ): if theSeq[j]

smallNdx = j

# Swap the ith value and smallNdx value only if the smallest value is # not already in its proper position. Some implementations omit testing # the condition and always swap the two values. if smallNdx != i :

tmp = theSeq[i] theSeq[i] = theSeq[smallNdx] theSeq[smallNdx] = tmp 

# theSeq[i], theSeq[smallNdx] = theSeq[smallNdx], theSeq[i]

second part

# Sorts a sequence in ascending order using the insertion sort algorithm.

def insertionSort( theSeq ): n = len( theSeq ) # Starts with the first item as the only sorted entry. for i in range( 1, n ) : # Note that we start from index 1, not 0!

 # Save the value to be positioned. 

value = theSeq[i] # "Next value on top of the card deck" # Find the position where value fits in the ordered part of the list. pos = i while pos > 0 and value

 # Shift the items to the right during the search. 

theSeq[pos] = theSeq[pos - 1] pos -= 1

 # Put the saved value into the open slot. 
 theSeq[pos] = value 
Test the implementation of the selectionSort() function in Listing 5.6 (page 137) in your book. Demonstrate with several examples that the function behaves as expected. Question 4 Test the implementation of the insertion Sort() function in Listing 5.7 (page 140) in your book. Demonstrate with several examples that the function behaves as expected

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

Practical Database Programming With Visual C# .NET

Authors: Ying Bai

1st Edition

0470467274, 978-0470467275

More Books

Students also viewed these Databases questions

Question

What are three factors that can affect your encoding skills?

Answered: 1 week ago