Skip to main content

Selection Sort Program in C++

Selection sort 

Basic Concept

The time complexity of selection sort is O(n2), for best, average, and worst case scenarios.
Selection sort is one of the basic algorithms for sorting data, its simplicity proves useful for sorting small amounts of data. Selection sort works by first starting at the beginning array (index 0) and traverses the entire array comparing each value with the current index, if it is smaller than the current index than that index is saved. Once the loop has traversed all the data and if a smaller value than the current index was found a swap is made between the current index in the index where the smaller value was found. The current index is then incremented, now to index 1, the algorithm repeats.








PROGRAM

#include<iostream>
using namespace std;
template <class T>         // The template class is used to have flexibility in the type of data, whether                                                       integer, float, or character it will work for all

void s_sort(T a[],int n)
{
     int i,j,t;
     for(i=0;i<n;i++)
     {
                     for(j=i+1;j<n;j++)
                     {
                                       if(a[j]<a[i]) //for descending order use if(a[j]>a[i])
                                       {
                                                    t=a[i];
                                                    a[i]=a[j];
                                                    a[j]=t;
                                       }
                     }
     }
}


int main()
{
    int a[100],i,n;
    cout<<"Enter The number of Element:";
    cin>>n;
    cout<<"\nEnter Elements\n";
    for(i=0;i<n;i++)
    {

                      cout<<"\nEnter:";
                    cin>>a[i];
    }
    s_sort(a,n);
    cout<<"\nAfter Sorting\n";
    for(i=0;i<n;i++)
    {
                    cout<<a[i]<<"\t";
    }
    getch();
    return 0;
}


OUTPUT (screenshot):





Comments

Popular posts from this blog

How to create Bootable USB using Rufus

Rufus is a great software that allows you to create a bootable USB drive using an .ISO file.Its Ideal for installing programs and software on windows for whom CD drive are not available. In order to create a bootable USB drive you will need three things : 1 USB drive 2 Your .ISO file which you want to boot your USB with. 3. Rufus Let's get going!! STEP 1 First of all download Rufus , you can download it from their site -  https://rufus.akeo.ie/ Download the latest version (right now its Rufus 2.9). You don't need to install it , it runs directly STEP 2 : Inser the USB drive and open Rufus , make sure you take backup of all files in your USB drive because Rufus will format the USB before booting it. STEP 3: Once you have started Rufus check for all desired things. Make sure device field is set to the correct device(device that you want to boot i.e USB) Also Make Sure "Create a bootable disk " option is ticked else i...

SQL Injection ,Hacking PHP 4.4 sites in seconds

Today I am going to teach you how to hack a certain type of websites with very least efforts. Websites with PHP  4.4 have a SQL injection vulnerability in them which makes their Admin control panel easily accessible,and in just few steps you will access the admin's account of that website. Remember,this tutorial is applicable on PHP 4.4 machines with Apache running in parallel with them. Also,since I will be hacking REAL websites,I will not be displaying their URL’s or else I will be sued!!!. Also this tutorial is only for educational purpose. Here we go!!! Step 1 – Search for them Yep,make a Google dork to find sites running Apache and PHP 4.4 . Its quite easy.You can do this by searching inurl:adminlogin . Step 2 – Scan them Start by scanning them using Nmap ,Do and intense scan and find the open ports. If you find port 2000 open,then you have almost got it. most websites running PHP4.4 have this port for admin login. Now just login using port 2000 ie - ...

Bellmanford Algorithm C++ Program

The  Bellman–Ford algorithm  is an  algorithm  that computes  shortest paths  from a single source  vertex  to all of the other vertices in a  weighted digraph . It is slower than  Dijkstra's algorithm  for the same problem, but more versatile, as it is capable of handling graphs in which some of the edge weights are negative numbers. Here is the Source Code: #include<iostream> #include<stdio.h> using namespace std; #include<conio.h> #define INFINITY 999 struct node {     int cost;     int value;     int from; }a[5]; void addEdge(int am[][5],int src,int dest,int cost) {      am[src][dest] = cost;      return; } void bell(int am[][5]) {     int i, j, k, c = 0, temp;     a[0].cost = 0;     a[0].from = 0;     a[0].value = 0;     for (i = 1; i < 5; i++)     { ...