Try This Quick Sort
In Chapter 5, you saw a simple classification method called
bubble sorting. It was mentioned at that time that there are
significantly better ratings. Here, you will develop a version of one of the best: quick sort (Quicksort).
Quick classification, invented and named by C.A.R. Hoare, is the best general-purpose classification algorithm currently available. I couldn't show it in Chapter 5 because the best implementation of quick sort is based on recursion. The version we will develop classifies an array of characters, but the logic can be adapted to classify any type of object.
Quick sort is based on the idea of partitions. The general procedure involves selecting a value, called comparing, and then dividing the array into two sections. All elements greater than or equal to the partition value are inserted on one side and smaller ones are inserted on the other. This process is repeated for each remaining section until the array is sorted. For example, given the array fedacb and using the value d as the compare, the first pass of quick sort would rearrange the array as shown below:
Initial f e d a c b
Passage 1 b c a d e f
This process is then repeated for each section – i.e. bca and def. As you can see, the process is essentially recursive in nature, and in fact, the cleanest implementation of quick sort is recursive.
You can select the comparison value in two ways. You can select it randomly or by finding the average of a small set of values taken from the array. To obtain an optimal classification, you must select a value that is exactly in the middle of the value range. However, it is not easy to do this for most datasets. The worst case is when the selected value is at one end. Even so, quick sort will run correctly.
The version of quick sort we will develop selects the middle element of the array as the comparison.
See QSDemo.java.
Quick Sort:
- One of the most efficient and widely used classification algorithms.
- Invented by C.A.R. Hoare.
- Based on the concept of partitions, where the array is divided into sections that are recursively sorted.
- More efficient than bubble sort and other simple methods.
Operation:
- Comparison Value (Pivot):
- A value is chosen as a reference (pivot) and the array is organized around that value.
- Elements smaller than the pivot go to one side and larger ones to the other.
- The process is repeated recursively for each section until the array is completely sorted.
Quicksort
QSDemo
The above is the detailed content of Try This Quick Sort. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
