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

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 - ...

Software Testing through Fuzzing

Fuzz testing or fuzzing is a software testing technique used to discover coding errors and security loopholes in software, operating systems or networks by inputting massive amounts of random data, called fuzz, to the system in an attempt to make it crash . Fuzzing is  often automated or semi-automated, that  involves providing invalid, unexpected, or random data to the inputs of a computer program. The program is then monitored for exceptions such as crashes, or failing built-in code assertions or for finding potential memory leaks. Fuzzing technique is commonly used to test for security problems in software or computer systems ans also used to discover coding errors and security loopholes in software, operating systems  or networks by inputting massive amounts of random data, called fuzz, to the system  in an attempt to make it crash. If a vulnerability is found, a tool called a fuzz tester (or fuzzer), indicates potential causes. There are two forms ...

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++)     { ...