Problem statement:- Program to Sort the elements of an array in ascending order.
Data requirement:-
Input Data:- arr,size
Output Data:-arr
Output Data:-arr
Additional Data:- i, j, temp
Program in C
Here is the source code of the C Program to Sort the elements of an array in ascending order.
Code:
#include<stdio.h>
main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size];
int i,temp,j;
printf("Enter the Element of the array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
printf("\nAfter sorting array elements are:");
for(i=0;i<size;i++)
{
printf("%d ",arr[i]);
}
}
main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size];
int i,temp,j;
printf("Enter the Element of the array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
printf("\nAfter sorting array elements are:");
for(i=0;i<size;i++)
{
printf("%d ",arr[i]);
}
}
Input/Output:
Enter the size of the array:5
Enter the Element of the array:
4
5
3
2
1
After sorting array elements are:1 2 3 4 5
Enter the Element of the array:
4
5
3
2
1
After sorting array elements are:1 2 3 4 5
Program in C++
Here is the source code of the C++ Program to Sort the elements of an array in ascending order.
Code:
#include<iostream>
using namespace std;
main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size];
int i,temp,j;
cout<<"Enter the Element of the array:\n";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
cout<<"\nAfter sorting array elements are:";
for(i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
}
using namespace std;
main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size];
int i,temp,j;
cout<<"Enter the Element of the array:\n";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
cout<<"\nAfter sorting array elements are:";
for(i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
}
Input/Output:
Enter the size of the array:4
Enter the Element of the array:
978
305
10
30
After sorting array elements are:10 30 305 978
Enter the Element of the array:
978
305
10
30
After sorting array elements are:10 30 305 978
Program in Java
Here is the source code of the Java Program to Sort the elements of an array in ascending order.
Code:
import java.util.Scanner;
public class p25 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int i,j,temp,size;
size=sc.nextInt();
int arr[ ]=new int[size];
System.out.println("Enter the Element of the array:");
for(i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
System.out.print("\nArray elements are:");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
System.out.print("\nAfter sorting array elements are:");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}
sc.close();
}
}
public class p25 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int i,j,temp,size;
size=sc.nextInt();
int arr[ ]=new int[size];
System.out.println("Enter the Element of the array:");
for(i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
System.out.print("\nArray elements are:");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]>=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
System.out.print("\nAfter sorting array elements are:");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}
sc.close();
}
}
Input/Output:
Enter the size of the array:
6
Enter the Element of the array:
45
78
98
45
12
85
Array elements are:45 78 98 45 12 85
After sorting array elements are:12 45 45 78 85 98
6
Enter the Element of the array:
45
78
98
45
12
85
Array elements are:45 78 98 45 12 85
After sorting array elements are:12 45 45 78 85 98
Program in Python
Here is the source code of the Python Program to Sort the elements of an array in ascending order.
Code:
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
print("Before sorting array elements are:")
for i in range(0,size):
print(arr[i],end=" ")
for i in range(0,size):
for j in range(i+1, size):
if arr[i] >= arr[j]:
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
print("\nAfter sorting array elements are:")
for i in range(0, size):
print(arr[i],end=" ")
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
print("Before sorting array elements are:")
for i in range(0,size):
print(arr[i],end=" ")
for i in range(0,size):
for j in range(i+1, size):
if arr[i] >= arr[j]:
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
print("\nAfter sorting array elements are:")
for i in range(0, size):
print(arr[i],end=" ")
Input/Output:
Enter the size of the array: 4
Enter the Element of the array:
78
999
1025
54
Before sorting array elements are:
78 999 1025 54
After sorting array elements are:
54 78 999 1025
Enter the Element of the array:
78
999
1025
54
Before sorting array elements are:
78 999 1025 54
After sorting array elements are:
54 78 999 1025
Most Recommend Questions:-
More Questions:-
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box