Data Structures and Algorithms - #5
Data Structures and Algorithms - #5
Implementation for
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertion_sort(arr, n);
return 0;
}
output
Original array: 12 11 13 5 6
Sorted array: 5 6 11 12 13
Question 1: Sorting with Insertion Sort and Output Analysis
1.Write a C++ function called insertion_sort that sorts an array of integers in
ascending order.
2.Use the function to sort the following array: {34, 7, 23, 32, 5, 62}.
3.Print the array before and after sorting.
4.Track the number of comparisons and shifts made during the sorting process, and print
these values after sorting.
Original array: 34 7 23 32 5 62
Sorted array: 5 7 23 32 34 62
Comparisons: [total comparisons]
Shifts: [total shifts]