Sort an Array and Insert an Element Inside Array in Java

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:

  1. Create a new array of size N+1.
  2. Copy first array in New array.
  3. Insert number at the end of the array.
  4. Sort the array.

Example: Inserting an element and then sorting the array.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to insert an element in 
// an array and then sorting it. 
  
// Importing util files
import 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] + " ");
    }
}

chevron_right


Output:



1 2 3 4 5 6 7 8 9 10

Time complexity: O(n log n)

Approach 2:

  1. Sort the array.
  2. Create a new array of size N+1.
  3. Start traversing the given array and copy elements.
  4. If the given number is less than equal to the number present in the array, append given a number to the new array.
  5. Copy remaining elements of the given array to the new array.

Example: Inserting an element and then sorting the array.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to insert an element 
// in an array and then sorting it. 
  
// Importing util files
import 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] + " ");
    }
}

chevron_right


Output:

1 2 3 4 5 6 7 8 9 10

Time complexity: O(n log n)

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.