OneBite.Dev - Coding blog in a bite size

sort items in array by desc in C

Code snippet on how to sort items in array by desc in C

  void bubbleSort(int arr[], int n) 
  {  
     int i, j; 
     for (i = 0; i < n-1; i++)       
      for (j = 0; j < n-i-1; j++)  
          if (arr[j] < arr[j+1]) 
              swap(&arr[j], &arr[j+1]); 
  } 

This code is using the Bubble Sort algorithm to sort an array of integers. The array and the number of elements to be sorted are passed as parameters to the function. The algorithm has two nested loops which iterate over the array. The outer loop iterates over all the elements of the array and the inner loop compares two elements at a time, the element to its right and the element to its left. If the element to its right is greater than the element to its left, the two elements are swapped. This process continues iterating over the array until the array is arranged in descending order.

c