write a program to sort the array

09 June 2022

write a program to sort the array


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Given an integer array of size N, write a program to sort the array;

Sample input 1:

4

2 4 1 3

Sample output 1:

1 2 3 4

Sample input 2:

5

1 5 7 5 3

Sample output 2:

1 3 5 5 7

 

C Program

#include <stdio.h>

#include <limits.h>

 

void Sorting(int *arr, int n)

{

    int idx,temp;

    for(int i = 0 ; i<n-1 ; i++)

    {

        idx = i;

        for(int j = i+1 ; j<n ; j++)

        {

            if(arr[idx]>arr[j])

            {

                idx = j;

            }

        }

        temp = arr[i];

        arr[i] = arr[idx];

        arr[idx] = temp;

    }

}

 

int main()

{

      int n;          scanf("%d",&n);

      int arr[n];

      for(int i = 0 ; i<n ; i++)

      {

            scanf("%d",&arr[i]);

      }

      Sorting(arr,n);

      for(int i = 0 ; i<n ; i++)

      {

            printf("%d ",arr[i]);

      }

      return 0;

}

C++ Program

#include <bits/stdc++.h>

 using namespace std;

 

int main()

{

      int n;          cin>>n;

      int arr[n];

      for(int i = 0 ; i<n ; i++)

      {

            cin>>arr[i];

      }

      sort(arr,arr+n);

      for(int i = 0 ; i<n ; i++)

      {

            cout<<arr[i]<<" ";

      }

      return 0;

}

JAVA Program

import java.util.*;

class Main

{

      public static void main(String[] args) throws java.lang.Exception

      {

            Scanner sc = new Scanner(System.in);

            int n = sc.nextInt();

            int arr[] = new int[n];

            for(int i = 0 ; i<n ; i++)

            {

                        arr[i] = sc.nextInt();

            }

            Arrays.sort(arr);

            for(int i = 0 ; i<n ; i++)

            {

                        System.out.print(arr[i] + " ");

            }

      }

}

PYTHON Program

#Driver function

n = int(input())

arr = list(map(int,input().split(' ')))

arr.sort()

print(arr)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !