write a c program to accept 1-D array from the user and perform Bubble sort in descending order.
Answers
Answer:
This is your answer mate :-
Explanation:
/* C Program to sort array in descending order using bubble sort */
#include<stdio.h>
int main(){
int array[50], n, i, j, temp;
printf("Enter number of elements :: ");
scanf("%d", &n);
printf("\nEnter %d integers :: \n", n);
for(i = 0; i < n; i++)
{
printf("\nEnter %d integer :: ", i+1);
scanf("%d", &array[i]);
}
for (i = 0 ; i < ( n - 1 ); i++){
for (j= 0 ; j < n - i - 1; j++){
if(array[j] < array[j+1]){
temp=array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("\nSorted list in descending order : ");
for ( i = 0 ; i < n ; i++ )
printf(" %d ", array[i]);
return 0;
}
- How to use this:-
- You need to first tell the computer how many numbers you need to add which in this situation is represented by - Enter number of elements
- And then you need to add your all numbers there
- Hope it helps
- Please mark me as a brainliest
Answer:
#include<stdio.h>
void bubble_sort(int arr[] , int len)
{
for(int i = 0; i < len; i++)
{
for(int j = 0; j < len; j++)
{
if(arr[i] > arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main()
{
int len;
printf("Enter the number of elements in the array:");
scanf("%d" , &len);
int arr[len];
printf("Enter all %d elements in the array: \n" , len);
for(int i = 0; i < len; i++){
scanf("%d" , &arr[i]);
}
printf("---THE ORIGINAL ARRAY--- \n");
for(int i = 0; i < len; i++){
printf("%d " , arr[i]);
}
bubble_sort(arr,len);
printf("\n---AFTER SORTING IN DESCENDING ORDER--- \n");
for(int i = 0; i < len; i++){
printf("%d " , arr[i]);
}
return 0;
}
Explanation: