Hi, I've been trying to sort a list in ascending order for a problem in class, but my for loop isn't working.

import java.util.*;
public class AscendingOrder
	public static void main(String[]args)
                int min;
		Scanner input=new Scanner(System.in);
		list=new int[10];
		list1=new int[10];
		System.out.print("Enter 10 integers: ");
		for (int i=0; i<10; i++)

		for (int j=0; j<10; j++)
			for (int i=j; i<10; i++)
				if (list[i]<min)

		for (int j=0; j<10; j++)
			System.out.println(list1[j] + "");

Probably most of you know this, but I will explain the code above and my ideas in more detail in case it helps.

It is supposed to ask for a list of 10 integers which it will input into list[]. It will also create a new list to put in the sorted numbers later, which is list1[]. Then setting the min at a random high value (1000000), the for loops go through list[] and find the min, then input that into list1[] and repeat until it is filled. However, when I do this, I just get the same number (the min) repeated within list1[]. Is this because I need to remove that number from later on in the list, otherwise it keeps being treated as the min? And if so, could you give me some hints to do so?

Recommended Answers

All 4 Replies

I executed your code. you are getting all 0's in your output.
There's nothing to do with min in your program.

Your ascending logic is not correct. Simply swap the numbers if a value is greater than others.
Here's how you do this..

for (int j=0; j<10; j++){
				for (int i=j+1; i<10; i++)
				if (list[j]>list[i]){
					                int temp=list[i];

Now even there's no need of list1 .
Simply print list.

for (int j=0; j<10; j++)
			System.out.println(list[j] + "");
for (int i=j+1; i<10; i++)

I understand most of what you did besides this line. Why the

int i=j+1


because we dont want to compare the value with itself..

Oh, nevermind, yea I get it now. Thanks for the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.