Program to swap maximum and minimum element of Array

Description

To swap maximum and minimum numbers of an aaray, first find the position of maximum
and minimum element. When position found then swap the element of that position.

C/C++

/* C program to swap smallest and largest element of array*/
//Save it as SwapSmallestLargestElement.c

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

    int i, n;

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

    int arr[n];

    printf("Enter the elements to be entered : ");
    for(i=0;i<n;i++){
        scanf("%d",&arr[i]);
    }
    
    //Call a function Find maximum element of array
    int maxElementPos = findMaxElement(arr,n);
    printf("\nThe maximum element : %d", arr[maxElementPos]);

    //Call a function Find minimum element of array
    int minElementPos = findMinElement(arr,n);
    printf("\nThe minimum element : %d", arr[minElementPos]);

    //Call a function swap maximum and minimum element
    swapMaxMinElement(arr, maxElementPos, minElementPos);

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

    return 0;
}

//Find maximum element of array
int findMaxElement(int arr[],int n){
    int i, pos=0;
    int maxVal = arr[0];
    for(i=0;i<n;i++){
        if(arr[i]>maxVal){
            maxVal = arr[i];
            pos=i;
        }
    }
    return pos;
}

//Find minimum element of array
int findMinElement(int arr[],int n){
    int i, pos=0;
    int minVal = arr[0];
    for(i=0;i<n;i++){
        if(arr[i] < minVal){
            minVal = arr[i];
            pos=i;
        }
    }
    return pos;
}

//Swap maximum and minimum element
void swapMaxMinElement(int arr[], int maxElementPos, int minElementPos){


/*To swap max and min numbers, take a temporary variable, and follow the below step, 
1) assign maximum number to temporary vaiable.
2) assign minimum number to maximum number.
3) asign temporary vaiable to minimum number*/

    int temp;
    temp = arr[maxElementPos];
    arr[maxElementPos] = arr[minElementPos];
    arr[minElementPos] = temp;
}
Input:
Enter the size of array : 
5
Enter the elements to be entered : 
25
29
55
37
16
Output:
The maximum element : 55
The minimum element : 16
The array after swap is : 
25 29 16 37 55

Java

/* Java program to swap smallest and largest element of array*/
//Save it as SwapSmallestLargestElement.java

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

public class SwapSmallestLargestElement {

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

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

        int arr[] = new int[n];

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

            //Call a function Find maximum element of array
        int maxElementPos = findMaxElement(arr,n);
        System.out.println("The maximum element : "+ arr[maxElementPos]);

            //Call a function Find minimum element of array
        int minElementPos = findMinElement(arr,n);
        System.out.println("The minimum element : "+ arr[minElementPos]);

            //Call a function swap maximum and minimum element
        swapMaxMinElement(arr, maxElementPos, minElementPos);

            // Finally print array after swap
        System.out.println("The array after swap is : ");
        for(i=0;i<n;i++){
            System.out.print(arr[i]+ " ");
        }
    }
    
    //Find maximum element of array
    public static int findMaxElement(int arr[],int n){
        int i, pos=0;
        int maxVal = arr[0];
        for(i=0;i<n;i++){
            if(arr[i]>maxVal){
                maxVal = arr[i];
                pos=i;
            }
        }
        return pos;
    }
    
        //Find minimum element of array
    public static int findMinElement(int arr[],int n){
        int i, pos=0;
        int minVal = arr[0];
        for(i=0;i<n;i++){
            if(arr[i] < minVal){
                minVal = arr[i];
                pos=i;
            }
        }
        return pos;
    }
    
    //Swap maximum and minimum element
    public static void swapMaxMinElement(int arr[], int maxElementPos, int minElementPos){
        
        /*To swap max and min numbers, take a temporary variable, and follow the below step, 
          1) assign maximum number to temporary vaiable.
          2) assign minimum number to maximum number.
          3) asign temporary vaiable to minimum number*/
        
        int temp;
        temp = arr[maxElementPos];
        arr[maxElementPos] = arr[minElementPos];
        arr[minElementPos] = temp;
    }
}
Input:
Enter the size of array : 
5
Enter the elements to be entered : 
25
29
55
37
16
Output:
The maximum element : 55
The minimum element : 16
The array after swap is : 
25 29 16 37 55

Related Programs

1) Find Smallest element and it’s position of an Array
2) Program for finding Second Largest Element Of Array
3) Program to Find the smallest missing number
4) Program to Find missing odd number in first n odd number
5) Program to Find K largest elements from array
6) Program to Print How Many Numbers Smaller than Current Number
7) Remove Duplicate Elements From Array
8) Program to Check Array is Perfect or Not
9) Program to Find Mode of a Array
10) Program to Rearrange an array such that arr[i]=i
Share Me

Leave a Reply