Hi I'm new to c++ I've written a selection sorting program can you help me and say in swap function why we put the stars(*), and in selectionSort we should use & in <<swap(&arr[min], &arr[i]);>> i mean cant we just say:
int newint = a;
a = b;
b = newint;
instead of:
int newint = *a;
*a = *b;
*b = newint;
main code:
using namespace std;
void swap(int *a, int *b);
void selectionSort(int arr[], int size);
void printArray(int arr[], int size);
int main() {
int arr[] = {20, 12, 10, 15, 2};
int size = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, size);
cout << "Sorted array in Acsending Order:\n";
printArray(arr, size);
}
// ================================================
// swap function to swap two element of the array
void swap(int *a, int *b){
int newint = *a;
*a = *b;
*b = newint;
}
// ================================================
// the selection function made of two main loop
void selectionSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
int min = i;
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[min])
min = j;
}
swap(&arr[min], &arr[i]);
}
}
// ================================================
// print function to show the final result
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}