Java Program - Example #1(Bubble Sort)

Java Program - Example #1(Bubble Sort)

 


Question:


1. Write a full Java program that asks the user to type in 5 words, prompting the user for each word with a number. The program then reports the longest and the shortest word the user typed in.Write a full Java program that asks the user to type in 5 words, prompting the user for each word with a number. The program then reports the longest and the shortest word the user typed in.

2.Write a Java GUI Program that will store student ID, names and gpa in arrays, Then ask the user to enter a student ID, then search and display the student Info if it is found in the arrays, display an error otherwise

3.Write a Java Program that ask the user to enter the size of an integer array then all the elements of the array, then apply the Bubble sort algorithm, the Program should display the array before and after the sort.

 

 

 


Answer:


//1


import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Vamsi
 */
public class words {
   
    //1
    public static void main(String argv[])
    {
       
       int n=5;
        //declaring scanner to read
        Scanner sc = new Scanner(System.in);///to read input
       
        System.out.print("Enter "+n+" strings with numbers: ");
       
        String []a =  new String[n];//declaring string of arrays
        int i=0;
        while(i<n)
        {
            a[i] = sc.next();//reading strings
            i++;
        }
       
        //finding longest and shortest
       
        int l=0,s=0;
        i=1;
        while(i<n)
        {
            if(a[i].length() <  a[s].length())
                s=i;//updating smallest
            if(a[l].length() <  a[i].length())
                l=i;//updating largest
           
            i++;
        }
        System.out.println("Largest :"+a[l]);
        System.out.println("Smallest :"+a[s]);
       
       
    }
}

output:

run:
Enter 5 strings with numbers: surya1
phane2
bha5
hello9
bhai3
Largest :surya1
Smallest :bha5
BUILD SUCCESSFUL (total time: 19 seconds)
 

//3

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Vamsi
 */
public class BubbleSort {
 
    //method to sort array
   static void bubblesort(int[] A)
 { 
  int n = A.length; 
  int temp = 0; 
  for(int i=0; i < n; i++) ///traversing array
  { 
   for(int j=1; j < (n-i); j++)
   { 
      
    if(A[j-1] > A[j])
    {  
       
     
     temp = A[j-1]; 
     A[j-1] = A[j]; 
     A[j] = temp; 
    
    } 
 
   } 
  } 
 
 }
 
 static void print(int[] A){
    
     for(int i=0; i < A.length; i++)
  { 
   System.out.print(A[i] + " "); 
  }
     System.out.println();
    
 }
   
   
 public static void main(String[] args)
 { 
  int arr[] ={8,3,9,4,1,2}; 
 
  System.out.println("---Array before Bubble Sort---");
  
     print(arr);
 
  bubblesort(arr);
  System.out.println("---Array after Bubble Sort---"); 
  
  print(arr);
 
 } 
  
}

output:

run:
---Array before Bubble Sort---
8 3 9 4 1 2
---Array after Bubble Sort---
1 2 3 4 8 9
BUILD SUCCESSFUL (total time: 10 seconds)
 

Previous Post
Next Post

post written by:

Related Posts

0 Comments: