PROGRAM FOR A SELECTION SORT

// PROGRAM FOR A SELECTION SORT;
#include "iostream.h"
#include "conio.h"
class selection
{
int i,data[100],size,j,t;
public:
void get();
void sort();
void show();
};
void selection::get()
{
cout<<"\n\nENTER THE SIZE OF ARRAY:"; cin>>size;//from keybord
for(i=0;i<=size-1;i++) { cout<<"\n\nenter el:"; cin>>data[i];//from keybord
}
}
void selection::sort()
{
for(i=0;i<=size-2;i++) for(j=i+1;j<=size-1;j++) { if(data[i]>data[j])
{
t=data[i];
data[i]=data[j];
data[j]=t;
}
}
}
void selection::show()
{
cout<<"\n\n THE SERIES IS :"; for(i=0;i<=size-1;i++) { cout<<" "<<<"\n\nAFTER SORTING :"; s.show(); getch(); } //*/*/*/*/*/*/*/*/*/*/*/*/*//**/*/*/*/*/*/*/*/***/*/*/*/*/*/* /* EXPLANATION FOR THE 'FOR LOOP'; i.e for(i=0;i<=size-2;i++) // for how many iterations for(j=i+1;j<=size-1;j++) if(data[i]>data[j])
{
t=data[i];
data[i]=data[j];
data[j]=t;
}


THIS IS EXPLAIN AS GIVEN BELOW.....
let us take example that the series is
55 22 44 11 33
HERE ONE THING IS NOTABLE THAT THE INDEX OF
'55' is '0'
'22' is '1'
'44' is '2'
'11' is '3'
'33' is '4'
when i=0; 'j' will be 1 and we know that in case
of two loop the second loop will execute first.
now the size of the series is 5 therefore 'j' will
goes from 1 to 4. In the series here 'i' is 55 and
'j' is 22 44 11 33. Second for loop execute first . i.e is
it check data[55]>data[22] . if it is true then will
interchange otherwise as it is .Again when loop round
i.e when 'j' become 2 it check again data[22]>data[44],
because 'i' becomes 22 as the condition was true .
Here as the condition is false , there will be no
change .Again loop round and 'j' become 3.It check
data[22]>data[11],as it is true there will a change.
Now at last when 'j' becomes 4 it check data[22]>data[33],
it is false therefore here will be no change.
Now when j becomes 5 secomd loop
will exit as the condition is false for 'for loop';
Now 'i' becomes 1 and 'j' will be 2. Here in second execution
'j' will goes from 2 to 4; NOW THE SERIES IS 11 55 44 22 33
As the index of 55 is 1 therefore here 'i' will be 55 and
'j'would be 44 22 33 .In this csse data[55]>data[44],
as the condition is true there will be change .Now the series
is 11 44 55 22 33 .Next data[44]>data[22] ,it is true i.e is
the series is 11 22 55 44 33 .Now data[22]>data[33] as it
false ,therefore there will no change . Now it exit from
second loop again. Now 'i' will be 55 and 'j' are 44 33.
It check data[55]>data[44] it is true therefore series is
11 22 44 55 33.Loop round and check data[44]>data[33]
it is true .Now the series is 11 22 33 55 44.Again it will
exit from second loop. Now 'i'is 3 in last round and the
55 is at third index therefore 'i' will be 55 and 'j' becomes
44.now it check data[55]>data[44]. As it is true there will
be change ,the series is 11 22 33 44 55. It will exit from
second loop .Now 'i' is 4 BUT condition is(i=0;i<=size-2;i++) therefore it is false and execution will stop here.THE GIVEN SERIES IS SORTED SERIES . */

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!