Question

1. Consider the following well-known sorting algorithm, which is studied later in the book, with a...

1. Consider the following well-known sorting algorithm, which is studied later
in the book, with a counter inserted to count the number of key comparisons.
ALGORITHM SortAnalysis(A[0..n − 1])
//Input: An array A[0..n − 1] of n orderable elements
//Output: The total number of key comparisons made
count ←0
for i ←1 to n − 1 do

v ←A[i]
j ←i − 1
while j ≥ 0 and A[j ]> v do
count ←count + 1
A[j + 1]←A[j ]
j ←j − 1
A[j + 1]←v
return count
Is the comparison counter inserted in the right place? If you believe it is, prove
it; if you believe it is not, make an appropriate correction.

Run the program of Problem 1, with a properly inserted counter (or counters) for the number of key comparisons, on 20 random arrays of sizes 1000, 2000, 3000, . . . , 20,000. b. Analyze the data obtained to form a hypothesis about the algorithm’s average-case efficiency. c. Estimate the number of key comparisons we should expect for a randomly generated array of size 25,000 sorted by the same algorithm.

please provide the solution step by step

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

//count is at wrong place it is out of the loop therefore it never count the no. of comparision

//below is the complete code that run and compute the total no. of comparision made to sort the randomly generated array

//"get_data function" is used to assign random value to the arrays like a[1000],b[2000],c[25000]

//"sort function" is used to sort the array

//sort function takes two argument one is which is to be sort another is the size of the array

//function work in such a way that it set the smallest no first

//step 1:- again=0

//step 2:- for loop iterate element by element,

//step 3:- if array is not sorted at that value of i then if "a[i+1]<a[i] =True",then a[i+1] swap with a[i], again=1

// if "a[i+1]<a[i] =false" that means upto that value of i array is sorted and program move to next step

//step 4:- count=count+1 and i=i+1.... and continue step2 to step 4 till i<iter(or size-1)

//step 5:- while loop check the value of again. if again=1 that means array is not sorted and loop run again and step1 to step5

// whereas if again=0 that means array is sorted and while loop terminated.

//step 6:- count is return to the main

#include <stdio.h>

#include <stdlib.h>

int sort(int array[], size_t size)

{

int i = 0;

int temp,count=0;

int again = 1;

int iteration = size - 1;

do {

again = 0;

for(i=0; i < iteration; ++i)

{

if (array[i + 1] < array[i]) //if array is not sorted this condition is true that makes again=1 while rerun the do-while

{ //followed by for loop otherwise again is set to 0 before terminates the loop

temp = array[i]; //swapping

array[i] = array[i + 1];

array[i + 1] = temp;

again = 1;

}

count++;

}

--iteration;

} while (again);

return count;

}

//function used to print array

void print_data(int array[],size_t size)

{

unsigned i = 0;

for(; i < size; ++i)

{

printf("%d\n", array[i]);

}

}

//get_data method is used to assign random value to the arrays like a[1000],b[2000],c[25000]

void get_data(int array[],size_t size)

{

unsigned i = 0;

for(; i < size; ++i)

{

array[i] = rand() %100;

}

}

int main(void)

{

int a[1000], b[2000], c[25000];

get_data(a, sizeof a / sizeof (int));

get_data(b, sizeof b / sizeof (int));

get_data(c, sizeof c / sizeof (int));

int count = sort(a, sizeof a / sizeof (int));

printf("%d\n",count);

//print_data(a, sizeof a / sizeof (int));

count = sort(b, sizeof b / sizeof (int));

printf("%d\n",count);

count = sort(c, sizeof c / sizeof (int));

printf("%d\n",count);

//print_data(c,sizeof c / sizeof (int));

exit(0);

}

//estimated count for 25000 size array is 312453570.

Add a comment
Know the answer?
Add Answer to:
1. Consider the following well-known sorting algorithm, which is studied later in the book, with a...
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
  • Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion...

    Comparison of Sorting Algorithms You are required to implement all the sorting algorithms (Bubble, Selection, Insertion, Quick, Merge). Take help from lecture slides and welb . You will then create arrays of different sizes as instructed below, test each algorithm on each array and record the execution times of each individual algorithm on each array. . You will report these execution times in a table and then write a report explaining the execution times of the sorting algorithms according to...

  • I've developed a new, super cool sorting algorithm. Here's the procedure: Until sorted: Randomly generate i...

    I've developed a new, super cool sorting algorithm. Here's the procedure: Until sorted: Randomly generate i = some number between 0 and n - 1 (n = size of array) Randomly generate j = some number between i + 1 and n - 1 if array[i] > array[j], swap Check to see if the array has been sorted List the algorithm's efficiency using Big-O.

  • Consider an ordered array A of size n and the following ternary search algorithm for finding...

    Consider an ordered array A of size n and the following ternary search algorithm for finding the index i such that A[i] = K. Divide the array into three parts. If A[n/3] > K. the first third of the array is searched recursively, else if A[2n/3] > K then the middle part of the array is searched recursively, else the last thud of the array is searched recursively. Provisions are also made in the algorithm to return n/3 if A[n/3]...

  • Data Structures: For each of the following situations, name the best sorting algorithm we studied. (For...

    Data Structures: For each of the following situations, name the best sorting algorithm we studied. (For one or two questions, there may be more than one answer deserving full credit, but you only need to give one answer for each.) (a) The array is mostly sorted already (a few elements are in the wrong place). (b) You need an O(n log n) sort even in the worst case and you cannot use any extra space except for a few local...

  • Consider an array A[1...n] which is originally unsorted. One method to sort the array is as...

    Consider an array A[1...n] which is originally unsorted. One method to sort the array is as follows: First find the largest key in the unsorted portion of the array (initially the entire array s unsorted) and swap this with the last position in the unsorted section. This last position is now considered part of the sorted portion. The procedure is repeated until the entire array is sorted. (a) Write an algorithm to sort A as outlined above (in pseudo-code, no...

  • Bubble sort is a popular, but inefficient, sorting algorithm. It works by repeatedly swapping adjacent elements...

    Bubble sort is a popular, but inefficient, sorting algorithm. It works by repeatedly swapping adjacent elements that out of order. BUBBLESORT(A) 1. for i = 1 to A.length – 1 2. for j = i + 1 to A.length 3. if A[j] < A[i] 4. exchange A[j] with A[i] a) A loop invariant for the outer for loop in lines 1 – 4 is: At iteration i, the sub-array A[1..i] is sorted and any element in A[i+1..A.size] is greater or...

  • This is an assignment for my algorithm class which I have written the code partially and...

    This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...

  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is...

    (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an earray A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A i=j-1 while i > 0 and A[i] > key do Ali + 1] = Ai i=i-1 7: A[i+1] = key (a) Follow this algorithm for A[1..4) =< 7,9,6,8 >. Specifically, please indicate the contents of the array after each iteration of the outer...

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