Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Programming No Strings/Pointers USER DEFINED FUNCTIONS IS A REQUIREMENT. Can U

ID: 3540066 • Letter: C

Question

C Programming


No Strings/Pointers


USER DEFINED FUNCTIONS IS A REQUIREMENT. Can Use arrays/selection/logical statements/loops etc.


Problem: The Move Sort algorithm will move an identified element to the rear of the array. All elements in between the selected element and the end of the data set will move one position to the left (an index one less than its previous position in the array). Unlike the Selection, Bubble, and Insertion sorting algorithms the Move Sort algorithm will terminate immediately when the array is sorted. There will be no passes through the Move Sort algorithm that fails to make any moves.

Given a data set of non-negative integers apply the Move Sort algorithm to order the data from smallest to largest in as few moves as possible. The maximum number of elements input is 20, but the user can enter a -1 to indicate that no more input will follow if less than 20 elements is desired.


Note: Input will always be non-negative, but elements in the data set do not have to be unique


Example Execution #1:

Number of moves: 4

Description of Example Execution #1:

>>>The first move identifies that the 3 must be sent to the rear of the array. All other elements shift one position to the left.

>>>The second move identifies that the 4 must be sent to the rear of the array. The 6, 2, and 3 elements move one position to the left.

>>>The third move identifies that the 5 must be sent to the rear of the array and all other elements shift one position to the left.

>>>The fourth and final move identifies that the 6 must move to the rear of the array and elements 2, 3, 4, and 5 move one position to the left.


Example Execution #2:

Number of moves: 4


Example Execution #3: Entire array filled, no -1 needed to terminate input.

Number of moves: 11


I need the code. Will award full points and stars.


Explanation / Answer

#include<stdio.h>
//#include<conio.h> //uncomment this line if u r using compiler other than gcc

int main()
{
    int arr[20]={0},N=0,temp=0,m=1,i=0,j=0,k=0,p=0;
    for(i=0;i<20;i++)
    {
        printf(" Enter data #%d ::",i+1);
        scanf("%d",&arr[i]);
        if(arr[i]==-1)
        {
            N=i;
            break;
        }
    }
    for(i=0;i<N-1;i++)
    {
        for(j=N-1;j>i;j--)
        {
            if(arr[i]>=arr[j])
            {
                temp=arr[i];
                for(k=i;k<j;k++)
                {
                    arr[k]=arr[k+1];
                }
                arr[j]=temp;
                printf(" After move #%d ::",m++);
                for(p=0;p<N;p++)
                {
                    printf(" %d",arr[p]);
                }
            }
        }
    }
    printf(" Number of moves :: %d ",m-1);
    getch(); //uncomment this line if u r using compiler other than gcc      
    return 0;
}