Question

(In C language) Implement Shellsort and count the number of moves on input in a given...

(In C language) Implement Shellsort and count the number of moves on input in a given file data.

Please provide the answer in text format please.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
int shellsort(int arr[], int num)
{
int i, j, k, tmp,count=0;
for (i = num / 2; i > 0; i = i / 2)
{
for (j = i; j < num; j++)
{
for(k = j - i; k >= 0; k = k - i)
{
if (arr[k+i] >= arr[k])
break;
else
{
tmp = arr[k];
arr[k] = arr[k+i];
arr[k+i] = tmp;
count++;
}
}
}
}
return count;
}
int getData(int arr[],int count)
{
   FILE *myFile;
myFile = fopen("C:/Users/temp-01968/Desktop/arrayV3.txt", "r");

//read file into array
int i=0;

while(fscanf(myFile, "%d", &arr[i])!=EOF)
{
   i++;
   if(i==100)
   {
       return i;
   }
}
return i;
}
int main()
{
int arr[100];
int k, num=-1,temp=0;
num=getData(arr,100);
  
printf("\nArray is: \n");
for (k = 0; k < num; k++)
{
printf("%d ", arr[k]);
   }
  
temp=shellsort(arr, num);
  
printf("\n Sorted array is: \n");
for (k = 0; k < num; k++)
{
printf("%d ", arr[k]);
   }
   printf("\nIt took %d steps to sort the array.",temp);
  
return 0;
}

Add a comment
Know the answer?
Add Answer to:
(In C language) Implement Shellsort and count the number of moves on input in a given...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create an algorithm to count the number of 1’s in a 32-bit number. Implement the program in a high level language like...

    Create an algorithm to count the number of 1’s in a 32-bit number. Implement the program in a high level language like C or Java. It does not need to run for me, but the code should be included in a text document called FirstnameLastnameHLA3.txt along with your assignment submission. Implement the program in MIPSzy Assembly language. Use the high level code as comments to the right of the Assembly code as the textbook does. If you write that MIPSzy...

  • C++ LANGUAGE /** * Given an input string, count the number of words ending * in...

    C++ LANGUAGE /** * Given an input string, count the number of words ending * in 'y' or 'z' -so the 'y' in "heavy" and the 'z' in "fez" * count, but not the 'y' in "yellow". Make sure that your * comparison is not case sensitive. We'll say that a y * or z is at the end of a word if there is not an alphabetic * letter immediately following it. * * Do not use any string...

  • In the language c using the isspace() function: Write a program to count the number of...

    In the language c using the isspace() function: Write a program to count the number of words, lines, and characters in its input. A word is any sequence of non-white-space characters. Have your program continue until end-of-file. Make sure that your program works for the case of several white space characters in a row. The character count should also include white space characters. Run your program using the following three sets of input:             1. You're traveling through ​               another...

  • Design and implement a C Language program that measures the performance of given processors. There are...

    Design and implement a C Language program that measures the performance of given processors. There are several metrics that measure the performance of a processor. We will be using the following 3 measures: 1.CPI (clock cycles per instruction) = #clock cycles /#instructions 2.CPU execution time = #instructions x CPI x clock cycle time . cylce time = 1/CPU clock rate in hertz units 3.MIPS (mega instructions per second)= #instrucrions/ CPU X 1000000 Typically, processors’ performance is measured using a wide...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Using C++ language, write a code to implement the "evaluate function" described below using the given...

    Using C++ language, write a code to implement the "evaluate function" described below using the given poly.cpp file and poly.h header file. (ignore the other 3 functions if asked) poly.cpp file: poly.h header file: You are asked to implement four functions for a data structure used to store polynomials. The data structure used for polynomials is a linked list that consists of terms where each term consists of a coefficient, an exponent and a pointer to the next term. A...

  • Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global...

    Language is C++ NOTE: No arrays should be used to solve problems and No non-constants global variables should be used. PART B: (Statistics Program) – (50%) Please read this lab exercise thoroughly, before attempting to write the program. Write a program that reads the pairs of group number and student count from the text file (user must enter name of file) and: Displays the number of groups in the file (5 %) Displays the number of even and odd student...

  • Write a C program which computes the count of the prime and perfect numbers within a...

    Write a C program which computes the count of the prime and perfect numbers within a given limit Land U, representing the lower and upper limit respectively. Prime numbers are numbers that have only 2 factors: 1 and themselves (1 is not prime). An integer number is said to be perfect number if its factors, including 1 (but not the number itself), sum to the number. Sample Inputi: 1 100 Sample Outputs: Prime: 25 Perfect: 2 Sample Input2: 101 10000...

  • (a) Implement Counting Inversions algorithm in C++. Provide the complete program here which returns the count...

    (a) Implement Counting Inversions algorithm in C++. Provide the complete program here which returns the count and the array. (b) Test your code using this input: a = [2, 6, 1, 5, 4, 3] where array a is the list of your ranking for the given 6 songs.

  • (Count vowels and consonants, file input, nested-loops, switch statement) Assume that letters A, E, I, O...

    (Count vowels and consonants, file input, nested-loops, switch statement) Assume that letters A, E, I, O and U are the vowels. Write a program that reads strings from a text file, one line at a time, using a while-loop. You do the following operations within each loop:  Read one line from the input file and store it in a string;  Count the number of vowels and consonants (using either while-loop or for-loop) in the file string. The while-loop...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT