Skip to content

Instantly share code, notes, and snippets.

@samair
Created October 3, 2016 05:46
Show Gist options
  • Save samair/14b1ffec8c209020da5050566306b6e6 to your computer and use it in GitHub Desktop.
Save samair/14b1ffec8c209020da5050566306b6e6 to your computer and use it in GitHub Desktop.
Sorting Alorithms
#include <iostream>
using namespace std;
void swap(int *a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void selection_sort( int a [], int size)
{
// First check if the element is smaller than previous ones
for (int i =1; i<size;++i)
{
for(int j=i; j>=0 ; --j)
{
if(a[j]< a[j-1])
{
swap(&a[j],&a[j-1]);
}
}
}
}
void printArray(int a[], int size)
{
for(int i=0; i<size; ++i)
{
cout<<a[i]<<":";
}
cout<<endl;
}
int main()
{
int arr[] = {9,4,5,10,27,1,9};
printArray(arr,7);
selection_sort(arr,7);
printArray(arr,7);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment