Modify the following program to count the # of data moves needed to order an arr
ID: 3633870 • Letter: M
Question
Modify the following program to count the # of data moves needed to order an array of 100 random numbers. Data move is any movement of an element of data from one position in the array to another, to a hold area, or from a hold area back to the array. Display array before and after sort as well as the total moves necessary to sort the array.#include <stdio.h>
#define MAX_ARY_SIZE 15
void insertionSort (int list[ ], int last );
int main ( void )
{
int ary[ MAX_ARY_SIZE ] = { 89, 72, 3, 15, 21,
57, 61, 44, 19, 98,
5, 77, 39, 59, 61 };
int i;
printf( "Unsorted array: ");
for (i = 0; i < MAX_ARY_SIZE; i++ )
printf( "%3d", ary[ i ] );
insertionSort ( ary, MAX_ARY_SIZE - 1);
printf( " Sorted array: " );
for (i = 0; i < MAX_ARY_SIZE; i++)
printf( "%3d", ary[ i ] );
printf( " " );
system("PAUSE");
return 0;
}
void insertionSort (int list[], int last)
{
int hold;
int walker;
int current;
for (current = 1; current <= last; current++)
{
hold = list[current];
for (walker = current - 1;
walker >= 0 && hold < list[walker];
walker--)
list[walker + 1] = list[walker];
list [walker + 1] = hold;
}
return;
}
Explanation / Answer
please rate - thanks
#include <stdio.h>
#define MAX_ARY_SIZE 15
int insertionSort (int list[ ], int last );
int main ( void )
{
int ary[ MAX_ARY_SIZE ] = { 89, 72, 3, 15, 21,
57, 61, 44, 19, 98,
5, 77, 39, 59, 61 };
int i,moves;
printf( "Unsorted array: ");
for (i = 0; i < MAX_ARY_SIZE; i++ )
printf( "%3d", ary[ i ] );
moves=insertionSort ( ary, MAX_ARY_SIZE - 1);
printf( " Sorted array: " );
for (i = 0; i < MAX_ARY_SIZE; i++)
printf( "%3d", ary[ i ] );
printf( " " );
printf("Total moves to sort the array %d ",moves);
system("PAUSE");
return 0;
}
int insertionSort (int list[], int last)
{
int hold;
int walker;
int current;
int moves=0;
for (current = 1; current <= last; current++)
{hold = list[current];
moves++;
for (walker = current - 1;walker >= 0 && hold < list[walker];walker--)
{list[walker + 1] = list[walker];
moves++;
}
list [walker + 1] = hold;
moves++;
}
return moves;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.