Question

[Acuña] Give sortints Big-Oh order if its input was already sorted, where the expression bounds the number of swaps. Include

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

Given an sorting algorithm and input is already sorted.

--- If the input is already sorted. Then no swapping will happen.

So the number of swaps = 0

but irrespective of the sorted array, outer for loop will iterate n times when n is the size of the input array.

So based on swappings f(n) = 1

but if you consider the outer for loop also then T(n) = O(n)

Add a comment
Know the answer?
Add Answer to:
[Acuña] Give sortints' Big-Oh order if its input was already sorted, where the expression bounds the...
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
  • What is the Big-Oh order of the following code fragment? The fragment is parametrized on the...

    What is the Big-Oh order of the following code fragment? The fragment is parametrized on the variable N. Assume that you are measuring the number of times j is decremented. public static void sort(Comparable[] a) { int N-a.length; for (int i = 1; i < N;i++) { for (int j = i; j > && less(a[5], a[j-1]); j--) //measure j -- exch(a, j, j-1); O(nlogn) O O(n^2) Q(n) Does not exist.

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • #1. Using the definition of big-O, prove that f(x) = 5x^4+x^3+8x-2 . Show all work. #2....

    #1. Using the definition of big-O, prove that f(x) = 5x^4+x^3+8x-2 . Show all work. #2. void bubbleSort(Student myClass[], int size) { int pass = 0; // counts each pass of the sort bool done = false; // whether sorted or not // each pass puts one element into its sorted position, // smallest value bubbles to the top of the array while (!done) { done = true; // possibly sorted // compare consecutive elements, swap if out of order...

  • Give a big-Oh characterization, in terms of n,of the running time for each of the following...

    Give a big-Oh characterization, in terms of n,of the running time for each of the following code segments (use the drop-down): - public void func1(int n) { A. @(1). for (int i = n; i > 0; i--) { System.out.println(i); B. follogn). for (int j = 0; j <i; j++) System.out.println(j); c.e(n). System.out.println("Goodbye!"); D.@(nlogn). E.e(n). F.ein). public void func2 (int n) { for (int m=1; m <= n; m++) { system.out.println (m); i = n; while (i >0){ system.out.println(i); i...

  • Hey i got the program to work but i cant get the merge sorter from big...

    Hey i got the program to work but i cant get the merge sorter from big to little import java.util.*; class MergeSorter {    public static void sort(int[] a)    { if (a.length <= 1) { return; } int[] first = new int[a.length / 2]; int[] second = new int[a.length - first.length]; for (int i = 0; i < first.length; i++) {    first[i] = a[i]; } for (int i = 0; i < second.length; i++) {    second[i] =...

  • What is the runtime of each method? Give answer in Θ(big Theta) notation as a function...

    What is the runtime of each method? Give answer in Θ(big Theta) notation as a function of n, give a brief explanation. A. public static int method1(int n){ int mid = n/2; for (int i = mid; i >= 0; i--) System.out.println(i); for (int i = mid + 1; i <= n; i++) System.out.println(i); return mid; } B. public static int method2(int n){ for (int i = n; i >= 0; i / 3){ System.out.println(i ); } return mid; }...

  • 6. (4 points) The following code for insertion sort has been modified to print out the...

    6. (4 points) The following code for insertion sort has been modified to print out the sorted component of the array during the sort. What will be the output of running the main method of the following code? Try to trace by hand and then verify by executing the code public class Insertion ( public static void sort (String[] a) f int n- a.length; for (int i-1; i < n; i++) f for (int j i; j 0; j--) if...

  • Which big-O expression best characterizes the worst case time complexity of the following code? public static...

    Which big-O expression best characterizes the worst case time complexity of the following code? public static int foo(int N) ( int count = 0; int i1; while (i <N) C for (int j = 1; j < N; j=j+2) { count++ i=i+2; return count; A. O(log log N) B. O(log N2) C. O(N log N) D. O(N2)

  • Determine the Big 0 provide the order (Big O) of the execution speed also determine the...

    Determine the Big 0 provide the order (Big O) of the execution speed also determine the exact execution speed. public class CountIt { public long SnippetNestedLoop(long n) { long i, j, x = 0; i=0; x++; while(i<n){ x++; //i<n // SomeStatement // j = 0; // j < n // SomeStatement // j++; // Can you explain why is this here? // i++; // Can you explain why is this here? Ans: i < n } } } x++; return...

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