WELCOME TO MY BLOG BY SHABEEB.K

C,C++ PROGRAMMING: 11/08/13

Search This Blog

Friday, 8 November 2013

BUBBLE SORT


 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream.h>
#include<conio.h>
int j;
class sorting
{
int a[500];
int i;
int temp;
int size;
public:

void in()
{
cout<<"ENTER ARRAY SIZE\n";  //instruction to the user to specify array size.
cin>>size;

cout<<"ENTER ELEMENT\n";    //programm to insert element in the array until array size full;
for(i=0;i<size;i++)
{
cin>>a[i];
}
}

void search()
{
j++;                   //increment j by 1.
for(i=0;i<size;i++)
{
if(a[i]<=a[i-1])         //swap values if a[i]<=a[i-1].         
{

temp=a[i]+a[i-1];             

a[i]=temp-a[i];

a[i-1]=temp-a[i-1];
}
}
}

void dis()          //function to display each sorted elements.
{

cout<<"\n";
cout<<"sorting: "<<j<< "-> ";

for(i=0;i<size;i++)
{
cout<<a[i]<<",";
}


while(j<size)        //continue search and display function to sort the all elements complietly untile j<size
{
search(); dis();        
}



}
};

void main()      //calling functions
{
clrscr();
sorting s;
s.in();
s.search();
s.dis();

getch();
}