与排序数组 C++ 的概念差距
Conceptual gap with sorting arrays C++
从 Jumping into C++ 数组排序中寻找一些帮助理解此示例代码中使用的方法。至此,作者从传递数组到函数的基础知识跳到了这一点,并且步骤不清楚。如果有人可以帮助解释 findSmallestRemainingElement() 和 swap() 函数中发生了什么?还有索引是怎么回事?这是各种纷繁复杂的世界。我觉得我需要重新开始。
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int findSmallestRemainingElement (int array[], int size, int index);
void swap (int array[], int first_index, int second_index);
void sort (int array[], int size)
{
for (int i = 0; i < size; i++)
{
int index = findSmallestRemainingElement (array, size, i); //Why are there not [] for array here?
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;
}
void displayArray (int array[], int size)
{
cout << "{";
for (int i = 0; i < size; i++)
{
if (i != 0)
{
cout << ", ";
}
cout << array[i];
}
cout << "}";
}
int main()
{
int array[10];
srand(time(NULL));
for (int i = 0; i < 10; i++)
{
array[i] = rand() % 100;
}
cout << "Original array: ";
displayArray(array, 10);
cout << "\n";
sort(array, 10);
cout << "Sorted array: ";
displayArray(array, 10);
cout << "\n";
}
请允许我尝试用一个现实生活中的例子来解释。
假设您想在 class 中找到最重的孩子(我知道这有点意思)。问题是你的记忆力真的很差(一次只能记住一个孩子的体重)。你会怎么做?
好吧,首先让您 class 中的所有孩子排成一行,并在他们的额头上画一个数字(他们的索引)。第一个孩子的索引为 0,直到最后一个孩子的索引为 n。这基本上就是您的阵列。
但是他们额头上的数字并不能告诉您孩子的体重……很明显。为此,您需要一个秤。所以,要 'look up' 每个孩子的体重,你叫他们的索引号,他们走上体重秤,你就得到了他们的体重。这是通过索引号访问数组元素。
那么,下一步是什么?好吧...称孩子'0'。他是迄今为止最重的吗?好吧,他是第一个孩子,所以显然是的。所以请记住您脑海中的索引“0”。
现在,呼叫下一个编号的孩子(孩子“1”),并通过让他站在体重秤上来获取他的体重。在执行此操作时,用第二个秤称量您记得的编号为“0”的孩子。如果新孩子的体重比你记得的孩子高,那么忘记旧孩子的指数并记住新孩子的指数。
继续这样做,直到你读完所有的孩子,然后脱口而出你记得的孩子的编号。这是函数 return.
'swap' 函数我会留给其他人回答。
从 Jumping into C++ 数组排序中寻找一些帮助理解此示例代码中使用的方法。至此,作者从传递数组到函数的基础知识跳到了这一点,并且步骤不清楚。如果有人可以帮助解释 findSmallestRemainingElement() 和 swap() 函数中发生了什么?还有索引是怎么回事?这是各种纷繁复杂的世界。我觉得我需要重新开始。
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int findSmallestRemainingElement (int array[], int size, int index);
void swap (int array[], int first_index, int second_index);
void sort (int array[], int size)
{
for (int i = 0; i < size; i++)
{
int index = findSmallestRemainingElement (array, size, i); //Why are there not [] for array here?
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;
}
void displayArray (int array[], int size)
{
cout << "{";
for (int i = 0; i < size; i++)
{
if (i != 0)
{
cout << ", ";
}
cout << array[i];
}
cout << "}";
}
int main()
{
int array[10];
srand(time(NULL));
for (int i = 0; i < 10; i++)
{
array[i] = rand() % 100;
}
cout << "Original array: ";
displayArray(array, 10);
cout << "\n";
sort(array, 10);
cout << "Sorted array: ";
displayArray(array, 10);
cout << "\n";
}
请允许我尝试用一个现实生活中的例子来解释。
假设您想在 class 中找到最重的孩子(我知道这有点意思)。问题是你的记忆力真的很差(一次只能记住一个孩子的体重)。你会怎么做?
好吧,首先让您 class 中的所有孩子排成一行,并在他们的额头上画一个数字(他们的索引)。第一个孩子的索引为 0,直到最后一个孩子的索引为 n。这基本上就是您的阵列。
但是他们额头上的数字并不能告诉您孩子的体重……很明显。为此,您需要一个秤。所以,要 'look up' 每个孩子的体重,你叫他们的索引号,他们走上体重秤,你就得到了他们的体重。这是通过索引号访问数组元素。
那么,下一步是什么?好吧...称孩子'0'。他是迄今为止最重的吗?好吧,他是第一个孩子,所以显然是的。所以请记住您脑海中的索引“0”。
现在,呼叫下一个编号的孩子(孩子“1”),并通过让他站在体重秤上来获取他的体重。在执行此操作时,用第二个秤称量您记得的编号为“0”的孩子。如果新孩子的体重比你记得的孩子高,那么忘记旧孩子的指数并记住新孩子的指数。
继续这样做,直到你读完所有的孩子,然后脱口而出你记得的孩子的编号。这是函数 return.
'swap' 函数我会留给其他人回答。