Rewrite the straight insertion sort from chapter 8 using pointer arithmetic. The
ID: 3536536 • Letter: R
Question
Rewrite the straight insertion sort from chapter 8 using pointer arithmetic. The data to be sorted are to be read from a file. The array is to be dynamically allocated in the heap after reading the file to determine the number of elements. While reading the data to determine the size of array you will require, print them 10 integers to a line. Use the test data shown below.
The data will be sorted as they are read into the array. Do not fill the array and then sort the data. after the array has been sorted, print the data again using the same format you used for the unsorted
Explanation / Answer
void insertionSort(int arr[], int length) {
int i, j, tmp;
for (i = 1; i < length; i++) {
j = i;
while (j > 0 && arr[j - 1] > arr[j]) {
tmp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = tmp;
j--;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.