//C++ program for Bubble Sort Algorithm
#include <iostream>
#include <stdbool.h>
#define MAX 4
using namespace std;
double l[MAX] = {3.12,7.777,0.999,4.444};
void display()
{ int i; cout << "[";
for(i = 0; i < MAX; i++)
{ cout << l[i] << " ";}
cout << "]" << endl;
}
void bubbleSort() { double temp;
int i,j; bool swapped = false;
for(i = 0; i < MAX-1; i++) { swapped = false;
for(j = 0; j < MAX-1-i; j++) {
if(l[j] > l[j+1]) { temp = l[j]; l[j] = l[j+1];l[j+1] = temp; swapped = true;}
}
}
int main() { cout<<"Input Array:"; display(); cout<<"\n";
bubbleSort(); cout<<"\nOutput Array: "; display();
}
#include <iostream>
using namespace std;
template<class T>
class vector{ T *v; int size;
public: vector(int m){ v=new T[3]; for(int i=0;i<3;i++) v[i]=0; cout<<"\n Vector initialized..";}
vector(T *a){ for(int i=0;i<3;i++) v[i]=a[i]; }
void show(void){ for(int i=0;i<3;i++) cout<<v[i]<<" "; cout<<endl;}
};
int main()
{ int x[]={100,2,3};
float y[]={1.1,2.2,3.3};
vector <int> v1(3);
vector <float> v2(3);
v1=x;
cout<<"\n Vector Elements of V1 : ";
[Link]();
cout<<"\n Vector Elements of V2 : ";
v2=y;
[Link]();
return 0;
}
-----------------
#include <iostream>
#include <stdbool.h>
#define MAX 4
using namespace std;
char l[MAX] = {'z','a','B','V'};
void display() { int i; cout << "[";
for(i = 0; i < MAX; i++) { cout << l[i] << " ";} cout << "]" << endl;
}
template <class T>
void bubbleSort() { T temp; int i,j; bool swapped = false;
for(i = 0; i < MAX-1; i++) { swapped = false;
for(j = 0; j < MAX-1-i; j++) {
cout << " Items compared: [ " << l[j] << ", " << l[j+1] << " ] ";
if(l[j] > l[j+1]) { temp = l[j]; l[j] = l[j+1]; l[j+1] = temp;
swapped = true;
cout << " => swapped [" << l[j] << ", " << l[j+1] << "]" << endl;
}
else { cout << " => not swapped" << endl; }
}
if(!swapped) { break; }
cout << "Iteration " << (i+1) << "#: ";
display();
}
}
int main() { cout<<"Input Array:"; display(); cout<<"\n";
bubbleSort<char>(); cout<<"\nOutput Array: "; display();
}
g++ [Link]
admin1@admin1-Vostro-3480:~$ ./[Link]
Input Array:[z a B V ]
Items compared: [ z, a ] => swapped [a, z]
Items compared: [ z, B ] => swapped [B, z]
Items compared: [ z, V ] => swapped [V, z]
Iteration 1#: [a B V z ]
Items compared: [ a, B ] => swapped [B, a]
Items compared: [ a, V ] => swapped [V, a]
Iteration 2#: [B V a z ]
Items compared: [ B, V ] => not swapped
Output Array: [B V a z ]
g++ [Link]
Vector initialized..
Vector initialized..
Vector Elements of V1 : 100 2 3
Vector Elements of V2 : 1.1 2.2 3.3