Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Quick Sort Algorithm Implementation, Study notes of Computer Science

An implementation of the Quick Sort algorithm. The algorithm partitions elements based on whether they are smaller or greater than the pivot. It then finds two entries, one larger and one smaller than the pivot, which are out of order and corrects their ordering by swapping them. examples of the algorithm's implementation and sorting of sub-lists. useful for students studying algorithms and data structures.

Typology: Study notes

2019/2020

Available from 03/23/2023

raj-paliwal-1
raj-paliwal-1 ๐Ÿ‡ฎ๐Ÿ‡ณ

5 documents

1 / 45

Toggle sidebar

Related documents


Partial preview of the text

Download Quick Sort Algorithm Implementation and more Study notes Computer Science in PDF only on Docsity! Quick Sort 1 Implementation Next, recall that our goal is to partition all remaining elements based on whether they are smaller than or greater than the pivot We will find two entries: โ€“ One larger than the pivot (staring from the front) โ€“ One smaller than the pivot (starting from the back) which are out of order and then correct the ordering โ€“ I.e., swap them 7.6.5 Quick Sort 2 Implementation Continue doing so until the appropriate entries you find are actually in order The index to the larger entry we found would be the first large entry in the list (as seen from the left) Therefore, we could move this entry into the last entry of the list We can fill this spot with the pivot 7.6.5 Quick Sort 5 Quick Sort Example Starting at the 2nd and 2nd-last locations: โ€“ we search forward until we find 70 > 57 โ€“ we search backward until we find 49 < 57 7.6.5 Quick Sort 6 Quick Sort Example We swap 70 and 49, placing them in order with respect to eachother 7.6.5 Quick Sort 7 Quick Sort Example We search forward until we find 97 > 57 We search backward until we find 16 < 57 7.6.5 Quick Sort 10 Quick Sort Example We swap 63 and 55 7.6.5 Quick Sort 11 Quick Sort Example We search forward until we find 85 > 57 We search backward until we find 36 < 57 7.6.5 Quick Sort 12 Quick Sort Example We swap 85 and 36, placing them in order with respect to each other 7.6.5 Quick Sort 15 Quick Sort Example We search forward until we find 76 > 57 We search backward until we find 9 < 57 โ€“ The indices are out of order, so we stop 7.6.5 Quick Sort 16 Quick Sort Example We move the larger indexed item to the vacancy at the end of the array We fill the empty location with the pivot, 57 The pivot is now in the correct location 7.6.5 Quick Sort 17 Quick Sort Example We will now recursively call quick sort on the first half of the list When we are finished, all entries < 57 will be sorted 7.6.5 Quick Sort 20 Quick Sort Example We search forward until we find 49 > 24 We search backward until we find 21 < 24 7.6.5 Quick Sort 21 Quick Sort Example We swap 49 and 21, placing them in order with respect to eachother 7.6.5 Quick Sort 22 We search forward until we find 38 > 24 We search backward until we find 16 < 24 The indices are reversed, so we stop Quick Sort Example 7.6.5 Quick Sort 25 Quick Sort Example The first partition has three entries, so we sort it using insertion sort 7.6.5 Quick Sort 26 Quick Sort Example The second partition also has only four entries, so again, we use insertion sort 7.6.5 Quick Sort 27 Quick Sort Example First we examine the first, middle, and last entries of the sub-list 7.6.5 Quick Sort 30 Quick Sort Example We swap 70 and 84 placing them in order 7.6.5 Quick Sort 31 Quick Sort Example We search forward until we find 85 > 74 We search backward until we find 61 < 74 7.6.5 Quick Sort 32 Quick Sort Example We swap 85 and 61 placing them in order 7.6.5 Quick Sort 35 Quick Sort Example We sort the left sub-list first It has four elements, so we simply use insertion sort 7.6.5 Quick Sort 36 Having sorted the four elements, we focus on the remaining sub-list of seven entries Quick Sort Example 7.6.5 Quick Sort 37 Quick Sort Example To sort the next sub-list, we examine the first, middle, and last entries 7.6.5 Quick Sort 40 Quick Sort Example We swap 85 and 77, placing them in order 7.6.5 Quick Sort 41 Quick Sort Example We search forward until we find 97 > 79 We search backward until we find 77 < 79 The indices are reversed, so we stop 7.6.5 Quick Sort 42 Quick Sort Example Finally, we move 97 to the vacant location and copy 79 into the appropriate location โ€“ 79 is now in the correct location 7.6.5
Docsity logo



Copyright ยฉ 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved