Design a program to solve Chapter 9 Programming Exercise 2 (Sorted Names) on pag
ID: 3691585 • Letter: D
Question
Design a program to solve Chapter 9 Programming Exercise 2 (Sorted Names) on page 375 in your textbook with the following additional assumptions:
• The following module already exists:
// The swapS module accepts two String elements
// and swaps their contents
Module swapS(String ref a, String ref b)
Declare String temp
// Swap a and b
Set temp = a
Set a = b
Set b = temp
End Module
• The array of 20 names has already been populated with values.
Create a module that takes two parameters (the String array of names, and the number of items in the array of names) to solve the sorting requirements for (alphabetical ordering). Use the pseudocode format given in the textbook when implementing each of the following sort algorithms:
Sorted Names
Design a program that allows the user to enter 20 names into a String array. Sort
the array in ascending (alphabetical) order and display its contents.
For this problem I need a bubble sort Algorithm, a selection sort algorithm, and a insertion sort algorithm.
(only the modules, not the entire program.)
Explanation / Answer
Dear,
Bubble sort Module
// The maxString variable will contain the subscript
// of the last element in the array to compare.
Declare Integer maxString
// The count variable will be used as a counter
// in the inner For loop.
Declare Integer Count
// The outer loop positions maxString at the last
// element to compare during each pass through the
// array. Initially maxString is the index of the
// last element in the array. During each iteration,
// it is decreased by one.
//array index stars from 0 to 20, so last element will
// be at index 19
For maxString = 19 To 0 Step -1
// The inner loop steps through the array, comparing
// each element with its neighbor. All of the
// elements from index 0 through maxString are
// involved in the comparison. If two elements are
// out of order, they are swapped.
For i = 0 To maxString - 1
// Compare an element with its neighbor and swap
// as needed.
If arrayString[i] > arrayString[i + 1] Then
Call swapS(arrayString[i], arrayString[i + 1])
End If
End For
End For
End Module
// The swapS module accepts two String arguments
// and swaps their contents.
Module swapS(String ref a, String ref b)
// Local variable for temporary storage.
Declare String temp
// Swap the values in a and b.
Set temp = a
Set a = b
Set b = temp
End Module
Selection sort:
//The selectionSort module accepts an array of Strings
// and the array's size as arguments.
Module selectionSort(String Ref arrayString[], Integer arrSize)
// Integer i will hold the starting position of the array.
Declare Integer i
// j will hold the subscript of the element with
// the smallest value found in the array.
Declare Integer j
// min will hold the smallest value found in the
// scanned area.
Declare Integer min
// index is a counter variable used to hold a subscript.
Declare Integer index
// The outer loop iterates once for each element in the
// array, except the last element. The startScan variable
// marks the position where the scan should begin.
For i = 0 To arrSize - 2
// Assume the first element is the smallest value.
Set j = i
Set min = arrayString[i]
//starting at the 2nd element in the array to
look for the smallest value
For index = i + 1 To arrSize - 1
If arrayString[index] < min Then
Set min = arrayString[index]
Set j = index
End If
End For
// Swap the element with the smallest value
// with the first element.
Call swap(arrayString[j, arrayString[i])
End For
End Module
Module swapS(String ref a, String ref b)
// Local variable for temporary storage.
Declare String temp
// Swap the values in a and b.
Set temp = a
Set a = b
Set b = temp
End Module
Insertion Sort:
//The insertionSort module accepts an array of Strings
// and the array's size as arguments.
Module insertionSort(Integer Ref arrayString[], Integer Size)
// Loop counter
Declare Integer i
// Variable used to iterate through the array.
Declare Integer j
// Variable to hold the first unsorted value.
Declare Integer nsort
// Assume first array element is sorted
//The outer loop iterates starting at 1.
For i= 1 To Size - 1
// The first element outside the sorted subset is
// arrayString[i]. Store the value of this element
// in nsort.
Set nsort = arrayString[i]
// Start from first element of the sorted subset.
Set j = i
// Move the first string outside the sorted subset
While j> 0 AND arrayString[j-1] > nsort
Set arrayString[j] = arrayString[j-1]
Set j = j - 1
End While
// Insert the unsorted value in to array
// within the sorted subset.
Set arrayString[j] = nsort
End For
End Module
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.