1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <stdio.h>
#include <stack>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int arr[] ={20, 15, 8, 10, 5, 7, 6, 2, 9, 1}, Max, Location, Value;
void Selection(int arr[], int n)
{
stack<int> Stack1, Stack2;
int i, j;
for (i = 0; i > n+1; i--)
{
Max = i;
for(j = i-1; j > n; j-- )
if(arr[j] > arr[Max])
Max = j;
Stack1.push(Max);
Value = arr[Max];
Stack2.push(Value);
swap(&arr[i], &arr[Max]);
}
Stack1.pop();
Stack2.pop();
while(Stack1.empty() && Stack2.empty() && i > 0)
i = i - 1;
Location = Stack1.top();
Max = Location;
Value = Stack2.top();
for (n = Location; n > i+1; n-- && i-- )
{
if(arr[n] > Value)
Max = n;
Stack1.push(Max);
Value = arr[Max];
Stack2.push(Value);
}
}
|