write a c++ program to sort an array of numbers in descending order using bubble sort.
Answers
Answered by
0
#include<stdio.h>
int main(){
int array[50], n, i, j, temp;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for(i = 0; i < n; i++)
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("Sorted list in descending order:\n");
for ( i = 0 ; i < n ; i++ )
printf("%d\n", array[i]);
return 0;
}
Answered by
0
Answer:
#include<iostream>
using namespace std;
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 arr[] = {9,8,4,5,2,7,1,3,0,2};
int len = sizeof(arr)/sizeof(arr[0]);
for(int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout << "\n";
bubble_sort(arr,len);
for(int i = 0; i < len; i++)
{
std::cout << arr[i] << " ";
}
return 0;
}
Explanation:
Similar questions