2016-05-12 2 views
-1

Я довольно новичок в java, и у меня возникают проблемы с этим проектом для моего класса.Как я могу объединить эти две программы?

В основном я должен написать «Программу Java, чтобы найти значение 45.3 из этого списка = {- 3,10,5,24,45.3,10.5}, используя метод двоичного поиска».

И мой код, который находится здесь:

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)); 
    } 
} 

Однако я понял, что он не был отсортирован, поэтому я использовал простой BubbleSort, что я уже имел и заткнул номера здесь:

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]); 
    } 
} 
} 

Но в тот момент, когда я нахожусь в этом классе, я понятия не имею, как либо заставить файлы классов работать вместе, либо помещать их все в один файл .java или .class.

Любые советы?

Спасибо!

+0

'Integer' не capabie для хранения' 45.3' и '10.5'. Использование 'BigDecimal' лучше, потому что оно не потеряет точность. – MikeCAT

ответ

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

Начните с только что скопированной & вставляя программу для слияния. К счастью, нет никаких переменных столкновений.

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)); 
    } 
} 

Затем измените программу, чтобы правильно обрабатывать нецелые значения, такие как 45.3 и 10.5. В настоящее время этот код работает только с целыми числами, а значение 45.3 не используется, кроме как в строке.

Смежные вопросы