15 views

Uploaded by Thu Vu

- Wk 13 L2
- quiz solutions
- Sorting Types
- TREE (C)
- Study Guide
- ImpofAlgorithms_Topcoder
- CBLM Core 4
- java book
- 09-MapReduceAlgorithms
- CodingJava
- Google Interview Prep Frontend Mobile Engineer
- C_lang_Ref_Raja
- Prog Prob
- (Www.entrance Exam.net) (Www.entrance Exam.net) Hcl1
- B.tech. (Computer Engineering) Part-II to IV (Semester III to VIII) (Batch 2011)
- List of Practice Problems(CS 201)
- Practical CS FILE 2016-17
- Quick Sort Explanation
- Data Structures & Algorithms- Final Part 1 Answers
- Array

You are on page 1of 5

Tutorial 6 Questions SORTING ALGORITHMS

Required Questions Question 1. Many operations can be performed faster on sorted than on unsorted data. For which of the following operations is this the case? a. checking whether one word is an anagram of another word, e.g., plum and lump b. finding the minimum value. c. computing an average of values d. finding the middle value (the median) e. finding the value that appears most frequently in the data Answer a. Yes b. Yes c. No d. Yes e. Yes Question 2. In which case, the following sorting algorithm is fastest/slowest and what is the complexity in that case? Explain. a. insertion sort b. selection sort c. bubble sort d. quick sort Answer Assume that the algorithms are performed on contiguous lists (i.e. array). Fastest Slowest Algorithm The input is a reverse-ordered list. At Insertion The input is an ordered list. At every step, the element at current is only every step, the element at current is sort compared to the element at (current 1). compared to every elements preceded it. Comparisons: C = n 1 = O(n) Moves: M = 0 or M = 2(n 1) = O(n) Comparisons: C = n(n 1)/2 = O(n2) Moves: M = (3+4+5+...(n1+2)) = (n(If we change the algorithm so that it 1)(n+4)/2 = O(n2) does not copy out the data in line 1.2.1 1

Selection sort

Bubble sort

Quick sort

and copy in the data in line 1.2.4 in the (At each i, we have to copy the data lecture slide every time, in this fastest out to the variable temp, and move (i case, the number of moves is 0. 1) elements forward, and copy the Otherwise, we need 2 moves at each temp to the first position.) step. Then the number of moves is 2(n 1).) This algorithm performs the same in all cases. C = n(n 1)/2 = O(n2) M = 3(n 1) = O(n) At each iteration, we have to compare all the remained elements to select the next smallest elements, follows by an exchange (3 moves) of the current position and the smallest position. For an ordered list input, if we change the algorithm so that it does not exchange the elements if the smallest position and the current position are the same, the number of move is M = 0. The input is an ordered list. The The input is an reverse-ordered list. algorithm stops after the first iteration. C=n(n 1)/2 = O(n2) C = n 1 = O(n) M=n(n 1)/2 = O(n2). M=0 At each step, we have to compare and move the next smallest element up to the beginning. Each recursion step the partitioning Each recursion step an unbalanced produces two parts of equal length. partitioning is produced, namely that one part consists of only one element Running time: O(n log(n)). and the other part consists of the rest of the elements. This is because the recursion depth is Running time: O(n2). log(n) and on each level there The recursion depth is n-1 and on each are n elements to be treated. level there are n elements to be treated.

Question 3. Consider the sequence of integers S = {5, 8, 2, 4, 3, 6, 1, 7} For each of the following sorting algorithms, indicate the sequence S after executing each step of the algorithm as it sorts this sequence: a. insertion sort b. selection sort c. heap sort d. bubble sort e. merge sort 2

Answer 1 Smallest in quick sort 1 Pivot in quick sort 1 i index in quick sort

S 3 3 3 3 8 6 5 5 3 3 8 8 5 5 5 4 3 8 5 5 5 5 6 6 6 6 6 8 6 6 6 6 6 6 6 6 6 3 4 4 8 6 6 6 1 1 1 1 1 1 8 7 5 5 5 5 8 8 7 6 6 6 6 8 7 7 7 7 7 7 7 7 7 8 7 7 7 7 7 7 8 7 7 7 7 7 8 8

Heap sort

(Slide)

5 5 5 8 8 4 7 7 1 6 6 1 5 5 3 4 4 1 3 2 2 1

8 8 8 5 7 7 4 5 5 5 5 5 1 4 4 3 3 3 1 1 1 2

2 6 6 6 6 6 6 6 6 1 2 2 2 2 2 2 2 2 2 3 3 3

7 7 7 7 5 5 5 4 4 4 4 4 4 1 1 1 1 4 4 4 4 4

3 3 3 3 3 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5

6 2 2 2 2 2 2 2 2 2 1 6 6 6 6 6 6 6 6 6 6 6

1 1 1 1 1 1 1 1 7 7 7 7 7 7 7 7 7 7 7 7 7 7

4 4 4 4 4 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8

C = 2, M = 3 C = 2, M = 0 C = 2, M = 3 C = 2, M = 3 C = 1, M = 0 C = 2, M = 6 C = 2, M = 3 C = 2, M = 6 C = 1, M = 3 C = 2, M = 6 C = 2, M = 3 C = 2, M = 6 C = 1, M = 0

Question 4. Consider the sequence of integers T = {1, 9, 2, 6, 4, 8, 0, 7} Indicate the sequence T after executing each step of the Cocktail sort algorithm (see Appendix) as it sorts this sequence. Answer T 1 9 2 6 4 8 0 7 1 0 0 0 2 1 1 1 6 2 2 2 4 6 4 4 8 4 6 6 0 8 7 7 7 7 8 8 9 9 9 9 C = number of comparisons S = number of swap C = 7, S = 6 (the first for loop in do while) C = 7, S = 5 (the second for loop in do while) C = 7, S = 2 (the first for loop in do while) C = 7, S = 0 (the second for loop in do while) Break do while C = 28, S = 13

Appendix Cocktail sort Cocktail sort, also known as bidirectional bubble sort, cocktail shaker sort, shaker sort (which can also refer to a variant of selection sort), ripple sort, shuffle sort, shuttle sort or happy hour sort, is a variation of bubble sort that is both a stable sorting algorithm and a comparison sort. The algorithm differs from bubble sort in that it sorts in both directions on each pass through the list. This sorting algorithm is only marginally more difficult to implement than bubble sort, and solves the problem with so-called turtles in bubble sort. (Wikipedia) Pseudocode

procedure cocktailSort( A : list of sortable items ) defined as: do swapped := false for each i in 0 to length( A ) - 2 do: if A[ i ] > A[ i + 1 ] then // test whether the two elements are in the wrong order swap( A[ i ], A[ i + 1 ] ) // let the two elements change places swapped := true end if end for if swapped = false then // we can exit the outer loop here if no swaps occurred. break do-while loop end if swapped := false for each i in length( A ) - 2 to 0 do: if A[ i ] > A[ i + 1 ] then swap( A[ i ], A[ i + 1 ] ) swapped := true end if end for while swapped // if no elements have been swapped, then the list is sorted end procedure

-- End --

- Wk 13 L2Uploaded byrizvi098
- quiz solutionsUploaded byapi-347983176
- Sorting TypesUploaded bykalanithi
- TREE (C)Uploaded byबानि तमिन्
- Study GuideUploaded byYazan Aswad
- ImpofAlgorithms_TopcoderUploaded bymaile4scribd
- CBLM Core 4Uploaded byKevin Taguba
- java bookUploaded byapi-3836128
- 09-MapReduceAlgorithmsUploaded bycooldesi47
- CodingJavaUploaded bychitranshpandey
- Google Interview Prep Frontend Mobile EngineerUploaded byKang-Li Cheng
- C_lang_Ref_RajaUploaded byRaja
- Prog ProbUploaded byJJohn Ssdg
- (Www.entrance Exam.net) (Www.entrance Exam.net) Hcl1Uploaded byAishwarya Krishnan
- B.tech. (Computer Engineering) Part-II to IV (Semester III to VIII) (Batch 2011)Uploaded byDrAditiBansal
- List of Practice Problems(CS 201)Uploaded byArnab Bhattacharjee
- Practical CS FILE 2016-17Uploaded byDelhiite Siddhartha
- Quick Sort ExplanationUploaded byTrishool Narayanasetty
- Data Structures & Algorithms- Final Part 1 AnswersUploaded byKeshireddy Srikanthreddy
- ArrayUploaded byAmit sinha
- ComplexityUploaded byAditya
- Ada Lab ManualUploaded byManohar NV
- Lesson1-5.5.pdfUploaded byProjectlouis
- lec3Uploaded byPrince
- Bundle SortingUploaded bymanuelq9
- Linked ListsUploaded byPreetam Saha
- MC0080Uploaded bySorokhaibam Thouba Meetei
- Test 2 Fall 03 AnswersUploaded byGobara Dhan
- 239750255/Core-Java-Professional-Think-Twice-Code-Once-Complete-Java-Programming-GuideUploaded bygucci
- Sphere - The Learning PointUploaded byPrashantBhattacharji

- ExercisesUploaded byThu Vu
- ClippingUploaded byThu Vu
- Report 1Uploaded byThu Vu
- vq-ubmUploaded byThu Vu
- dsachdot1Uploaded byThu Vu
- Tut 7_SolUploaded byThu Vu
- Kinh Nghiem Sat Hach CNTTUploaded byBố Đéo
- Tut 2Uploaded byThu Vu
- ExercisesUploaded byThu Vu
- ExercisesUploaded byThu Vu
- ExercisesUploaded byThu Vu
- Gmmubm Vqubm ComparisonUploaded byThu Vu
- nhan dang nguoi noiUploaded byThu Vu
- thicuoikyUploaded byThu Vu
- Assignment Web ProgrammingUploaded byThu Vu
- dsachdot1Uploaded byThu Vu
- Tut1 SolutionUploaded bybuiductaianh
- Tut5Uploaded byThu Vu
- Tut2 SolutionUploaded byThu Vu
- Tut2_200213Uploaded byThu Vu
- Tut2_200213Uploaded byThu Vu
- Tut5_SolUploaded byThu Vu
- Exercise1_w2_vn_v1.1Uploaded byThu Vu
- Tut 4Uploaded byĐừngh
- LayoutUploaded byThu Vu
- Final2011 SolUploaded byThu Vu
- Final 2010Uploaded byThu Vu
- Final 2010Uploaded byThu Vu
- Tut+6Uploaded byThu Vu

- BCCSPUploaded byPinnacle Systems Group
- Java Chat ProgramUploaded byMuhammad Hanif
- Incluyendo ConstraintsUploaded byFelix Yohan Sancan Molina
- Behavior ArticleUploaded bywebfrank2
- Dot Net Practical FileUploaded byDharmendra Kumar
- lab5 Zybo XilinxUploaded byTryphon Tso-hunter
- FormattingUploaded byHadi Khajouee Nejad
- Mca5033 Winter 2016 solved assignmentUploaded byaapki education
- Programming Paradigms for Dummies: What Every ProgrammerUploaded byapi-25884893
- CS604Uploaded bychi
- ACA305-2000Uploaded bynitin_280890
- ETL Error and Audit Log Process Using aUploaded bykushrajan2
- Optimizing Code Performance and Size for Stellaris® MicrocontrollersUploaded byTi Microcontrollers
- Netbean-CygwinUploaded byjs998
- Javascript for BreakfastUploaded byapi-3699342
- Assignment 1 - LORD of the RINGSUploaded byJonathan Sergio
- Idms SimplifiedUploaded bysuzeet1
- Java Magazine JUnit 5 ArrivesUploaded byWesley Ramos
- PM IMG Configuration Tracker & KDS (key data structure)Uploaded bySarthakTripathy
- Java LabUploaded byVishnu Kumar Reddy P
- pthreadsTutorialUploaded byAlhad_Rajurwar_2900
- 1st Semester _ PEC.pdf 3Uploaded byShikha Singla
- IntroductionToWekaUploaded bysandyguru05
- 120 NotesUploaded byAaa Mmm
- SQLUploaded bycyanide20000
- BWOpen Hub Extraction EnhancementUploaded bysanthosh945
- Exception Pdflibexception With Message Handle Parameter Image HasUploaded byJeremy
- Common Language Infrastructure Annotated Standard PDFUploaded byJon
- Flask.pdfUploaded byfergalicious
- Load Utilities in TeradataUploaded byAnirudReddy