How to sort an array in java?

0 votes
I was making an array with 10 elements which consist of random values. Now i want to sort them in order of lowest to highest value. How would I write a code for the same?
Apr 17, 2018 in Java by Daisy
• 8,050 points
84 views

3 answers to this question.

0 votes
int[] arr = new int[10];
Random r = new Random();
for (int i = 0; i < arr.length; i++)
    arr[i] = r.nextInt(100) + 1;
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
for (int i = arr.length - 1; i >= 0; i--)
    System.out.print(arr[i] + " ");
System.out.println();
answered Apr 17, 2018 by anonymous
0 votes
public void bubbleSort(int[] array) {
    boolean swapped = true;
    int j = 0;
    int tmp;
    while (swapped) {
        swapped = false;
        j++;
        for (int i = 0; i < array.length - j; i++) {
            if (array[i] > array[i + 1]) {
                tmp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = tmp;
                swapped = true;
            }
        }
    }
}
answered Jul 24, 2018 by Sushmita
• 6,880 points
0 votes
import java.util.Arrays;


public class Sort {
    public static void main(String args[])
    {
        int [] array = new int[10];
        for ( int i = 0 ; i < array.length ; i++ ) {
            array[i] = ((int)(Math.random()*100+1));
        }
        Arrays.sort( array );
        for ( int i = 0 ; i < array.length ; i++ ) {
            System.out.println(array[i]);
        }
    }
}
answered Aug 24, 2018 by parth295
• 4,640 points

Related Questions In Java

+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,150 points
74 views
0 votes
1 answer

How to reverse an int array in Java?

for(int i = 0; i < Data.length ...READ MORE

answered May 11, 2018 in Java by Akrati
• 3,150 points
55 views
0 votes
2 answers

How to sort an ArrayList of custom object by property in Java?

You can Sort using java 8 yourList.sort(Comparator.comparing(Classname::getName)); or  yourList.stream().forEach(a -> ...READ MORE

answered Aug 13, 2018 in Java by samarth295
• 2,190 points
164 views
0 votes
2 answers

How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System.o ...READ MORE

answered Aug 16 in Java by Sirajul
• 40,980 points
276 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
693 views
0 votes
2 answers

One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

answered Jul 26, 2018 in Java by samarth295
• 2,190 points
288 views
0 votes
2 answers

How to convert an array to arraylist?

In Java 9 you can use: List<String> list= List.of("Hello", "World", ...READ MORE

answered Aug 1, 2018 in Java by samarth295
• 2,190 points
54 views
0 votes
2 answers

How to concatenate two arrays in Java?

public <T> T[] concatenate(T[] a, T[] b) ...READ MORE

answered Jul 19, 2018 in Java by Sushmita
• 6,880 points
671 views
0 votes
3 answers

How can I add new elements to an Array in Java

String[] source = new String[] { "a", ...READ MORE

answered Sep 19, 2018 in Java by Sushmita
• 6,880 points
162 views
0 votes
3 answers

How to convert File to Byte[] in Java?

import java.io.File; import java.nio.file.Files; File file; // ...(file is initialised)... byte[] ...READ MORE

answered Aug 14, 2018 in Java by samarth295
• 2,190 points
100 views