2016-05-12 16 views
-1

Ich bin ziemlich neu in Java und ich habe einige Probleme mit diesem Projekt für meine Klasse.Wie würde ich diese beiden Programme kombinieren?

Grundsätzlich muss ich schreiben "Ein Java-Programm, um den Wert 45.3 aus dieser Liste = {- 3,10,5,24,45.3,10.5} mit der binären Suchmethode zu finden."

Und mein Code für das heißt hier:

public class BinarySearch 
{ 
    public static final int NOT_FOUND = -1; 
    public static int binarySearch(Integer[] a, int x) 
    { 
     int low=0; 
     int high = a.length - 1; 
     int mid; 
     while (low <= high) 
     { 
      mid = (low + high)/2; 
      if (a[mid].compareTo(x)<0) 
       low = mid + 1; 
      else if (a[mid].compareTo(x) > 0) 
       high = mid - 1; 
      else 
       return mid; 
     } 
      return NOT_FOUND; 
    } 

    public static void main(String[] args) 
    { 
     int x = (453/10); 
     int y = (105/10); 
     int SIZE = 6; 
     Integer [] a = {-3, 10, 5, 24, x, y}; 
     System.out.println("45.3 found at " +binarySearch(a, x)); 
    } 
} 

Jedoch habe ich erkannt, dass es nicht so habe ich ein einfaches BubbleSort sortiert wurde, die ich schon hatte und steckte die Zahlen hier:

class BubbleSort 
{ 
public static void main(String args[]) 
{ 

    int x = (453/10); 
    int y = (105/10); 
    int a[] = {-3, 10, 5, 24, x, y}; 
    int b = a.length; 
    int c, d, e; 
    System.out.print("Original Order : "); 
    for (c = 0; c < b; c++) 
    { 
     System.out.print(" " + a[c]); 
    } 

    System.out.println("\n"); 
    System.out.print("Ascending Order : "); 
    for (d=1; d < b; d++) 
    { 
     for (c=0; c < b-d; c++) 
     { 
      if (a[c] > a[c+1]) 
      { 
       int f = a[c]; 
       a[c] = a[c+1]; 
       a[c+1] = f; 
      } 
     } 
    } 
    for(c = 0; c < b; c++) 
    { 
     System.out.print(" " + a[c]); 
    } 
} 
} 

Aber an dem Punkt, an dem ich in dieser Klasse bin, habe ich keine Ahnung, wie man die Klassendateien irgendwie zusammenarbeitet oder alles in eine einzige .java oder .class Datei legt.

Irgendwelche Tipps?

Danke!

+0

'Integer' kann nicht' 45.3' und '10.5' gespeichert werden. Die Verwendung von 'BigDecimal' ist besser, da die Genauigkeit nicht verloren geht. – MikeCAT

Antwort

0
public class Search { 
    public final static int NOT_FOUND = -1; 

    public static double[] bubbleSort(double[] a) { 
     int length = a.length; 
     System.out.print("Original Order : "); 
     for (int i = 0; i < length; i++) { 
      System.out.print(" " + a[i]); 
     } 
     System.out.println("\n"); 
     System.out.print("Ascending Order : "); 
     for (int i = 1; i < length; i++) { 
      for (int j = 0; j < length - j; j++) { 
       if (a[j] > a[j + 1]) { 
        double f = a[j]; 
        a[j] = a[j + 1]; 
        a[j + 1] = f; 
       } 
      } 
     } 
     for (int i = 0; i < length; i++) { 
      System.out.print(" " + a[i]); 
     } 
     System.out.println(); 
     return a; 
    } 

    public static int binarySearch(double[] a, double x) { 
     int low = 0; 
     int high = a.length - 1; 
     int mid; 
     while (low <= high) { 
      mid = (low + high)/2; 
      if (a[mid] - x < 0) 
       low = mid + 1; 
      else if (a[mid] - x > 0) 
       high = mid - 1; 
      else { 
       return mid; 
      } 
     } 
     return NOT_FOUND; 
    } 

    public static void main(String[] args) { 
     double[] array = { -3, 10, 5.0, 24, 45.3, 10.5 }; 
     double[] sortedArray = bubbleSort(array); 
     System.out.println(binarySearch(sortedArray, 45.3)); 
    } 
} 
0

Beginnen Sie mit nur Kopieren & Einfügen des Programms zum Zusammenführen. Glücklicherweise gab es keine variablen Kollisionen.

public class BubbleSortAndBinarySearch 
{ 
    public static final int NOT_FOUND = -1; 
    public static int binarySearch(Integer[] a, int x) 
    { 
     int low=0; 
     int high = a.length - 1; 
     int mid; 
     while (low <= high) 
     { 
      mid = (low + high)/2; 
      if (a[mid].compareTo(x)<0) 
       low = mid + 1; 
      else if (a[mid].compareTo(x) > 0) 
       high = mid - 1; 
      else 
       return mid; 
     } 
      return NOT_FOUND; 
    } 

    public static void main(String[] args) 
    { 
     int x = (453/10); 
     int y = (105/10); 
     int SIZE = 6; 
     Integer [] a = {-3, 10, 5, 24, x, y}; 

     int b = a.length; 
     int c, d, e; 
     System.out.print("Original Order : "); 
     for (c = 0; c < b; c++) 
     { 
      System.out.print(" " + a[c]); 
     } 

     System.out.println("\n"); 
     System.out.print("Ascending Order : "); 
     for (d=1; d < b; d++) 
     { 
      for (c=0; c < b-d; c++) 
      { 
       if (a[c] > a[c+1]) 
       { 
        int f = a[c]; 
        a[c] = a[c+1]; 
        a[c+1] = f; 
       } 
      } 
     } 
     for(c = 0; c < b; c++) 
     { 
      System.out.print(" " + a[c]); 
     } 

     System.out.println(); // inserting this will make the result better to read 

     System.out.println("45.3 found at " +binarySearch(a, x)); 
    } 
} 

Dann ändern Sie das Programm, um nicht ganzzahlige Werte wie 45.3 und 10.5 richtig zu behandeln. Derzeit funktioniert dieser Code nur mit Ganzzahlen und der Wert 45.3 wird nicht verwendet, außer in der Zeichenkette.

Verwandte Themen