Sort data Learn programming Java

Lesson:

Arrays, Structures and Strings


Exercise:

Sort data


Objetive:

Create a java program to ask the user for 10 integer numbers (from -1000 to 1000), sort them and display them sorted.


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int total = 9;
		int[] data = new int[total];
		int i, j, aux;

		for (i = 0; i < total; i++)
		{
			System.out.printf("Enter number %1$s: ", i + 1);
			data[i] = Integer.parseInt(new Scanner(System.in).nextLine());
		}

		for (i = 0; i < total - 1; i++)
		{
			for (j = i + 1; j < total; j++)
			{
				if (data[i] > data[j])
				{
					aux = data[i];
					data[i] = data[j];
					data[j] = aux;
				}
			}
		}
		System.out.print("Sorted:");
		for (int valor : data)
		{
			System.out.printf("%1$s ", valor);
		}
	}
}

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.