Hi I’m at part 10 of the intermediate c++ course (sorting arrays), and getting an error when calling the “sort” function inside the main can you help? Thanks
Are you using namespace std? And what are your imports?
Use function sizeof to get the size of an array.
there is no size() function
you should use the ‘size’ parameter that you supply
I dont think sizeof() is a good or efficient choice
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.
You can try this code:
void selection_sort(int arr[], int n) {
int i, j, min_idx;
// Traverse through all array elements
for (i = 0; i < n - 1; i++) {
// Find the minimum element in the unsorted part of the array
min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
You can run the code, and hope this will help you. I also found this article about the bubble sort on the internet.