枚举算法:找到数组中最大值
int array[5] = {
300,350,200,400,250 };
int max = 0;
for (int i = 0; i < 5; i++)
{
if (array[i] > max)
{
max = array[i];
}
}
cout << max;
倒置算法:数组的倒置
int array[5] = {
1,2,3,4,5 };
for (int i = 0; i < 5; i++)
{
cout << array[i] << endl;
}
int start = 0;
int end = sizeof(array) /sizeof(array[1])-1;
int temp=0;
while (start<=end)
{
temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
for (int i = 0; i < 5; i++)
{
cout << array[i] << endl;
}
排序算法:冒泡排序
int array[9] = {
4,2,8,0,5,7,1,3,9 };
cout << "排序前:" << endl;
for (int i = 0; i < 9; i++)
{
cout << array[i] << "\t";
if (i == 8)
cout << endl;
}
for (int i = 0; i < 9-1; i++)
{
for (int j = 0; j < 9 - i - 1; j++)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
for (int i = 0; i < 9 ; i++)
{
cout<< array[i] << "\t";
if (i == 8)
cout << endl;
}