+92 332 4229 857 99ProjectIdeas@Gmail.com

Bubble sort-Descending (C++)


Descending through bubble sort
bubbleSortDesc() sorts the array in descending order. It takes 2 parameter:
first parameter is the starting address of the array and the
second parameter is the length of the array.
It returns the starting address of the array-sorted in descending order.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

int*bubbleSortDesc(int*arr,int len)
{
       int temp;
   
       for(int i=0;i<len;i++)
       {
              for(int j=0;j<len-1;j++)
              {
                     if(arr[j]<arr[j+1])
                     {
                           temp=arr[j];
                           arr[j]=arr[j+1];
                           arr[j+1]=temp;
                     }
              }
       }
return arr;
}

int main()
{
       const int len=11;
       int    arr[len]={-1,-2,-3,-4,-5,0,5,4,3,2,1};

       cout<<"Array before sorting : ";
              for(int i=0;i<len;i++)
                     cout<<arr[i]<<" ";
             
              cout<<endl;

              int*ptrAsc=bubbleSortDesc(arr,len);

       cout<<"Array  after sorting : ";
              for(int i=0;i<len;i++)
                     cout<<ptrAsc[i]<<" ";

              cout<<endl;

              _getche();
       return 0;
}

Output
Array before sorting : -1 -2 -3 -4 -5 0 5 4 3 2 1
Array  after sorting : 5 4 3 2 1 0 -1 -2 -3 -4 -5

Related articles

0 comments: