Sorting an array can be done by using inbuilt sort function while for the insertion we have to create a new array to do so as arrays in Java are immutable. To learn more about sorting in Java follow the article mentioned below:
Sorting: Arrays.sort() in Java with examples
Approach 1:
- Create a new array of size N+1.
- Copy first array in New array.
- Insert number at the end of the array.
- Sort the array.
Example: Inserting an element and then sorting the array.
Java
// Java program to insert an element in// an array and then sorting it.Â
// Importing util filesimport java.util.*;Â
public class Gfg {Â
    // Main function    public static void main(String args[]) throws Exception    {Â
        // Given number        int given_number = 1;Â
        // Array        int array[] = { 6, 7, 8, 2, 3, 4, 5, 9, 10 };Â
        // Creating new array with increased size        int new_array[] = new int[array.length + 1];Â
        // Copying elements from one array to another        for (int i = 0; i < array.length; i++) {            new_array[i] = array[i];        }Â
        // Adding new element        new_array[new_array.length - 1] = given_number;Â
        // Sorting new array        Arrays.sort(new_array);Â
        // print array        for (int i = 0; i < new_array.length; i++)            System.out.print(new_array[i] + " ");    }} |
1 2 3 4 5 6 7 8 9 10
Time complexity: O(n log n)
Approach 2:Â
- Sort the array.
- Create a new array of size N+1.
- Start traversing the given array and copy elements.
- If the given number is less than equal to the number present in the array, append given a number to the new array.
- Copy remaining elements of the given array to the new array.
Example: Inserting an element and then sorting the array.Â
Java
// Java program to insert an element// in an array and then sorting it.Â
// Importing util filesimport java.util.*;Â
public class Gfg {Â
    // Main function    public static void main(String args[]) throws Exception    {Â
        // Given Number        int given_number = 1;Â
        // Array        int array[] = { 6, 7, 8, 2, 3, 4, 5, 9, 10 };Â
        // Sort Given array        Arrays.sort(array);Â
        // Creating new array with increased size        int new_array[] = new int[array.length + 1];Â
        // Copying elements from one        // array to another as in approach 2        int i = 0, j = 0;        for (i = 0; i < new_array.length; i++) {            if (given_number <= array[i]) {                new_array[i] = given_number;                break;            }            else                new_array[i] = array[j++];        }Â
        // copy the remaining elements        for (int k = i + 1; k < new_array.length; k++)            new_array[k] = array[j++];Â
        // print new array        for (i = 0; i < new_array.length; i++)            System.out.print(new_array[i] + " ");    }} |
1 2 3 4 5 6 7 8 9 10
Time complexity: O(n log n)
Approach 3:
- Create a set.
- Start adding all the elements in the set.
- Copy remaining elements of the given set to the new array.
Example: Inserting an element and then sorting the array.Â
Java
// Java program to insert an element// in an array and then sorting it.Â
// Importing util filesimport java.util.Arrays;import java.util.HashSet;import java.util.Set;Â
public class GFG {Â
    public static void main(String[] args)    {Â
        // using wrapper class here for array,        // to convert into object        Integer arr[] = { 6, 7, 8, 2, 3, 4, 5, 9, 10 };Â
        Set<Integer> sets            = new HashSet<Integer>(Arrays.asList(arr));Â
        sets.add(1);Â
        arr = sets.toArray(arr);Â
        // print the array        System.out.println(Arrays.toString(arr));    }} |
1 2 3 4 5 6 7 8 9 10
Time complexity: O(n log n)
