#include <iostream> #include <ctime> using namespace std; void MergeSort(int arr[], int…
- User
#include #include
using namespace std;
void MergeSort(int arr[], int low, int high);
void Merge(int arr[], int low, int mid, int high) { //Use 'temp' as an array of size high-low+1, for temporary memory.
//If you haven't taken 103 yet, you are not expected to understand this. int *temp = new int[high-low+1]; int i = low, j = mid+1; int k = 0;while (i <= mid && j <= high){ if (arr[i] < arr[j]){ temp[k] = arr[i]; i++; } else{ temp[k] = arr[j]; j++; } k++; } while (i <= mid){ temp[k++] = arr[i++]; } while (j <= high){ temp[k++] = arr[j++]; } for (int k=0; k < high-low+1;k++){ arr[k+low] = temp[k]; } //Your code here. delete[] temp;}
void MergeSort(int arr[], int low, int high) { if (low < high){ int mid = (high - low) / 2; MergeSort(arr, low, mid); MergeSort(arr, mid + 1, high); Merge(arr,low, mid, high);
} }//Do NOT change the main function or InsertionSort. //But if you do anyway for testing purposes, make sure to change it back //and VERIFY you still pass the submission tests.
void InsertionSort(int arr[], int len) { for (int i = 1; i < len; i++) { int j = i; while (j > 0 && arr[j] < arr[j-1]) { int temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = temp; j--; } } }
int main(int argc, char* argv[]) { int n; clock_t start; double durationI = 0, durationM = 0;
cin >> n; int *arr = new int[n]; int *backup = new int[n]; for (int i = 0; i < n; i++) { if (n < 100) cin >> arr[i]; else arr[i] = n-i; backup[i] = arr[i]; } start = clock(); MergeSort(arr, 0, n-1); durationM += ( clock() - start ) / (double) CLOCKS_PER_SEC; if (n < 100) { for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; } for (int j = 0; j < n; j++) arr[j] = backup[j]; start = clock(); InsertionSort(arr, n); durationI += ( clock() - start ) / (double) CLOCKS_PER_SEC; if (n >= 100) { cout << "MergeSort on " << n << " elements took " << durationM << " seconds." << endl; cout << "InsertionSort took " << durationI << " seconds." << endl; } delete [] arr; delete [] backup; return 0;}
what problem with merge sort function that cause segmentation fault for this program when run with: 8 1 2 3 4 5 6 7 8
- Assistant
The segmentation fault in your
MergeSortfunction is due to incorrect calculation of the midpoint in the recursive calls. The line:int mid = (high - low) / 2;does not correctly compute the midpoint in the context of a recursion that deals with different ranges of the array. This calculation only considers the distance between
highandlowand divides it by 2. However, it does not account for the actual position within the array, leading to incorrect indices being used in the recursive calls and merges.To correctly calculate the midpoint in a way that is useful for dividing the array into two halves for the recursive algorithm, you should compute the midpoint as:
int mid = low + (high - low) / 2;This calculation ensures that you correctly find the midpoint relative to the current
lowindex, leading to proper segmentation of the array for recursive sorting.Therefore, the corrected function would be:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.