All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Bubble Sort In Java Using Scanner

Last Updated : Mar 11, 2024

Bubble Sort In Java Using Scanner

In this article we will show you the solution of bubble sort in java using scanner, as usual firstly we imported package after to getting input from user imported scanner pack.

We defined main class with the same name of program, which contains static main method.

There we asking user to enter total count of array values, then requesting as much as count separate values. And using two dimensional for loop we sorting those values and displaying at last.

Step By Step Guide On Bubble Sort In Java Using Scanner :-

Here we need to specify scanner package separately for getting support of scanner then only you can get input from user.

We defined main class 'bubSort' with static main method, there we declared integer variables 'cnt,c,d,temp'.

To request input from user we defined scanner with input object 'inp', now you can appending that with 'nextInt' will help you to request total count.

When sets that into array square bracket provides way to gets input from user by for loop.

To sort through bubble concept we need to define two loops, then comparing integer with nearby value. Then we sorting by comparison and storing on array variable and printed result.

//Bubble Sort Example
package Sortings;
import java.util.Scanner;
class bubSort {
 public static void main(String []args) {
  int cnt, c, d, temp;
  Scanner inp = new Scanner(System.in);
  System.out.println("Enter Total count Of Array Values");
  cnt = inp.nextInt();
  int arr[] = new int[cnt];
  System.out.println("Enter " + cnt + " integers to sort");
  for (c = 0; c < cnt; c++)
        arr[c] = inp.nextInt();
  for (c = 0; c < ( cnt - 1 ); c++) {
   for (d = 0; d < cnt - c - 1; d++) {
    if (arr[d] > arr[d+1])
    {
     temp = arr[d];
     arr[d] = arr[d+1];
     arr[d+1] = temp;
    }
   }
  }
  System.out.println("Sorted Array Values");
  for (c = 0; c < cnt; c++)
        System.out.println(arr[c]);
 }}
  1. The ‘Package Sortings’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces. The util.Scanner widely supports to getting input from end user.
  2. The ‘public static void main(String[] args)’ is defined that's starting point from where compiler starts program execution.
  3. First we defined main class 'bubSort', then within public static void main() method. There declared integer variables 'cnt,c,d,temp', each one declared for separate purpose. Using Scanner we defined object input object 'inp' then printed line of text 'Enter Total count Of Array Values'. To instruct user to what input it needs from user.
  4. To getting user input you need to specify 'nextInt- next with datatype of value' with scanner object. That's why we declared 'inp.nextInt()' this line that give the total length of array and stored on variable 'cnt'.
  5. With that length you need to create integer type array variable 'arr'. Again we printed text like 'Enter 4 integers to sort', which is alerts the user to enter 4 different values.
  6. Then we defined two array values to compare with nearby values. If statement true, then sorting each other index values otherwise checking with next set of values.
  7. At last, we printing text 'Sorted Array Values' to indicates the result and printed by for loop iteration.
  8. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  9. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to do bubble sort array in java. To compile java program you need to tell the java compiler which program to compile through ‘javac bubSort.java’.

After successful compilation you need to execute ‘java bubSort‘line for gets the output.

Then you can see 'Enter Total count Of Array Values' text. User need to provide total count of array value for instance you gave '4'.

Next text 'Enter 4 integers to sort' will display, so you have to enter like '12 3 5 1' values.

Lastly you can get result as 'Sorted Array Values' with '1 3 5 12' sorted values of output that might be vary based on end user given inputs.

On eclipse just press of run button then you will get output inside terminal.

I hope this article on bubble sort in java using scanner helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪