Gudam,

Please help me, regarding my ascending code.I've been doubt how to compare the two loop
which is the outer loop and inner loop.I cant get the the output which is ascending in
form...Please guide me this time because I'm just a beginner. Thank you in advance

Here is my code...

import java.util.*;
 public class Array
{
	
   private Scanner scan;
   private int num [];
  
  public Array()
  {
    scan=new Scanner(System.in);
    num =new int [5]; 	
  }	
  public void fillArray()
  {
     for(int i=0; i<num.length; i++)
     {
        System.out.print("Enter an integer: ");
        num[i]=scan.nextInt();
     }
     	
     	
  }


  public void Ascending()
  {
 
  	System.out.print("Ascending: ");
  	for(int i=0; i<num.length-1; i++)
  	{
 	  for(int j=0; j<num.length-1; j++)
 	  {
 	   
 	   
 	   if(num[i]>num[j+1])
 	   int temp[0];
 	   {
 	     temp=num[j];
 	     num[i]=num[j+1];
 	     num[j+1]=temp;
 	   }
 	  	
 	  }
 	}
    for(int a=0; a<num.length; a++)
    	System.out.print(num[a]+ " ");
  }
  


public static void main(String[] args)
  {
  	Array a=new Array();
  	a.fillArray();
    
  	System.out.print("");
  	a.Descending();		
  		 
  }

From the looks of it, you're trying to bubble sort. You'll want to do something like this:

import java.util.Scanner;


public class ArraySort {

	private int[] numbers = new int[5];
	
	public void fillArray() {
		for (int i = 0; i < numbers.length; i++) {
			System.out.print("Enter Integer: ");
			numbers[i] = new Scanner(System.in).nextInt();
		}
	}

	public void sortArray() {
		for (int i = 0; i < numbers.length; i++) {
			for (int j = numbers.length - 1; j > i; j--) {
				if (numbers[j-1] > numbers[j]) {
					int temp = numbers[j];
					numbers[j] = numbers[j - 1];
					numbers[j - 1] = temp;
				}
			}
		}
	}
	
	public void printArray() {
		System.out.println();
		System.out.print("Sorted: ");
		for (int number : numbers)
			System.out.print(number + " ");
		System.out.println();
	}
	
	public static void main(String[] args) {
		ArraySort as = new ArraySort();
		
		as.fillArray();		
		as.sortArray();
		as.printArray();
	}
}

Console:

Enter Integer: 6
Enter Integer: 7
Enter Integer: 1
Enter Integer: 2
Enter Integer: 5

Sorted: 1 2 5 6 7

whole for statements (loop) in public void Ascending() you can replace just with

acs Collections.sort(myArrayName);
desc Collections.sort(myArrayName, Collections.reverseOrder());

From the looks of it, you're trying to bubble sort. You'll want to do something like this:

import java.util.Scanner;


public class ArraySort {

	private int[] numbers = new int[5];
	
	public void fillArray() {
		for (int i = 0; i < numbers.length; i++) {
			System.out.print("Enter Integer: ");
			numbers[i] = new Scanner(System.in).nextInt();
		}
	}

	public void sortArray() {
		for (int i = 0; i < numbers.length; i++) {
			for (int j = numbers.length - 1; j > i; j--) {
				if (numbers[j-1] > numbers[j]) {
					int temp = numbers[j];
					numbers[j] = numbers[j - 1];
					numbers[j - 1] = temp;
				}
			}
		}
	}
	
	public void printArray() {
		System.out.println();
		System.out.print("Sorted: ");
		for (int number : numbers)
			System.out.print(number + " ");
		System.out.println();
	}
	
	public static void main(String[] args) {
		ArraySort as = new ArraySort();
		
		as.fillArray();		
		as.sortArray();
		as.printArray();
	}
}

Console:

Enter Integer: 6
Enter Integer: 7
Enter Integer: 1
Enter Integer: 2
Enter Integer: 5

Sorted: 1 2 5 6 7

thank you so much sir for helping me.

whole for statements (loop) in public void Ascending() you can replace just with

acs Collections.sort(myArrayName);
desc Collections.sort(myArrayName, Collections.reverseOrder());

thank you so much sir for helping me.

This question has already been answered. Start a new discussion instead.