Question

4. It is common practice for banks to record account transactions in the chronological order that they were made. However, it is also common for people to prefer to order their transactions by check number, versus chronological order. This is essentially a problem of sorting a list initially ordered chronologically into one sorted by check number, where it is highly likely that the chronologically ordered list is almost already sorted by check number. Consider the foll owing sorting algorithm where A is the already chronologically sorted list of transactions: 1: for 2 to length (A) do Ali] 4: while j 0 and AUI-checknumber v check number do Ali 1 AU1 7: end while AU+ 1] 9: end for (a) (8 points) Using a loop invariant prove that the sorting algorithm is correct. (b) (2 points) What is the worst-case? Briefly explain. (c) (4 points) What is a tight bound for the worst-case runtime? (d) (2 points) What is the best-case? Briefly explain. (e) (4 points) What is a tight bound for the best-case runtime? (f) (2 points) Recall the MERGESORT algorithm discussed in lecture. Explain, between MERGE- SORT and the above sorting algorithm, which you would recommend to perform the reordering from chronological to check number?

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

(a)
1st iteration(for loop)--

i=2,j=1 , there will only be 1 while loop iteration and if the A[j].checknumber>A[i].checknumber they will be swapped

so now , first 2 elements of array are sorted

2nd iteration(for loop)--

i=3,j=2, now if A[i].checknumber>A[j].checknumber we dont need to check further as the first two elements are already sorted
       and if A[j].checknumber>A[i].checknumber then the algorithm will find the correct place for A[i] and place it there.
       so the array till i will become sorted.

Kth iteration(foor loop)--

i=k,j=k-1, now array till index k-1 is already sorted.
       So if A[k].checknumber>A[k-1].checknumber then the array is already sorted.
       If A[k].checknumber<A[k-1].checknumber then we need to find the correct place for A[k] and place it there.

Last iteration(for loop)--
Let length of array=l
i=l,j=l-1, now array till index l-1 is already sorted.
       So if A[l].checknumber>A[l-1].checknumber then the array is already sorted.
       If A[l].checknumber<A[l-1].checknumber then we need to find the correct place for A[l] and place it there.

       And hence the whole array will become sorted.

(b)
Worst case is when the array is sorted in decreasing order checknumber wise.
In every iteration we need to traverse all the way back to the first element.
Time complexity in worst case -- O(n2)

(c)
Tight bound for the worst case runtime O(n2)

(d)
Best case is when the array is already sorted in increasing order checknumber wise.
We just need n comparisions in that case.
We will never enter inside the inner while loop.
Time complexity in best case -- O(n)

(e)
Tight bound for the best case runtime is O(n)

(f)

As it is given in the question that chronologically ordered list is almost already sorted by check number so Merge sort take O(nlogn) time in best case but the given algorithm which is Insertion sort takes O(n) time in best case.So I would recommend using Insertion sort in this particular problem.

Add a comment
Know the answer?
Add Answer to:
It is common practice for banks to record account transactions in the chronological order that they...
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
  • 8 Sorting Algorithms: Bubble, selection, insertion, quick, merge, bucket, radix, counting. 1. A..Which of the above...

    8 Sorting Algorithms: Bubble, selection, insertion, quick, merge, bucket, radix, counting. 1. A..Which of the above sorting algorithms does TimSort use? 2. Which of the above algorithms sort a REVERSE ORDER list in O(n2 ) (worst case)? 3. Which of the above algorithms sort a REVERSE ORDER list in O(nlogn) (worst case)? 4. Which of the above algorithms sort an ordered list , a reverse ordered list, and a random list (all three) in 0(nlogn) (worst case)? 5. Which of...

  • 2. Short programming assignment. Implement a bottom-up mergesort that first sorts blocks of M elements using...

    2. Short programming assignment. Implement a bottom-up mergesort that first sorts blocks of M elements using insertion sort. Test your program with large sets of random data and determine what value of M works best. 3. The following refer to linked list mergesort Explain why mergesort is more suitable for linked lists than other sorting methods. a. Explain why mergesort is more suitable for linked lists than other b. Consider a file that is “mostly sorted.” Explain how the principles...

  • problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to...

    problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to write its pseudo-code). To sort an array A, you will then call Det-QuickSort(A, 1, n). You also need to provide the worst case time complexity analysis of your algorithm. 2. (20 points) Given a set of n distinct numbers, we wish to find the k largest in sorted order using a comparison-based algorithm. Give an algorithm that implements each of the following methods, and...

  • please I need it urgent thanks algorithms 2.1 Searching and Sorting- 5 points each 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Ex...

    please I need it urgent thanks algorithms 2.1 Searching and Sorting- 5 points each 3. What is the worst case for quick sort? What is the worst case time com- plexity for quick sort and why? Explain what modifications we can make to quick sort to make it run faster, and why this helps. 4. Give pseudocode for an algorithm that will solve the following problem. Given an array AlL..n) that contains every number between 1 and n +1 in...

  • Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an...

    Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an input sequence of numbers. MysteryAlgorithm Input: a1, a2....,an n, the length of the sequence. p, a number Output: ?? i != 1 j:=n While (i < j) While (i <j and a < p) i:= i + 1 End-while While (i <j and a 2 p) j:=j-1 End-while If (i < j), swap a, and a End-while Return( aj, a2,...,an) (a) Describe in English...

  • Do the following project: Following is the file to be programmed in Linux kernel. Run this...

    Do the following project: Following is the file to be programmed in Linux kernel. Run this program. Include the screenshot of the results. Multi threaded Sorting Application Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sub list using a sorting algorithm of your choice. The two sub lists are then merged by a third thread—a...

  • PLEASE READ CAREFULLY AND ALL THE WAY THROUGH!!! I already asked this question but unfortunately the...

    PLEASE READ CAREFULLY AND ALL THE WAY THROUGH!!! I already asked this question but unfortunately the person who answered it did not read it. There is a sorting algorithm, “Stooge-Sort” which is named after the comedy team, "The Three Stooges." if the input size, n, is 1or 2, then the algorithm sorts the input immediately. Otherwise, it recursively sorts the first 2n/3 elements, then the last 2n/3 elements, and then the first 2n/ 3 elements again. The details are shown...

  • Please help me to solve the problem with java language! An implementation of the Merge Sort...

    Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply...

    . Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply that to the following array. Show your work in Detail. [15 points] 45 20 50 10 80 30 60 70 40 90 2. Is Shell sort a stable sorting algorithm? Answer this with an example. [10 points] 3. Apply Merge Sort to sort the following list. Show your work in Detail. [15 Points] 45 20 50 10 80 30 60 70 40 90 4....

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