Bsort Bubble Sort Function Help!!

I have made a bubble sort user defined function program, which will arrange elements of array in descending order but it can't arrange it in descending order. Please help. I'm attaching the source code and output screenshot below.

P.S. I don't know high level coding like scanf printf, we have been only taught cout cin gets puts only. I also don't know about %d.
Screenshot:
https://s.yimg.com/hd/answers/i/f8a5ddce9c324cbe9c99ab3604f2e966_A.jpeg?a=answers&mr=0&x=1392570889&s=8a983a08abe7dd4056f3551483903c31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  #include<iostream.h>
#include<conio.h>
void bsort(int [],int);
void main()
{
        int i;
        clrscr();
        int b[5];
        bsort(b,5);
        cout<<"\n\nElements of Array in Descending Order: ";
        for(i=0;i<5;i++)
        {
                cout<<"\n\n"<<b[i];
        }
        getch();
}
void bsort(int a[],int n)
{
        clrscr();
        cout<<"\nEnter Size of Array: ";
        cin>>n;
        int i,j ,temp;
        cout<<"\nEnter Elements of Array: ";
        for(i=0;i<n;i++)
        {
                if(i>0)
                {
                        cout<<"\n                         ";
                }
                cin>>a[i];
        }
        for(i=n-2;i>=0;i--)
        {
                for(j=i-1;j>=0;j--)
                {
                        if(a[j]<a[j+1])
                        {
                                temp=a[j];
                                a[j]=a[j+1];
                                a[j+1]=temp;
                        }
                }
        }
}
closed account (iAk3T05o)
You are using c and c++ together?
The scanf() and printf() functions are for I/O in C. Avoid them. (You are doing fine using cin and cout.) You also appear to be using an older compiler. (Borland C++?)

Your bsort() function is doing too many things. It should not do any I/O. The only thing it should do is sort. If you want, make a separate function to get input for the array. Here are some prototypes to help:

1
2
3
4
5
6
7
8
9
10
11
12
int get_array_from_user( int a[], int max_n );
/* Asks the user for the number of elements to enter, making sure that 
    it is less than or equal to max_n.
    Gets the elements from the user and stores them in a[].
    Returns the number of elements entered by the user.
 */

void bsort( int a[], int n );
/* Sorts the first n elements of a[].  */

void print_array( int a[], int n );
/* Prints the first n elements of a[] to the standard output. */

A word of warning: your array is only five elements long (as per line 8). Yet you ask the user to enter a number of elements. What if the user enters 20?

Not all elements of an array need actually be used.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
        #define B_SIZE 100  /* Give the user up to 100 elements to use */
        int b[ B_SIZE ];
        int n;  // number of elements the user gives

        n = get_array_from_user( b, B_SIZE );

        bsort( b, n );

        print_array( b, n );

        getch();
}

Choice of sort order is controlled by two things: the direction of your loop(s) and the condition on line 38.

You are trying to do an optimized bubble sort, but you have confused yourself with everything.

Read through here for help.
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/bubble-sort/

Remember, the OUTER loop controls how far you need to go each time: i is where the inner loop should stop.

The INNER loop is the bubble loop, swapping things as necessary. It should always start at the first element (0) and stop at the last (i).

The CONDITION of the if statement should then control your sort order. < means that lesser elements get moved toward the back (descending order). > means that greater elements get moved toward the back (ascending order).

Hope this helps.
Thanks for the replies, I appreciate your work, but also I figured the other way round my code only, ie I sorted the array in Ascending order and then printed the array in Reverse Order so it made the array in Descending order, and Yes I am using Borland C++ which is taught in our School:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream.h>
#include<conio.h>
#include<string.h>
void bsort(int [],int);
void main()
{
	int i;
	clrscr();
	int b[5];
	bsort(b,5);
	cout<<"\n\nElements of Array in Descending Order: ";
	for(i=4;i>=0;i--)
	{
		if(i<4)
		{
			cout<<"\n\n                                       ";
		}
		cout<<b[i];
	}
	getch();
}
void bsort(int a[],int n)
{
	clrscr();
	cout<<"\nEnter Size of Array: ";
	cin>>n;
	int i,j ,temp;
	cout<<"\nEnter Elements of Array: ";
	for(i=0;i<n;i++)
	{
		if(i>0)
		{
			cout<<"\n                         ";
		}
		cin>>a[i];
	}
	for(i=n-2;i>=0;i--)
	{
		for(j=0;j<=i;j++)
		{
			if(a[j]>a[j+1])
			{
				temp=a[j];
				a[j]=a[j+1];
				a[j+1]=temp;
			}
		}
	}
}
Topic archived. No new replies allowed.