For the descending sort, it only moves the highest number to the correct spot and leaves the other numbers in ascending order. Can anyone tell me what I am doing wrong?

#include<iostream.h>
#include<cmath>
void sortit(int x[], int n)
{
   int temp,i,j;
   for ( i=0;i<n;i++ )
      for ( j=i;j<n;j++ )
         if ( x[i]>x[j] )
         {
            temp=x[i];
            x[i]=x[j];
            x[j]=temp;
         }
}
void reversesort(int x[], int n)
{
   int temp,i,j;
   for ( i=0;i<n;i++ )
      for ( j=1;j<n;j++ )
         if ( x[i]<x[j] )
         {
            temp=x[i];
            x[i]=x[j];
            x[j]=temp;
         }
}
main()
{
   int num[20];
   int max=19;
   int i;
   for ( i=0;i<20;i++ )
   {
      cin>>num[i]; //
   }
   sortit(num,max);
   cout<<"The list in ascending order is: "<<endl;
   for ( int i=0;i<max;i++ )
      cout<<num[i]<<"\n";
   reversesort(num,max);
   cout<<"The list in descending order is: "<<endl;
   for ( int i=0;i<max;i++ )
      cout<<num[i]<<"\n";
}

Recommended Answers

All 2 Replies

void reversesort(int x[], int n)
{
   int temp,i,j;
   for ( i=0;i<n;i++ )
      for ( j=[B]i[/B];j<n;j++ )

Also

int max=20;

And

for ( i=0;i<max;i++ )

(The first loop in main.)

Or, something like this.

#include <iostream>
#include <cstdlib>
#include <cstddef>
using namespace std;

void sortit(int x[], int n)
{
   int temp, i, j;
   for ( i = 0; i < n; ++i )
   {
      for ( j = i; j < n; ++j )
      {
         if ( x[i] > x[j] )
         {
            temp = x[i];
            x[i] = x[j];
            x[j] = temp;
         }
      }
   }
}

void reversesort(int x[], int n)
{
   int temp, i, j;
   for ( i = 0; i < n; ++i )
   {
      for ( j = i; j < n; ++j )
      {
         if ( x[i] < x[j] )
         {
            temp = x[i];
            x[i] = x[j];
            x[j] = temp;
         }
      }
   }
}

int main()
{
   int num[5];
   size_t i;
   for ( i = 0; i < sizeof num / sizeof *num; ++i )
   {
      num[i] = rand(); // I don't feel like typing.
   }
   sortit(num, sizeof num / sizeof *num);
   cout<<"The list in ascending order is:\n";
   for ( i = 0; i < sizeof num / sizeof *num; ++i )
   {
      cout << num[i] << "\n";
   }
   reversesort(num, sizeof num / sizeof *num);
   cout<<"The list in descending order is:\n";
   for ( i = 0; i < sizeof num / sizeof *num; ++i )
   {
      cout << num[i] << "\n";
   }
   return 0;
}
for ( i = 0; i < n; ++i )
   {
      for ( j = i; j < n; ++j )

that is a waste of cpu cycles because it compares the first array element with itself. array is the same as array[j]

for ( i = 0; i < n-1; ++i )
   {
      for ( j = i+1; j < n; ++j )

The above changes makes the bubble sort run a few micro-milliseconds faster because it avoids unnecessary loop iterations and variable comparisons. This could make a more significant difference when comparing strings or other complex data types.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.