Skip to main content

Java Program to perform bubble sort and binary search on a 10 numbers' list

The following code is to perform the bubble sort and binary search on the same 10 numbers' list. As to perform binary search the list should be in sorted order, that's why before we perform binary search, bubble sort is done on the list.
Code:-

import java.util.*;
public class BubbleBinary {
    Scanner scan = new Scanner(System.in);
    int list[] = new int[10];
    int i = 0, j = 0, temp = 0, search;
    public void input() {
        try {
            System.out.println("Enter 10 number list: ");
            for (i = 0; i < 10; i++) {
                list[i] = scan.nextInt();
            }
            System.out.printf("Search Number: ");
            search = scan.nextInt();
        } catch (InputMismatchException | NumberFormatException | ArrayIndexOutOfBoundsException | NullPointerException e) {
            System.err.println("Error Occur\n" + e.getMessage());
            System.exit(0);
        }
    }
    public void bubble() {
        for (i = 0; i < 10; i++) {
            for (j = 0; j < 10 - i - 1; j++) {
                if (list[j + 1] < list[j]) {
                    temp = list[j + 1];
                    list[j + 1] = list[j];
                    list[j] = temp;
                }
            }
        }
        for (i = 0; i < 10; i++) {
            System.out.println(list[i]);
        }
    }
    public void binary() {
        int start;
        start = 0;
        int last = 9, med = 0;
        while (start < last) {
            med = (start + last) / 2;
            if (list[med] > search) {
                last = med - 1;
            } else if (list[med] < search) {
                start = med + 1;
            } else {
                System.out.println("Number Found At Index " + med + "\n");
                break;
            }
            if (start == last) {
                System.out.println("Number not Found In The List!");
            }
        }
    }
    public static void main(String[] args) {
        BubbleBinary bb = new BubbleBinary();
        bb.input();
        bb.bubble();
        bb.binary();
    }
}

Output:-
Enter 10 number list:
98
76
87
34
56
89
19
52
08
15
Search Number: 82
8
15
19
34
52
56
76
87
89
98
Number not Found In The List!

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Java Program to calculate the Strike Rate of a Cricket Batsman

import java.io.*; import java.util.*; public class StrikeRate{     Scanner scan=new Scanner(System.in);     int ballsFaced, runs;     double strikeRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Faced: ");             ballsFaced=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0); ...

WP similar or related posts widget without using any plugin

Hi guys, Recently I was working on some WP website and my client told me that he required a widget for displaying related/similar posts on the single post page. But as his website was already using many plugins, even for some pretty small tasks like this one, I decided not to use another WP plugin (plugins are not good for your WP websites, we will discuss about that on some other post.) I am not explaining the code as it is pretty simple if you are familiar with WP classes. But please let me know if you have any questions related to the PHP code posted below in the comments section or even much better, on Gist. You can add the following code directly in your child theme's functions.php file or you can create a separate file and include this at the bottom of functions.php file. <?php class similar_posts_widget extends WP_Widget {     function __construct()     {         parent::__construct('similar_posts_...