Friday, December 27, 2024
Google search engine
HomeLanguagesJavaSort an Array and Insert an Element Inside Array in Java

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




// 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] + " ");
    }
}


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




// 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] + " ");
    }
}


Output

1 2 3 4 5 6 7 8 9 10 

Time complexity: O(n log n)

Approach 3:

  1. Create a set.
  2. Start adding all the elements in the set.
  3. 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 files
import 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));
    }
}


Output

1 2 3 4 5 6 7 8 9 10 

Time complexity: O(n log n)

RELATED ARTICLES

Most Popular

Recent Comments