Question

1. How many comparisons and how many moves are needed to sort the array = {1,...

1. How many comparisons and how many moves are needed to sort the array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} using shell sort?

How many comparisons and how many moves are needed to sort the array = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1} using shell sort?

2. Write a c++ program that finds the number of comparisons and number of moves in shell sort.

Homework Answers

Answer #1

1. How many comparisons and how many moves are needed to sort the array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} using shell sort?

Ans:  No of comparisons to be made is : 25

No of moves to be made is : 0 ( Because it is already sorted )

2. How many comparisons and how many moves are needed to sort the array = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1} using shell sort?

Ans: No of comparisons to be made is : 51

No of moves to be made is : 26

( Swapping of elements with each other place is known to be 2 moves )

Program :

#include <iostream>
using namespace std;
  

int shell(int a[], int n)
{
int comp = 0;
int mov = 0;

for (int l = n/2; l > 0; l /= 2)
{
comp++;
  
for (int i = l; i < n; i += 1)
{
comp++;

int temp = a[i];

int j;   
for (j = i; j >= l && a[j - l] > temp; j -= l)
{
a[j] = a[j - l];
comp++;
comp++;
mov = mov + 2;
  
}

a[j] = temp;
}
}
cout << "Number of Comparison : \n" << comp << endl; // printing no of comparisons
  
cout << "Number of Moves : \n" << mov << endl; // printing no of moves
return 0;
}
  

int main()
{
int a[100], i, n;
cout << "Enter the no of elements to find the comparisons and moves: \n";
  
cin >> n; // read no of elements
  
cout << "Enter the elements to find the comparisons and moves: \n";
  
for(i = 0; i < n; i++)
{
cin >> a[i]; // read each element into an array
}
  
shell(a, n); // passing array and no of elements to shell function
  
cout << "After sorting: \n";
  
for(i = 0; i < n; i++)
{
cout << a[i] << " "; // print elements after sorting
}
  
return 0;
}

OUTPUT :

Know the answer?
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
Similar Questions
This program is in C++, And please consider " sort pass #" for the output: Write...
This program is in C++, And please consider " sort pass #" for the output: Write a program that uses two identical arrays of eight integers. It should display the contents of the first array, then call a function to sort it using an ascending order bubble sort, modified to print out the array contents after each pass of the sort. Next the program should display the contents of the second array, then call a function to sort it using...
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]...
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] W X D T P N R Q K M E If the array was being sorted using the SHELL sort and the halving method, and sorting into ASCENDING order as demonstrated in the course content, list the letters in the resulting array, in order AFTER the FIRST pass. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
First, understand the Selection-sort algorithm below: Selection-sort(A: Array [1..n] of numbers) 1       for i=n down to...
First, understand the Selection-sort algorithm below: Selection-sort(A: Array [1..n] of numbers) 1       for i=n down to 2 2                position=i 3                for j=1 to (i–1) 4                          if A[j]>A[position] then position=j 5                if position ≠ i then 6                          temp=A[i] 7                          A[i]=A[position] 8                          A[position]=temp (4 points) If input A=[12,5,11,6,10,7,9,8], what will A be after the 3rd iteration of the outermost for loop of the Selection-sort algorithm completes? A=[__, __, __, __, __, __, __, __] (8 points) Modify the algorithm to solve the...
CAN YOU PLEASE ANSWER ALL THESE QUSTIONS 1.In Merge sort, how many additional recursive partitioning levels...
CAN YOU PLEASE ANSWER ALL THESE QUSTIONS 1.In Merge sort, how many additional recursive partitioning levels are required for a list of 64 elements compared to a list of 8 elements? a. 3 b. 9 c. 8 d. 6 2. Which function best represents the number of operations in the worst-case for the following code fragment? for (i = 0; i < N; ++i) {    if (numbers[i] % 2 == 1)       factor = 2.5 } a. f(N) = 6N2 b....
Given this array: int sequence[10] = { 3, 4, 5, 6, 7, 8, 9, 10, 1,...
Given this array: int sequence[10] = { 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 }; Write a C++ program to ask the user to enter a number, if the number can be found in the array, your program should display "Found". Otherwise, your program should display "Not found". For example, if the user enters 7, your program should display "Found". If the user enters 11, your program should display "Not found".
# data structure Recall that the Insertion sort algorithm (for sorting an array A) from low...
# data structure Recall that the Insertion sort algorithm (for sorting an array A) from low to high has the skeleton form: for (i = 1; i < A.length; i++) { details omitted } For i = 1 to 6, show how the contents of the array is rearranged if at all) after each iteration of the for-loop. (10 pts) the original array A                                            A 6. 4. 7. 5....
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
Giving an array of integers A[1:8] ={5, 1, 3, 2, 7, 6, 8, 4}. Build a...
Giving an array of integers A[1:8] ={5, 1, 3, 2, 7, 6, 8, 4}. Build a MAX-HEAP on A. Show the steps using binary tree representation or array view. Use heap sort: show the array after the first, the second, and the third of heap sort
How do you take an array for example A = [4 6 7 1 2 5]...
How do you take an array for example A = [4 6 7 1 2 5] and sort it from ascending order. So it would output A = [1 2 4 5 6 7]. In MATlab without using the sort function as well.
Given array 8, 5, 3, 7, 1, 6, 4, 2. List array to show the array...
Given array 8, 5, 3, 7, 1, 6, 4, 2. List array to show the array changes in memory. You may list out array after each partition call is terminated. book’s Quick Sort algorithm
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT