Skip to main content

Insertion sort, bubble sort or Selection sort Using templates


    Use generic class to sort a list of elements. Give user the option to perform sorting using Insertion sort, bubble sort or Selection sort. Use templates.

#include<iostream>
#include<limits>

using namespace std;

template <class T>
class sorting
{
T a[10];
public:
void get_item()
{
for(int i=0;i<10;i++)
{
cout<<"\n a["<<i<<"] = ";
cin>>a[i];
}
}


void sel_sort()
{
T temp;

for(int i=0;i<10;i++)
{
for(int j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}

void bub_sort()
{
T temp;

for(int i=0;i<10;i++)
{
for(int j=0;j<10-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

void inst_sort()
{
T tmp;
int j;

for(int i=1;i<10;i++)
{
tmp=a[i];
j=i-1;
while(tmp<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=tmp;
}
}


void display()
{
for(int i=0;i<10;i++)
{
cout<<" "<<a[i]<<", ";
}
cout<<"\n\n";
}

};// End of Class


void main()
{
// Creating Integer Array using Template
sorting<int> iarr;

cout<<"\n\n Enter Elements of Integer Array\n";
iarr.get_item();
cout<<"\n\n Elements of Integer Array\n";
iarr.display();
iarr.sel_sort();
cout<<"\n\n After Selection Sorting\n Elememts of Integer Array\n";
iarr.display();
iarr.bub_sort();
cout<<"\n\n After Bubble Sorting\n Elememts of Integer Array\n";
iarr.display();
iarr.inst_sort();
cout<<"\n\n After Insertion Sorting\n Elememts of Integer Array\n";
iarr.display();

// Creating Double Array using Template
sorting<double> darr;
cout<<"\n\n Enter Elements of Double Array\n";
darr.get_item();
cout<<"\n\n Elements of Double Array\n";
darr.display();
darr.sel_sort();
cout<<"\n\n After Selection Sorting\n Elememts of Double Array\n";
darr.display();
darr.bub_sort();
cout<<"\n\n After Bubble Sorting\n Elememts of Double Array\n";
darr.display();
darr.inst_sort();
cout<<"\n\n After Insertion Sorting\n Elememts of Double Array\n";
darr.display();

system("pause");
}


Output:-










Comments

Post a Comment

Popular posts from this blog

Main menu dropdown parent item clickable in Drupal 8 using Bootstrap 4

 Edit menu--main.html.twig template file {# /**  * @file  * Bootstrap Barrio's override to display a menu.  *  * Available variables:  * - menu_name: The machine name of the menu.  * - items: A nested list of menu items. Each menu item contains:  *   - attributes: HTML attributes for the menu item.  *   - below: The menu item child items.  *   - title: The menu link title.  *   - url: The menu link url, instance of \Drupal\Core\Url  *   - localized_options: Menu link localized options.  *   - is_expanded: TRUE if the link has visible children within the current  *     menu tree.  *   - is_collapsed: TRUE if the link has children within the current menu tree  *     that are not currently visible.  *   - in_active_trail: TRUE if the link is in the active trail.  */ #} {% import _self as menus...

Add JS to the bottom of the page in Drupal 7

How to add JS at bottom of a particular page in Drupal 7 Step 1: Get the page id. In case of front page "is_front"  Step 2: In template.php file of your theme add the below code : function illume_preprocess_page(&$variables) { if ($variables['is_front']) { drupal_add_js(path_to_theme().'/js/util.js', array('type' => 'file', 'scope' => 'footer')); drupal_add_js(path_to_theme().'/js/main.js', array('type' => 'file', 'scope' => 'footer')); drupal_add_js(path_to_theme().'/js/slideimage.js', array('type' => 'file', 'scope' => 'footer')); $variables['bottom_scripts'] = drupal_get_js(); } } This way you can add JS file using drupal_add_js() and define scope as footer. Step 3: Now you can use bottom_script variable to the page tpl file where you want to add JS print $bottom_scripts;

Use Case Diagram for Online Book Store