Q. Write a program to sort an array element in descending order using Bubble Sort.
Bubble Sort :- Bubble sort is also known as sinking sort. Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if order is wrong.
Descending order :- Numbers are said to be in descending order when they are arranged from largest to smallest number. Such as 21, 17, 13, 9 and 3 are arranged in descending order.
Bubble Sort Algorithm
START Step 1: Repeat Step 2 For i = 0 to N Step 2: Repeat For J = 0 to N - I Step 3: IF A[J] < A[J+1] SWAP A[J] and A[J+1] [END OF INNER LOOP] [END OF OUTER LOOP Step 4: EXIT STOP
Bubble Sort Program
#include <stdio.h> int main(){ int temp, i, j, number[5]={6,7,2,1,4}; /* This is the main logic of bubble sort algorithm */ for(i = 0; i < 5; i++){ for(j = 0; j < 5-i-1 ; ++j){ if(number[j] < number[j+1]){ temp = number[j]; number[j] = number[j+1]; number[j+1] = temp; } } } printf("Sorted Array: "); for(i=0;i<5;i++) printf(" %d",number[i]); return 0; }
// Bubble sort in C++ #include <iostream> using namespace std; void bubble_Sort(int array[], int size) { for (int i = 0; i < size - 1; ++i) { for (int i = 0; i < i - i - 1; ++i) { // To sort in ascending order, change < to > in this line. if (array[i] < array[i + 1]) { int temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; } } } } void print_Array(int array[], int size) { for (int i = 0; i < size; ++i) { cout << " " << array[i]; } cout << "\n"; } int main() { int number[] = {6, 7, 2, 1, 4}; int size = sizeof(number) / sizeof(number[0]); bubble_Sort(number, size); cout << "Sorted Array :\n"; print_Array(number, size); }
// Bubble sort in Java import java.util.Arrays; class LFC { void bubble_Sort(int array[]) { int size = array.length; for (int i = 0; i < size - 1; i++) for (int j = 0; j < size - i - 1; j++) // To sort in ascending order, change < to > in this line. if (array[j] < array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } public static void main(String args[]) { int[] number = { 6, 7, 2, 1, 4 }; LFC bs = new LFC(); bs.bubble_Sort(number); System.out.println("Sorted Array :"); System.out.println(Arrays.toString(number)); } }
def bubble_Sort(array): for i in range(len(array)): for j in range(0, len(array) - i - 1): # To sort in ascending order, change < to > in this line. if array[j] < array[j + 1]: (array[j], array[j + 1]) = (array[j + 1], array[j]) number = [ 6, 7, 2, 1, 4 ]; bubble_Sort(number) print('Sorted Array: ') print(number)
using System; namespace LFC { class MySort { static void Main(string[] args) { int[] arr = { 6, 7, 2, 1, 4 }; int temp; for (int j = 0; j <= arr.Length - 2; j++) { for (int i = 0; i <= arr.Length - 2; i++) { if (arr[i] < arr[i + 1]) { temp= arr[i + 1]; arr[i + 1] = arr[i]; arr[i] = temp; } } } Console.WriteLine("Sorted Array:"); foreach (int p in arr) Console.Write(p + " "); Console.Read(); } } }
function bubble_Sort(&$number) { $n = sizeof($number); // Traverse through all array elements for($i = 0; $i < $n; $i++) { $temp = False; // Last i elements are already // in place for ($j = 0; $j < $n - $i - 1; $j++) { if ($number[$j] < $number[$j+1]) { $t = $number[$j]; $number[$j] = $number[$j+1]; $number[$j+1] = $t; $temp = True; } } // IF no two elements were swapped // by inner loop, then break if ($temp == False) break; } } // Driver code to test above $number = array(6, 7, 2, 1, 4); $len = sizeof($number); bubble_Sort($number); echo "Sorted Array : \n"; for($i = 0; $i < $len; $i++) echo $number[$i]." ";
#include <stdio.h> int main(){ int temp, i, j, number[5]={6,7,2,1,4}; /* This is the main logic of bubble sort algorithm */ for(i = 0; i < 5; i++){ for(j = 0; j < 5-i-1 ; ++j){ if(number[j] < number[j+1]){ temp = number[j]; number[j] = number[j+1]; number[j+1] = temp; } } } printf("Sorted Array: "); for(i=0;i<5;i++) printf(" %d",number[i]); return 0; }
Output
Sorted Array: 7 6 4 2 1
Recommended Programs
Program to find length of a stringProgram to check armstrong number