calebsimss
calebsimss

Reputation: 9

Sorting Arrays C++

Code Excerpt From a C++ Book Showing How to Sort Arrays:

void sort(int array[], int size)
{
    for(int i= 0; i < size; i++)
    {
        int index = findSmallestRemainingElement(array, size, i);
        swap(array,i,index);
    }
}
int findSmallestRemainingElement(int array[], int size, int index)
{
    int index_of_smallest_value = index;
    for (int i = index + 1; i < size; i++)

    {
        if (array[i] < array[index_of_smallest_value])
        {
            index_of_smallest_value= i;
        }
    }
    return index_of_smallest_value;
}
void swap(int array[], int first_index, int second_index)
{
    int temp=array[first_index];
    array[first_index] = array[second_index];
    array[second_index] = temp;
}

The book did not really explain this part very well, so I am left with a few questions about functions.

What is the difference between the size and index of an array?

The answer to that question will probably help me understand these functions a lot more, but in case it doesn't...

How exactly do the findSmallestRemainingElement(int array[], int size, int index) function and the void swap(int array[], int first_index, int second_index) functions work?

Obviously I know the overall purpose of each function, I just do not understand the line by line of it and the different things the code is doing.

Thank you to anyone who takes the time to help me understand this!

Upvotes: 0

Views: 714

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

What is the difference between the size and index of an array?

Arrays are declared by specifying the number of elements or the number of elements can be determined from the number of initializers used in the array declaration.

For example

int a1[10];

the array a1 is declared with 10 elements.

Or

int a2[] = { 1, 2, 3, 4, 5 };

The array a2 is declared with 5 elements.

In the context of your functions the size of an array is the number of elements in the array.

So the function sort declared like

void sort(int array[], int size);

can be called like

sort( a1, 10 );

or

sort( a2, 5 );

Though you may specify less number of elements than its actual size if you want to sort only a part of an array.

To access elements of an array you can use the subscipt operator. For example to output elements of the array a2 you can write

for ( int i = 0; i < 5; i++ )
{
    std::cout << a[i] << ' ';
}
std::cout << '\n';

So the variable i used in the loop place the role of the index to access a concrete element of the array.

How exactly do the findSmallestRemainingElement(int array[], int size, int index) function and the void swap(int array[], int first_index, int second_index) functions work?

For example the function swap declared like

void swap(int array[], int first_index, int second_index);

is used to swap values of two elements of an array.

For example if you want to swap the first and the last element of the array a2 shown above then you can call the function like

swap( a2, 0, 4 );

passing to the function apart from the array itself two indices of the target elements.

The function definition can look for example like

void swap(int array[], int first_index, int second_index)
{
    int temp = array[first_index];
    array[first_index] = array[second_index];
    array[second_index] = temp;
}

As for the function sort itself then it uses the selection sort method. It based on finding the index of a minimal element in the array and swaps the value of the first element with the value of the minimal element. Then the same is being done for the second element of the array and so on.

Upvotes: 1

Emily
Emily

Reputation: 1096

What is the difference between the size and index of an array?

The size of an array is the amount of elements in it. Although, it is to be noted that in C++ this can also refer to the amount of bytes the array uses up, which is why the term "length" is used more commonly, but this is not the case here.

You seem to be under the impression that an index is a property of an array, which is not true. An array's indices are a sequential numbering of its elements from left to right. So, a[0] is the 0th element ("element at index 0"), a[1] is the 1st element (index 1), etc.

The answer to that question will probably help me understand these functions a lot more, but in case it doesn't...

It probably will, so I'm going to leave it at that.

Upvotes: 0

Related Questions