Skip to content

Instantly share code, notes, and snippets.

@rmccullagh
Created October 4, 2013 18:08
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 rmccullagh/6830113 to your computer and use it in GitHub Desktop.
Save rmccullagh/6830113 to your computer and use it in GitHub Desktop.
selection sort
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int ar[] = {64, 25, 12, 22, 11};
int a = 0;
cout << "Before: " << endl;
while(a < 5)
{
cout << ar[a] << " ";
a++;
}
int i, j, min;
int * p = ar;
p = ar;
for(i = 0; i < 5-1; i++)
{
// hold a copy of of i
// the current minimum value
min = i;
for(j = i+1 ; j < 5; j++)
{
// is array at subscript j < array at ss min
if(ar[j] < ar[min] )
{
//min = j;
//*(ar + i) = min;
//cout << ar[j] <<k endl;
min = j;
}
}
// min is the index of the minimum element
if(min != i)
{
// swap(ar[i], ar[min]);
// *(p + i) = ar[min];
int t = ar[i];
p = ar + i;
*p = ar[min];
p = ar + min;
*p = t;
// *(p + i) = &ar[min];
}
// ar[i] = *(ar + min);
}
cout << endl << "After:" << endl;
for(int ii = 0; ii < 5; ii++)
cout << ar[ii] << " ";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment