Please Help Me to Give Algoritham for this program.

Write your question here.

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
  // 7} WAP to print number in ascending order using array.
//Asending Order
#include<iostream.h>
#include<conio.h>
void main()
{
int a[100];
int i,j,n,temp;
clrscr();
cout<<"How Many Numbers?"<<endl;
cin>>n;
cout<<"Enter The Elements"<<endl;
for(i=0;i<=n-1;++i)
{
cin>>a[i];
}
for(i=0;i<=n-1;++i)
{
  for (j=0;j<=n-1;++j)
{
  if (a[i]<a[j])
  {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
  }
 }
 }
 cout<<"Asending Order"<<endl;
 for(i=0;i<=n-1;++i)
 {
  cout<<a[i]<<'\t';
  }
  cout<<endl;
  getch();
  }
Give Algoritham for this program

You seem to have one already.
(Generally, one must first know what to do and then write it.)

Perhaps you have some other question(s)?
There’s a well known algorithm very close to the one you chose called “BubbleSort”.
The main difference is that it repeatedly checks if the array is already sorted and exits as soon as it realizes it is. This way, it avoids pointless iterations.
Example of BubbleSort algorithm with already sorted array:
How Many Numbers? 5
Enter the elements separated by spaces: 1 2 3 4 5
Ascending Order:
1       2       3       4       5
It took me 4 iteration.

While with your code the above sorting results in:
How Many Numbers? 5
Enter the elements separated by spaces: 1 2 3 4 5
Ascending Order:
1       2       3       4       5
It took me 25 iteration.

BubbleSort is explained here:
https://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm
If you need the code, just ask.

(Note: I pointed out the main difference, but there’s also another one: you don’t need your “i” and “j” to go both through the entire range.)
c++ has a built in sort, which is smart and fast.

If you want to do your own sort, I highly recommend shell sort, which is extremely efficient and
the code is virtually identical to the simple sorts (insertion, bubble, etc) in difficulty to write. It is a little challenging to understand its big-O but take it on faith that it runs @ N( (C+1)/C) run time, typical implementations get N(7/6) run time.

If you are sorting more than several million items, quicksort will overtake it somewhere in that range. Quicksort is a pain to write correctly, as it will use... a copy of shell sort when the split lists become small enough from the recursion, or some people use insertion there.

Last edited on
Topic archived. No new replies allowed.