Skip to main content

PROGRAMMING FOR PROBLEM SOLVING LAB : Program 17

Write a C program that sorts the given array of integers using selection sort in descending order.


#include <stdio.h> 

void main()

{

int data[100],i,n,j,temp; 

clrscr();

printf("\n Enter the number of elements to be sorted:\n "); 

scanf("%d",&n);

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

{

 printf("\n %d. Enter element: ",i+1); 

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

}

printf("\n The Entered Elements are:\n"); 

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

printf("%d\t",data[i]);


for(j=0;j<n;++j) 

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

{

if(data[j]<data[i]) //To sort in ascending order, change < to >

{

temp=data[j]; 

data[j]=data[i]; 

data[i]=temp;

}

}

printf("\n The Elements in descending order:\n"); 

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

printf("%d\t",data[i]); 

getch();

}

OUTPUT:

Enter the number of elements to be sorted:

 7

 1. Enter element: 43

 2. Enter element: 45

 13. Enter element: 6

 4. Enter element: 87

 5. Enter element: 49

 6. Enter element: 52

 7. Enter element: 3

 The Entered Elements are:

43 45 6 87 49 52 3

 The Elements in descending order:

87 52 49 45 43 6 3

Comments

Popular posts from this blog

Learning and Development Interview Questions and answers for Mathematics-1.

  1). What is Mean, Mode and Median? Solution Answer: The mean is the average of a  collection of numbers or terms in a sequence. To calculate the mean use a formula is sum of total terms divided  by number of terms. The mode  is the most f requent number or term in a sequence. It means the number that occurred  highest number of times  in a sequence. To find the mode arrange the numbers in ascending or descending order and verify which number repeated most number of times in a sorted sequence. The median is the middle number/term where the sequence is arranged in ascending or descending order. If the sorted sequence have odd number of terms then  divide by 2 and round up to get the position of the median number.  If the  sorted sequence  have  even  number of terms then  divide by 2  to get the position of the median number.  2 ). What is the Difference between Fractional and Rational number? Solu...