1. Please use SelectionSort method we developed today in class to have a program
ID: 3679860 • Letter: 1
Question
1. Please use SelectionSort method we developed today in class to have a program to sort through an array of double variables. Use can use example on page 270 (Listing 7.8)to code this program. Please submit just your .java code.
Required Introduction to Java Programming, Comprehensive Version, 10th edition, Y. Daniel Liang, Prentice Hall, ISBN-13: 978-0-13-376131-3
Suggested: Oracle’s Java Documentation https://docs.oracle.com/javase/8/docs (Links to an external site.)
https://docs.oracle.com/javase/tutorial/java/index.html (Links to an external site.)
Explanation / Answer
public class MySelectionSort {
// selection sort method for double type array
public static void selectionSort(double[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
double smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
public static void main(String a[]){
double[] arr1 = {10,34,2,56,7,67,88,42};
selectionSort(arr1);
for(double i:arr1){
System.out.print(i);
System.out.print(", ");
}
}
}
/*
Output:
2.0, 7.0, 10.0, 34.0, 42.0, 56.0, 67.0, 88.0,
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.