Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:03:04+00:00 2026-06-14T21:03:04+00:00

This is the instruction in one of the exercises in our Java class. Before

  • 0

This is the instruction in one of the exercises in our Java class. Before anything else, I would like to say that I ‘do my homework’ and I’m not just being lazy asking someone on Stack Overflow to answer this for me. This specific item has been my problem out of all the other exercises because I’ve been struggling to find the ‘perfect algorithm’ for this.

Write JAVA program that will input 10 integer values and display either in ascending or descending order. Note: Arrays.sort() is not allowed.

This is the code I have come up with, it works but it has one obvious flaw. If I enter the same value twice or more, for example:

5, 5, 5, 4, 6, 7, 3, 2, 8, 10

Only one of the three 5s entered would be counted and included in the output. The output I get (for the ascending order) is:

2 3 4 5 0 0 6 7 8 10.

import java.util.Scanner;

public class Exer3AscDesc
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        int tenNums[]=new int[10], orderedNums[]=new int[10];
        int greater;
        String choice;

        //get input
        System.out.println("Enter 10 integers : ");
        for (int i=0;i<tenNums.length;i++)
        {
            System.out.print(i+1+"=> ");
            tenNums[i] = scan.nextInt();
        }
        System.out.println();

        //imperfect number ordering algorithm
        for(int indexL=0;indexL<tenNums.length;indexL++)
        {
            greater=0;
            for(int indexR=0;indexR<tenNums.length;indexR++)
            {
                if(tenNums[indexL]>tenNums[indexR])
                {
                    greater++;
                }
            }
            orderedNums[greater]=tenNums[indexL];
        }

        //ask if ascending or descending
        System.out.print("Display order :\nA - Ascending\nD - Descending\nEnter your choice : ");
        choice = scan.next();

        //output the numbers based on choice
        if(choice.equalsIgnoreCase("a"))
        {
            for(greater=0;greater<orderedNums.length;greater++)
            {
                System.out.print(orderedNums[greater]+" ");
            }
        }
        else if(choice.equalsIgnoreCase("d"))
        {
            for(greater=9;greater>-1;greater--)
            {
                System.out.print(orderedNums[greater]+" ");
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:03:05+00:00Added an answer on June 14, 2026 at 9:03 pm

    You can find so many different sorting algorithms in internet, but if you want to fix your own solution you can do following changes in your code:

    Instead of:

     orderedNums[greater]=tenNums[indexL];
    

    you need to do this:

    while (orderedNums[greater] == tenNums[indexL]) {
         greater++;
    }
    orderedNums[greater] = tenNums[indexL];
    

    This code basically checks if that particular index is occupied by a similar number, then it will try to find next free index.

    Note: Since the default value in your sorted array elements is 0, you need to make sure 0 is not in your list. otherwise you need
    to initiate your sorted array with an especial number that you sure is
    not in your list e.g: Integer.MAX_VALUE

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would any one please explain this instruction for me: for (;;) I have encountered
I've got a regex that matches stuff like this: asdasd[text]; it works fine one
Does Java has a one line instruction to read to a text file, like
I'm trying to parse a processing instruction like this using StAX: <?item something=<some_element></some_element>?> StAX
I found this exam question that I have to tell how the beq instruction
in java, how can we execute an instruction only one time in a do
How can I make this code into a one line instruction? var qe :
In one article I have seen this instruction to convert a Maven project to
I've been seeing this instruction as the very first line of numerous CSS files
This is the instruction I use: BEGIN DBMS_SCHEDULER.RUN_JOB(25950); commit; END; And this is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.