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

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d

Partial preview of the text

Download Quick Sort Algorithm Implementation and more Study notes Computer Science in PDF only on Docsity!

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.

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.

Quick Sort Example

We select 57 to be our pivot

  • (^) We move 24 into the first location 7.6.

Quick Sort Example

Starting at the 2 nd and 2 nd -last locations:

  • (^) we search forward until we find 70 > 57
  • (^) we search backward until we find 49 < 57 7.6.

Quick Sort Example

We search forward until we find 97 > 57 We search backward until we find 16 < 57 7.6.

Quick Sort Example

We swap 16 and 97 which are now in order with respect to each other 7.6.

Quick Sort Example

We swap 63 and 55 7.6.

Quick Sort Example

We search forward until we find 85 > 57 We search backward until we find 36 < 57 7.6.

Quick Sort Example

We search forward until we find 68 > 57 We search backward until we find 9 < 57 7.6.

Quick Sort Example

We swap 68 and 9 7.6.

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.

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.

Quick Sort Example

We choose 24 to be our pivot We move 9 into the first location in this sub-list 7.6.

Quick Sort Example

We search forward until we find 49 > 24 We search backward until we find 21 < 24 7.6.