Insert a number in an array sorted in ascending order

Description

To insert element in an array sorted in ascending order, First compare the elements which is 
to be inserted(num) with every elements of array from start, if num is greater than elements
of array increment the position. And when the num become less than any element insert num 
to that position. Rest follow the insertion of element at given position.

C/C++

/* C program to insert element in an array 
sorted in ascending order*/

//Save it as InsertElementSortedArray.c

#include<stdio.h>
int main(){

    int i, n, arr[100], num, pos=0;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    printf("Enter the elements of the array : ");
    for (i = 0; i < n; i++) {
        scanf("%d",&arr[i]);
    }

    printf("Enter the element to be entered : ");
    scanf("%d",&num);

    
    /*Find position where elements should be entered.*/
    
    for(i=0;i<n;i++){
        if(arr[i]<num){
            pos++;
        }else{
            break;
        }
    }

    printf("%d should be entered at position %d",num,pos);

    
    /* Execute a loop to move all elements right by 1 position having 
    index greater than position where to insert element */
    
    for(i=n-1;i>=pos;i--) {
        arr[i+1] = arr[i];
    }
    
    // Insert element at given position
    arr[pos] = num;

    // Finally print new array after insertion of new element
    printf("\nThe new array is : ");
    for(i=0;i<=n;i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}
Input:
Enter the size of array : 
5
Enter the elements of the array : 
16
25
29
37
55
Enter the element to be entered : 
35
Output:
35 should be entered at position 3

The new array is : 
16 25 29 35 37 55

Java

/* Java program to insert element in an array 
   sorted in ascending order*/

//Save it as InsertElementSortedArray.java


import java.io.*;
import java.util.Scanner;

public class InsertElementSortedArray {

    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        int i, n, num, pos=0;
        
        int arr[] = new int[100];

        System.out.println("Enter the size of array : ");
        n = scanner.nextInt();

        System.out.println("Enter the elements of the array : ");
        for (i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        System.out.println("Enter the element to be entered : ");
        num = scanner.nextInt();

        
           /*Find position where elements should be entered.*/
        
        for(i=0;i<n;i++){
            if(arr[i]<num){
                pos++;
            }else{
                break;
            }
        }

        System.out.println(num + " should be entered at position "+ pos );
        
          /* Execute a loop to move all elements right by 1 position having 
             index greater than position where to insert element */
        
        for(i=n-1;i>=pos;i--) {
            arr[i+1] = arr[i];
        }
        
          // Insert element at given position
        arr[pos] = num;

           // Finally print new array after insertion of new element
        System.out.println("\nThe new array is : ");
        for(i=0;i<=n;i++) {
            System.out.print(arr[i]+" ");
        }
    }
}
Input:
Enter the size of array : 
5
Enter the elements of the array : 
16
25
29
37
55
Enter the element to be entered : 
35
Output:
35 should be entered at position 3

The new array is : 
16 25 29 35 37 55

Related Programs

1) Program to insert a number at given position of an Array
2) Program to delete an element from given location in an array
3) Program to delete an element from an array sorted in ascending order
4) Program to merge two sorted arrays
5) Program to swap maximum and minimum element of Array
6) Program to Search for an Element in an Array
7) Program to Reverse an Array using Recursion
8) Program to Find Median of a Array
9) Program to Find Mode of a Array
10) Program to Find Value equal to index value
11) Program to Check Array is Perfect or Not
Share Me

Leave a Reply