Advertisement
Guest User

Sorting and Searching V1

a guest
Sep 20th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. package chapter7;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class SortingAndSearching {
  7.  
  8.     private static void print(int x[], int columnsize, int numPerLine) {
  9.         for (int i = 0; i < x.length; i++) {
  10.             System.out.printf("%" + columnsize + "d", x[i]);
  11.             if ((i + 1) % numPerLine == 0 || (i + 1) == x.length)
  12.                 System.out.println();
  13.         }
  14.     }
  15.  
  16.     private static int[]generateJunk(int count) {
  17.         int values[] = new int[count];
  18.         for (int i = 0; i < values.length; i++) {
  19.             values[i] = (int) (Math.random() * 100) + 1;
  20.         }  
  21.         return values;
  22.     }
  23.    
  24.  
  25.     private static int[] copyArray(int values[]) {
  26.         int duplicate[] = new int[values.length];
  27.         for (int i = 0; i < values.length; i++) {
  28.             duplicate[i] = values[i];
  29.         }
  30.         return duplicate;
  31.     }
  32.    
  33.     public static void main(String[] args) {
  34.  
  35.         int values[] = generateJunk(100);
  36.         System.out.println("Random Junk!");
  37.         print(values, 5, 10);
  38.         System.out.println();
  39.  
  40.  
  41.         // linear search the junk
  42.         Scanner input = new Scanner(System.in);
  43.         int key;
  44.         do {
  45.             System.out.print("Enter key: ");
  46.             key = input.nextInt();
  47.             int index = LinearSearch.linearSearch(values, key);
  48.             if (index < 0) {
  49.                 System.out.println("Not in Array");
  50.             } else {
  51.                 System.out.println(key + " found in position " + index);
  52.             }
  53.         } while (key >= 0);
  54.        
  55.         // Duplicate the junk
  56.         int duplicate[] = copyArray(values);
  57.  
  58.  
  59.         // Sort the Duplicate Junk
  60.         SelectionSort.selectionSort(duplicate);
  61.         System.out.println("Sorted Junk!");
  62.         print(duplicate, 5, 10);
  63.         System.out.println();
  64.  
  65.         // Now Binary Search the dups array
  66.         do {
  67.             System.out.print("Enter key: ");
  68.             key = input.nextInt();
  69.             int index = Arrays.binarySearch(duplicate, key);
  70.             System.out.println(index);
  71.             if (index < 0) {
  72.                 System.out.println("Not in Array, but key could be placed in position " + (-index - 1));
  73.             } else {
  74.                 System.out.println(key + " found in position " + index);
  75.             }
  76.         } while (key >= 0);
  77.        
  78.         input.close();
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement