Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. package com.timbuchalka;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.     private static Scanner scanner = new Scanner(System.in);
  8.  
  9.     public static void main(String[] args) {
  10.         // Create a program using arrays that sorts a list of integers in descending order.
  11.         // Descending order is highest value to lowest.
  12.         // In other words if the array had the values in it 106, 26, 81, 5, 15 your program should
  13.         // ultimately have an array with 106,81,26, 15, 5 in it.
  14.         // Set up the program so that the numbers to sort are read in from the keyboard.
  15.         // Implement the following methods - getIntegers, printArray, and sortIntegers
  16.         // getIntegers returns an array of entered integers from keyboard
  17.         // printArray prints out the contents of the array
  18.         // and sortIntegers should sort the array and return a new array containing the sorted numbers
  19.         // you will have to figure out how to copy the array elements from the passed array into a new
  20.         // array and sort them and return the new sorted array.
  21.  
  22.         int[] myIntegers = getIntegers(5);
  23.         int[] sorted = sortIntegers(myIntegers);
  24.         printArray(sorted);
  25.  
  26.     }
  27.  
  28.     public static int[] getIntegers(int capacity) {
  29.         int[] array = new int[capacity];
  30.         System.out.println("Enter " + capacity +" integer values:\r");
  31.         for(int i=0; i<array.length; i++) {
  32.             array[i] = scanner.nextInt();
  33.         }
  34.         return array;
  35.     }
  36.  
  37.     public static void printArray(int[] array) {
  38.         for(int i=0; i<array.length; i++) {
  39.             System.out.println("Element " + i + " contents " + array[i]);
  40.         }
  41.     }
  42.  
  43.     public static int[] sortIntegers(int[] array) {
  44. //        int[] sortedArray = new int[array.length];
  45. //        for(int i=0; i<array.length; i++) {
  46. //            sortedArray[i] = array[i];
  47. //        }
  48.         int[] sortedArray = Arrays.copyOf(array, array.length);
  49.  
  50.         boolean flag = true;
  51.         int temp;
  52.         while(flag) {
  53.             flag = false;
  54.             // element 0     160
  55.             // element 1     50
  56.             // element 2     40
  57.  
  58.             for(int i=0; i<sortedArray.length-1; i++) {
  59.                 if(sortedArray[i] < sortedArray[i+1]) {
  60.                     temp = sortedArray[i];
  61.                     sortedArray[i] = sortedArray[i+1];
  62.                     sortedArray[i+1] = temp;
  63.                     flag = true;
  64.                 }
  65.             }
  66.         }
  67.  
  68.         return sortedArray;
  69.     }
  70.  
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement