简体   繁体   中英

not able to sort array by ascending order

I have just started to do programming ...I am trying to sort an array in ascending order.. but not getting desired result , please point where i am doing wrong..

public static void main(String[] args) {

    int count, temp;

    // User inputs the array size
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter number of elements you want in the array: ");
    count = scan.nextInt();

    int num[] = new int[count];
    System.out.println("Enter array elements:");
    for (int i = 0; i < count; i++) {
        num[i] = scan.nextInt();
    }
    scan.close();
    {
        int i = 0;
        while (i <= count) {
            for (int j = i + 1; j < count; j++) {
                if (num[i] > num[j]) {
                    temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                    i++;
                }
            }

        }
    }
    System.out.print("Array Elements in Ascending Order: ");
    for (int i = 0; i < count - 1; i++) {
        System.out.print(num[i] + ", ");
    }
    System.out.print(num[count - 1]);

}

you should increment the i outside the for loop or you can get rid of the while loop and use for loop too, you can find the code below :

import java.util.Scanner;
public class Ascending _Order 
{
    public static void main(String[] args) 
    {
        int n, temp;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter no. of elements you want in array:");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter all the elements:");
        for (int i = 0; i < n; i++) 
        {
            a[i] = s.nextInt();
        }
        for (int i = 0; i < n; i++) 
        {
            for (int j = i + 1; j < n; j++) 
            {
                if (a[i] > a[j]) 
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.print("Ascending Order:");
        for (int i = 0; i < n - 1; i++) 
        {
            System.out.print(a[i] + ",");
        }
        System.out.print(a[n - 1]);
    }
}

Output:

Enter no.of elements you want in array:5

Enter all the elements: 4 3 2 6 1

Ascending Order:1,2,3,4,6

or you can use the method sort :

import java.util.Scanner;
import java.util.Arrays;

public class Ascending_Order {
    public static void main(String[] args) {
        int n, temp;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter no. of elements you want in array:");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter all the elements:");
        for (int i = 0; i < n; i++) {
            a[i] = s.nextInt();
        }
        Arrays.sort(a);
        System.out.print("Ascending Order:");
        for (int i = 0; i < n - 1; i++) {
            System.out.print(a[i] + ",");
        }
        System.out.print(a[n - 1]);
    }
}

Output:

Enter no.of elements you want in array:5

Enter all the elements: 4 3 2 6 1

Ascending Order:1,2,3,4,6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM