Data Structures Practical 6
Question 1
Modify the Bubble Sort algorithm to sort an array in descending order.
bubblesort.c
C
#include <stdio.h>
#include <conio.h>
#define max 30
void main()
{
int i, size, temp, j, arr[max];
clrscr();
printf("\nEnter the number of elements in the array : ");
scanf("%d", &size);
printf("\nEnter the elements: ");
for (i = 0; i < size; i++)
{
printf("\nEnter element %d: ", i + 1);
scanf("%d", &arr[i]);
}
for (i = 0; i < size; i++)
{
for (j = 0; j < size - i - 1; j++)
{
if (arr[j] < arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("\nThe array sorted in descending order is :\n");
for (i = 0; i < size; i++)
printf("%d\t", arr[i]);
getch();
}
Output
