Pages

Thursday, 25 August 2016

Chapter 7 Exercise 20, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*7.20 (Revise selection sort) In Section 7.11, you used selection sort to sort an array. The selection-sort method repeatedly finds the smallest number in the current array and swaps it with the first. Rewrite this program by finding the largest number and swapping it with the last. Write a test program that reads in ten double numbers, invokes the method, and displays the sorted numbers. 



import java.util.Scanner;
 
 
public class ProgrammingEx7_20 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  int[] numbers = new int[10];
 
  System.out.print("Enter ten numbers:");
 
  for (int i = 0; i < numbers.length; i++) {
   numbers[i] = input.nextInt();
  }
 
  System.out.print("The sorted numbers are:");
 
  selectionSort(numbers);
  for (int i = 0; i < numbers.length; i++) {
   System.out.print(numbers[i] + " ");
  }
 }
 
  
 public static void selectionSort(int[] list) {
  for (int i = list.length-1; i > 0; i--) {
   // Find the max in the list[list.length...1]
   int currentMax = list[i];
   int currentMaxIndex = i;
 
   for (int j = 0; j < i; j++) {
    if (currentMax < list[j]) {
     currentMax = list[j];
     currentMaxIndex = j;
    }
   }
 
   // Swap list[i] with list[currentMaxIndex] if necessary
   if (currentMaxIndex != i) {
    list[currentMaxIndex] = list[i];
    list[i] = currentMax;
   }
  }
 }
 
}

No comments :

Post a Comment