selection sort problem

I am just learning the selection sort algorithm and I cannot seem to understand how this selection sort works. Its supposed to take the largest index and sort it. Any help would be appreciated!
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
int findLargestIndex( int a[], int size )
{
        int index = 0;
        for ( int i = 0; i < size; i++ )
        {
                if ( a[i] > a[index] )
                {
                        index = i;
                }
        }
        return index;

}
void swap( int &a, int &b )
{
        int tmp = a;
        a = b;
        b = tmp;
}
void sortMaxIndex( int a[], int size )
{
        int last = size - 1;

        for ( int i = 0; i < size; i++ )
        {

                for ( int j = 0; j <= last; j++ )
                {
                        int max_index = findLargestIndex(a, size);
                        swap( a[last], a[max_index] );
                }

        }

}
Last edited on
Topic archived. No new replies allowed.