Skip to content

Instantly share code, notes, and snippets.

@samair
Created October 3, 2016 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samair/1b4b239c424d4b944710ea86dfe806a7 to your computer and use it in GitHub Desktop.
Save samair/1b4b239c424d4b944710ea86dfe806a7 to your computer and use it in GitHub Desktop.
Sorting Algorithms
#include <iostream>
int startPos1= 0;
using namespace std;
void swap(int a[], int from, int to)
{
if (from !=to)
{
cout<<"Before"<<endl;
cout<<"a["<<to<<"] :"<<a[to];
cout<<"a["<<from<<"] :"<<a[from];
cout<<endl;
a[to] = a[to] + a[from];
a[from] = a[to] - a[from];
a[to] = a[to] - a[from];
cout<<"After"<<endl;
cout<<"a["<<to<<"] :"<<a[to];
cout<<"a["<<from<<"] :"<<a[from];
cout<<endl;
}
}
void selection_sort(int array [],int startPos, int size)
{
// Input is an array
int leastElement = array[startPos];
int leastElementPos = startPos;
if (startPos < size )
{
for(int j = 0 ; j < size;j++)
{
for(int i = leastElementPos; i < (size) ; i++)
{
if ( leastElement > array[i+1] )
{
leastElement = array[i+1];
leastElementPos = i+1;
}
cout<<"least Element is :"<<leastElementPos<<endl;
}
}
swap(array,leastElementPos,startPos);
//selection_sort(array,startPos+1,size);
}
}
int main()
{
int arrayToSort [] = {9,4,7,6,8,1};
size_t sizeofa = sizeof(arrayToSort)/sizeof(int);
for(int i = 0; i < sizeofa; ++i)
{
cout<<":"<<arrayToSort[i];
}
selection_sort(arrayToSort,0,sizeofa);
//print array
for(int i = 0; i <sizeofa; ++i)
{
cout<<":"<<arrayToSort[i];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment