Java Program to Sort an Given Array of Integers (Scan from console ) | Using For-Loop |Bubble sorting |StudyEcart | Sorting002

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package in.studyecart.sorting.array;

import java.util.Scanner;

public class NumArrayScan {

	public static void main(String[] args) {
		
		Scanner input=new Scanner(System.in);
		
		System.out.println(">> Array -Number -Sorting <<");
		
		System.out.println("\nPlease Enter size of array:");
		int asize=input.nextInt();
		int arr[]=new int[asize];
		System.out.println("Please Enter "+asize +" of Elements into array: ");
		//Scanning the numbers from user console area
		for(int i=0;i<arr.length;i++) {
			arr[i]=input.nextInt();
		}
	
	       
	       int temp;
	       for(int i=0;i<arr.length;i++) {
	    	   
	    	   for(int j=1;j<arr.length-i;j++) {
	    		   
	    		   //if(arr[j-1]<arr[j]) { //Descending Order
	    		   if(arr[j-1]>arr[j]) { //Ascending Order
	    			   temp=arr[j-1];
	    			   arr[j-1]=arr[j];
	    			   arr[j]=temp;
	    			   
	    		   }
	    	   }
	    	  
	    	  
	       }
	       
	       System.out.println("\nAfter Sorting arr[]:");
	       for(int i=0;i<arr.length;i++) {
	    	   System.out.println(arr[i]);
	       }
		   
			
			
		}

	}

Post a Comment

0 Comments

Top Post Ad

Bottom Post Ad