Created
October 4, 2013 18:08
-
-
Save rmccullagh/6830113 to your computer and use it in GitHub Desktop.
selection sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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