在函数中处理了变量后,变量的数组值将永远更改。可以将变量值恢复正常吗?在这种情况下,我使用一个函数对数组中的随机数据进行排序。下面是我的代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;
}
}
swap(&arr[min_idx], &arr[i]);
}
}
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
swap(&arr[j], &arr[j + 1]);
}
}
}
}
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
{
L[i] = arr[l + i];
}
for (j = 0; j < n2; j++)
{
R[j] = arr[m + 1 + j];
}
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main()
{
int select;
char req;
int arr[] = {20, 45, 30, 25, 15, 50, 100, 55, 10, 25};
int arr_size = sizeof(arr) / sizeof(arr[0]);
do
{
system("clear");
cout << "--- Sorting Menu ---\n\n";
cout << "1. Selection Sort\n";
cout << "2. Insertion Sort\n";
cout << "3. Bubble Sort\n";
cout << "4. Quick Sort\n";
cout << "5. Merge Sort\n\n";
cout << "Choose (1/2/3/4/5) : ";
cin >> select;
switch (select)
{
case 1:
system("clear");
cout << "Selection Sort\n\n";
cout << "Random data : ";
printArray(arr, arr_size);
selectionSort(arr, arr_size);
cout << "Sorted data : ";
printArray(arr, arr_size);
break;
case 2:
system("clear");
cout << "Insertion Sort\n\n";
cout << "Random data : ";
printArray(arr, arr_size);
insertionSort(arr, arr_size);
cout << "Sorted data : ";
printArray(arr, arr_size);
break;
case 3:
system("clear");
cout << "Bubble Sort\n\n";
cout << "Random data : ";
printArray(arr, arr_size);
bubbleSort(arr, arr_size);
cout << "Sorted data : ";
printArray(arr, arr_size);
break;
case 4:
system("clear");
cout << "Quick Sort\n\n";
cout << "Random data : ";
printArray(arr, arr_size);
quickSort(arr, 0, arr_size - 1);
cout << "Sorted data : ";
printArray(arr, arr_size);
break;
case 5:
system("clear");
cout << "Merge Sort\n\n";
cout << "Random data : ";
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
cout << "Sorted data : ";
printArray(arr, arr_size);
break;
default:
break;
}
cout << endl
<< endl;
cout << "Back to menu ? (Y/n) : ";
cin >> req;
if (req != 'Y' || req != 'y')
{
system("clear");
cout << "Exit from this program...\n\n";
}
} while (req == 'Y' || req == 'y');
return 0;
}
答案 0 :(得分:1)
在函数中处理了变量后,变量的数组值将永远更改。可以将变量值恢复为正常值吗?
在调用任何函数之前简单地复制它们:
std::vector<int>(arr_size) arr_copy;
std::copy(std::begin(arr), std::end(arr), std::begin(arr_copy));
switch (select)
{
case 1:
system("clear");
cout << "Selection Sort\n\n";
cout << "Random data : ";
printArray(arr, arr_size);
selectionSort(arr, arr_size);
cout << "Sorted data : ";
printArray(arr, arr_size);
,然后将其复制回去:
std::copy(std::begin(arr_copy), std::end(arr_copy), std::begin(arr));
break;