Question

In: Computer Science

Reverse the following selection sort to sort the items from right to left by computing the...

Reverse the following selection sort to sort the items from right to left by computing the max instead of min in java. (just write the algorithm)

public class Selection{

public static void sort(Comparable[] a) {

// Sort a[] into increasing order.

int N = a.length;

// array length

for (int i = 0; i < N; i++) {

// Exchange a[i] with smallest entry in a[i+1...N).

int min = i;

// index of minimal entr.

for (int j = i+1; j < N; j++)

if (less(a[j], a[min])) min = j;

exch(a, i, min);

}

}

}

Solutions

Expert Solution

AS u already know the algorithm so simply here isthe code

for sorting in reverse direction.

class selection{

public static void main(String[] args)   {            

int arr[] ={121,230,71,89,12};

// main algo for reverse selection sort

int n=5;
// size of input
for(int i=0;i<n-1;i++)               
{
for(int j=i+1;j<n;j++)               
{
// here sign is changed as to calculate in opposite direction
if(arr[i]<arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}           
}

}
for(int i=0;i<n;i++)
System.out.print(arr[i]+" ");
}              
}



Related Solutions

A word that reads the same from left to right and right to left is a...
A word that reads the same from left to right and right to left is a palindrome. For example, "I", "noon" and "racecar" are palindromes. In the following, we consider palindromic integers. Note that the first digit of an integer cannot be 0. How many positive palindromic integers are 5-digit long and contain 7 or 8?
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the...
Sort the following set of numbers using bubble sort, insertion sort, and selection sort. Show the process step-by-step, and find the time complexity in Big-O notation for each method. For sorting, use ascending order. 49, 7, 60, 44, 18, 105
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
1. Explain the following. a. Compare the right and left ventricular pressures and right and left...
1. Explain the following. a. Compare the right and left ventricular pressures and right and left ventricular volumes. b. What will happen to cardiac output during exercise? Explain what physiological changes occur that cause this change in cardiac output?
Bubble and Selection Sort For this assignment, you are to consider bubble and selection sort. Both...
Bubble and Selection Sort For this assignment, you are to consider bubble and selection sort. Both are O(n^2) however it may be possible to classify one algorithm as being more efficient than the other. You are to discuss which algorithm you feel is the most efficient and in what cases it will be more efficient. Provide any relevant test cases and code to support your belief. Submit a pdf containing your findings and test results along with any relevant code...
Create a vector of 50 characters PRG from A to Z. Sort the list in reverse...
Create a vector of 50 characters PRG from A to Z. Sort the list in reverse order (from Z to A). Please use c++. Please use #include<iostream>. Please don't use the ASCII values.
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in a list and swapping the first value with it, then finding the second smallest value and swapping the second value with it, continuing until all the values are in order. Implement this algorithm, then determine its growth function, and hence the order of the algorithm. Find how many swaps are made. Use Java Code to create algorithm
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort...
Explain why selection sort can be viewed as a divide and conquer algorithm. Compare selection sort with insertion sort with respect to performance (consider worst case and best case running times).
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from...
A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from the product of two 2-digit numbers is 9,009 = 91 × 99. The largest palindrome made from the product of two 3-digit numbers is 906,609 = 913 × 993. The largest palindrome made from the product of two 4-digit numbers is 99,000,099 = 9,901 × 9,999. 1. Write a function IN JAVASCRIPT to find the largest palindrome made from the product of two 7-digit...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT