Question

Write pseudocode to selection sort an array of pairs: (p_0, v_0), . . . , (p_n-1,...

Write pseudocode to selection sort an array of pairs: (p_0, v_0), . . . , (p_n-1, v_n-1)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Let’s start with an array of pairs with elements containing ‘n’ elements be defined as follows:

(p_0,v_0), (p_1,v_1), …, (p_n-1, v_n-1)

Let us sort this array, in ascending order, on the basic of first member of each pair of an array element, i.e., p_i (0<=i<n) be the key according to which this array will be sorted. To break conflict in case when p_i is equal to p_j, then we will compare v_i and v_j to finding the shorter one. If that matches too, then the element which occur first is choosen.

The pseudocode for the selection sort is-

function selectionSort (Argument Array, Argument arrayLength)

            Initialize iterator, for the array, i to zero

            While i is lesser than arrayLength

                        Initialize iterator, for the array, j to i+1

                        Initialize minimum_element_index to i

                        While j is lesser than arrayLength

if Array[j].p is less than Array[minimum_element_index].p

                                                minimum_element_index = j

else if Array[j].p is equal to Array[minimum_element_index]. p

if Array[j].v is less than Array[minimum_element_index].v

                                                            minimum_element_index = j

                                                End if

                                    End else-if

                                    Increment j by 1

                        End While

                        Swap Array[i] with Array[minimum_element_index]

                        Increment i by 1

            End While

            Return the Array

End of function selectionSort

Image of the pseudoCode to emphasize on indentation:

In this code, two arguments are passed are-

  1. Array – contains the unsorted array of pair
  2. arrayLength – length of the above array

We first initialize the iterator ‘i’ to zero(0). After this, a while loop is ran across the whole array using the iterator ‘i’ i.e. up to ‘arrayLength’.

Within this loop another iterator ‘j’ is initialized to ‘i+1’ and a variable ‘minimum_element_index’ to ‘i’. One more loop is used which is ran across the whole array until ‘j’ is lesser ‘than arrayLength’.

In this loop, we check if the pair (p_j,v_j) is lesser than (p_ minimum_element_index, v_ minimum_element_index) or not. To do this, first we check if p_i is lesser than p_ minimum_element_index or not, if true than ‘j’ is assigned to ‘minimum_element_index‘, else we check if v_j is lesser than v_ minimum_element_index or not, if true than ‘j’ is assigned to ‘minimum_element_index‘. Once the loop condition is evaluated to false (i.e. j < arrayLength), loop is terminated. Finally, ‘j’ is incremented by one(‘1’).

After the inner loop is executed, we swap the pair (p_i,v_i) with (p_ minimum_element_index, v_ minimum_element_index). Finally ‘i’ is incremented by one(‘1’). Once the loop condition is evaluated to false (i.e. i < arrayLength), loop is terminated.

Finally the sorted array ‘Array’ is returned and function ends.

Note: Array[i].p is equivalent to p_i part of pair (p_i, v_i) while Array[i] represents the pair (p_i, v_i).

Add a comment
Know the answer?
Add Answer to:
Write pseudocode to selection sort an array of pairs: (p_0, v_0), . . . , (p_n-1,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • TO DO: IMPLEMENT SELECTION SORT, BUBBLE SORT, MERGE SORT INSTRUCTIONS: GENERATE AN ARRAY arr AND FILL...

    TO DO: IMPLEMENT SELECTION SORT, BUBBLE SORT, MERGE SORT INSTRUCTIONS: GENERATE AN ARRAY arr AND FILL IT WITH 100 RANDOM INTEGERS, HAVING VALUES 0-99. PRINT THE UNSORTED ARRAY IMPLEMENT EACH SORTING ALGORITHM (DO NOT USE THE BUILT-IN LIBRARIES) FOR EACH ALGORITHM, INCLUDE PSEUDOCODE WITH NUMBERED STEPS. IN YOUR CODE, CLEARLY COMMENT WHICH STEP IS BEING PERFORMED BY THE LINE OR BLOCK OF CODE. USE A TIMER TO CHECK HOW LONG EACH ALGORITHM TAKES TO SORT THE ARRAY. THIS SHOULD BE...

  • Write a program that uses the selection sort algorithm to sort an array of coins by...

    Write a program that uses the selection sort algorithm to sort an array of coins by their value. Name your class SelectionSorter.java and include the method public static void sort(Coin[] a). The following classes are already included: Main.java and Coin.java and do not need to be downloaded and zipped into submission. Console [15, 5, 1, 100, 25, 50] [1, 5, 15, 25, 50, 100] Input Files Coin.java //package Sorting1; /** A coin. */ public class Coin { private int value;...

  • Transfer C code of selection sort to MIPS code and print the sorted array/results data Array:...

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • Transfer C code of selection sort to MIPS code and print the sorted array/results

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • Selection Sort is a common algorithm to sort the elements of an array. First, it scans...

    Selection Sort is a common algorithm to sort the elements of an array. First, it scans the elements of the array to find the smallest value and places it at index 0, thus creating a sorted “subarray” of size 1 that contains the smallest value. Then it scans the remaining unsorted values for the new smallest value and places it at index 1, creating a sorted subarray of size 2 that contains the 2 smallest values. It continues in this...

  • 1. Compare and contrast: SORTING -a bubble sort through an array -a selection sort through an...

    1. Compare and contrast: SORTING -a bubble sort through an array -a selection sort through an array Explain how each works and what the advantages and disadvantages are. Note the efficiency of each. 2. Compare and contrast: SEARCHING -a sequential search through a file -a sequential search through an array -a binary search through an array Explain how each works and what the advantages and disadvantages are. Note the efficiency of each.

  • Why is selection sort more efficient than the bubble sort on large array?

    Why is selection sort more efficient than the bubble sort on large array?

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • C++ Programing Consider the following sorting techniques: • Bubble Sort • Insertion Sort • Selection Sort...

    C++ Programing Consider the following sorting techniques: • Bubble Sort • Insertion Sort • Selection Sort • Merge Sort • Quick Sort Select any two of the above sorting techniques: 1. Write an algorithm (pseudocode) for the two selected sorting techniques 2. Write two separate C++ programs using user defined functions for each of the selected sorting techniques.

  • Write a program that meets the following criteria: Define a function that implements the selection sort...

    Write a program that meets the following criteria: Define a function that implements the selection sort algorithm to sort an integer array. Define a function that implements the binary search algorithm to search an array for a given integer. The function must return the location of the integer if it is found or a -1 if it is not found. The main function in your program must declare an integer array initialized with 10 unsorted values. The main function must...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT