Computer Science, asked by sujathaganesansuji, 2 months ago

write a c program to sort the given set of integers in ascending order​

Answers

Answered by anindyaadhikari13
3

Answer:

This is the required C program for the question.

The list is sorted using selection sort algorithm.

#include <stdio.h>

int main () {

int i,j,n,x;

printf("How many elements? ");

scanf("%d", &n);

printf("Enter them..\n");

int a[n];

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

printf("Enter: ");

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

}

printf("Given List: \n");

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

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

if(i!=n-1)

printf(", ");

}

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

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

if(a[i]>a[j]) {

x=a[i];

a[i]=a[j];

a[j]=x;

}

}

}

printf("\nSorted List: \n");

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

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

if(i!=n-1)

printf(", ");

}

return 0;

}

Algorithm:

  1. START.
  2. Accept n elements.
  3. Store them in array.
  4. Sort the array by comparing and swapping each elements with elements after it.
  5. Display the array.
  6. STOP.

See the attachment for output ☑.

Attachments:

Anonymous: Marvellous !
Answered by ashna2658
1

Answer:

1. Create an array of fixed size (maximum capacity), lets say 10.

2. Take n, a variable which stores the number of elements of the array, less than maximum capacity of array.

3. Iterate via for loop to take array elements as input, and print them.

4. The array elements are in unsorted fashion, to sort them, make a nested loop.

5. In the nested loop, the each element will be compared to all the elements below it.

6. In case the element is greater than the element present below it, then they are interchanged

7. After executing the nested loop, we will obtain an array in ascending order arranged elements.

HOPE IT WILL HELP YOU

Attachments:
Similar questions