CSE 5311 Section 501 Spring 1998
Homework 2
Due: February 16, 1998, in class
Any unstable sorting algorithm can be made stable by adding an extra field to the sequence entries. Instead of sorting , sort . When comparing two items, is considered to be < iff or both and .
Idea 1
The load factor here is n/m = 20,000/24,000 = 0.83. Thus the number of probes in an unsuccessful search is , and the expected number of probes in a successful search is .
Idea 2
The load factor for the high-frequency database is = 4,000/6,000 = .67, and the load factor for the low-frequency database is = 16,000/18,000 = .89.
An unsuccessful search will first look through hf then lf, and the expected number of probes is = 3.03 + 9.09 = 12.12.
The successful search has two cases: either the key is found in hf which is searched first, or hf is searched first then the key is found in lf. In the first case, the number of expected probes is equal to a successful search in hf, or = 1.65 + 1.49 = 3.14. In the second case, the number of expected probes is equal to the number of probes for unsuccessful search in hf plus the number of expected probes for a successful search in lf, or 3.03 + = 3.03 + 3.59 = 6.62.
The total number of expected probes for a successful search is weighted by the probability of finding the probe in case 1 or case 2. Thus the total number of expected probes is 0.8*3.14 + 0.2*6.62 = 3.84. Thus idea 1 performs better for unsuccessful and successful searches than idea 2.
For idea 1 the values will always be the same (5.88 probes for an unsuccessful search, 3.33 probes for a successful search). Idea 2 will perform better in the case of a successful search when . The value of p will need to be smaller than 1/5 given the same load factors.
Inserting 1... 1(B) Inserting 2... 2(R) 1(B) Inserting 3... Insert Case IIIb, x = 3(R) 3(R) 2(B) 1(R) Insert Case Ib, x = 5(R) 5(R) 3(B) 2(B) 1(B) Inserting 7... Insert Case IIIb, x = 7(R) 7(R) 5(B) 3(R) 2(B) 1(B) Inserting 6... Insert Case Ib, x = 6(R) 7(B) 6(R) 5(R) 3(B) 2(B) 1(B) Inserting 4... 7(B) 6(R) 5(R) 4(R) 3(B) 2(B) 1(B) Inserting 8... 8(R) 7(B) 6(R) 5(R) 4(R) 3(B) 2(B) 1(B) Inserting 10... Insert Case Ib, x = 10(R) Insert Case IIIb, x = 7(R) 10(R) 8(B) 7(R) 6(B) 5(B) 4(R) 3(B) 2(R) 1(B) Inserting 9... Insert Case IIb x = 9(R) Insert Case IIIb, x = 10(R) 10(R) 9(B) 8(R) 7(R) 6(B) 5(B) 4(R) 3(B) 2(R) 1(B)
Deleting 4... 10(R) 9(B) 8(R) 7(R) 6(B) 5(B) 3(B) 2(R) 1(B) Deleting 3... Delete Case IIb, x = NIL(B) 10(R) 9(B) 8(R) 7(R) 6(B) 5(B) 2(B) 1(R) Deleting 1... 10(R) 9(B) 8(R) 7(R) 6(B) 5(B) 2(B) Deleting 2... Delete Case Ia, x = NIL(B) Delete Case IIa, x = NIL(B) 10(R) 9(B) 8(R) 7(B) 6(R) 5(B) Deleting 9... 10(B) 8(R) 7(B) 6(R) 5(B) Deleting 10... 8(B) 7(B) 6(R) 5(B) Deleting 7... Delete Case IIIb, x = NIL(B) Delete Case IVb, x = NIL(B) 8(B) 6(B) 5(B) Deleting 6... Delete Case IIb, x = NIL(B) 8(B) 5(R) Deleting 5... 8(B) Deleting 8... NIL(B)