write a program which accepts 10 integers in an array and then arrange the array in ascending order.
Answers
Answered by
10
void main()
{
int i,j,temp,a[10];
clrscr();
printf(“Enter 10 integer numbers: \n”);
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
for (i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf(“\n\nThe 10 numbers sorted in ascending order are: \n”);
for(i=0;i<10;i++)
printf(“%d\t”,a[i]);
getch();
}
OUTPUT:
Enter 10 integer numbers:
2
9
7
4
3
6
8
1
5
10
The 7 numbers sorted in ascending order are:
1 2 3 4 5 6 7 8 9 10
{
int i,j,temp,a[10];
clrscr();
printf(“Enter 10 integer numbers: \n”);
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
for (i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf(“\n\nThe 10 numbers sorted in ascending order are: \n”);
for(i=0;i<10;i++)
printf(“%d\t”,a[i]);
getch();
}
OUTPUT:
Enter 10 integer numbers:
2
9
7
4
3
6
8
1
5
10
The 7 numbers sorted in ascending order are:
1 2 3 4 5 6 7 8 9 10
Answered by
3
This is can be done by sorting and sorting can be done in two process :
1)Bubble sort
2)Selection sort
Sorting the array by BUBBLE SORT
import java.util.Scanner;
class BubbleSort
{
public static void main(String []args)
{
int num, i, j, temp;
Scanner sc = new Scanner(System.in);
int array[] = new int[10];
System.out.println("Enter 10 NUMBER: ");
for (i = 0; i <10; i++)
{
array[i] = sc.nextInt();
}
for (i = 0; i < 9; i++)
{
for (j = 0; j <( 9 - i ); j++)
{
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("Sorted list of numbers in ASCENDING ORDER:");
for (i = 0; i < 10; i++)
{
System.out.println(array[i]);
}
}
}
Sorting by SELECTION SORT
import java.util.Scanner;
class SelectionSort
{
public static void main(String args[])
{
int i, j, position ,temp;
int array[] = new int[10];
Scanner scan = new Scanner(System.in);
System.out.print("Enter 10 NUMBER: ");
for(i=0; i<10; i++)
{
array[i] = scan.nextInt();
}
for (i = 0; i < 9; i++)
{
position = i;
for (j = i+1; j < 9; j++)
{
if (array[j] < array[position])
{
position = j;
}
}
temp = array[i];
array[i] = array[position];
array[position]= temp;
}
System.out.println("NUMBERS In ASCENDING ORDER is :");
for(i=0; i<10; i++)
{
System.out.println(array[i]);
}
}
}
HOPE IT HELPs!!!!!!!!!!
1)Bubble sort
2)Selection sort
Sorting the array by BUBBLE SORT
import java.util.Scanner;
class BubbleSort
{
public static void main(String []args)
{
int num, i, j, temp;
Scanner sc = new Scanner(System.in);
int array[] = new int[10];
System.out.println("Enter 10 NUMBER: ");
for (i = 0; i <10; i++)
{
array[i] = sc.nextInt();
}
for (i = 0; i < 9; i++)
{
for (j = 0; j <( 9 - i ); j++)
{
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("Sorted list of numbers in ASCENDING ORDER:");
for (i = 0; i < 10; i++)
{
System.out.println(array[i]);
}
}
}
Sorting by SELECTION SORT
import java.util.Scanner;
class SelectionSort
{
public static void main(String args[])
{
int i, j, position ,temp;
int array[] = new int[10];
Scanner scan = new Scanner(System.in);
System.out.print("Enter 10 NUMBER: ");
for(i=0; i<10; i++)
{
array[i] = scan.nextInt();
}
for (i = 0; i < 9; i++)
{
position = i;
for (j = i+1; j < 9; j++)
{
if (array[j] < array[position])
{
position = j;
}
}
temp = array[i];
array[i] = array[position];
array[position]= temp;
}
System.out.println("NUMBERS In ASCENDING ORDER is :");
for(i=0; i<10; i++)
{
System.out.println(array[i]);
}
}
}
HOPE IT HELPs!!!!!!!!!!
Similar questions