Quick sorting of 200 cases 66 for Java beginners

Keywords: Java Algorithm data structure quick sort

Introduction to the author

Author name: Ming Shiyin in programming world
Introduction: CSDN blog expert has been engaged in software development for many years and is proficient in Java and JavaScript. Bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to fight and upgrade with ADC. Welcome to pay attention and look forward to learning, growing and taking off with you!


Many Java beginners ask me that it's worrying for the novice to turn around and forget the Java knowledge he has studied very hard. How can Xiaobai grow up quickly and become a big cow?
In fact, there is only one skill to become a big God: "learn more and practice more", so brother Ming sorted out typical practice examples. Through practice, you can quickly improve coding skills and proficiency, so that you can never return on the way to become a big man (remember to practice with your own hands)!


✪ introduction to Java white 200 case series directory index
◄ previous article    65.Java sort() array sorting
► next    67. Select sort


Quick sort is a sort algorithm with high sorting efficiency. It uses divide and conquer to sort the sort sequence. Its idea is to separate the records to be sorted into two independent parts through one-time sorting. One part is smaller than the keyword and the latter part is larger than the keyword, and then sort the two parts in this way respectively, Through recursive operation, the order of the whole sequence is finally achieved.

Quick arrangement idea

  1. Suppose we do a quick sort on the array {66, 3, 11, 100, 99, 37, 85}.
  2. First, find an element in this array as the reference number, and take the first number 66 from the beginning.
  3. Traverse the array, place those less than the reference number on the left of the reference number, and those greater than the reference number on the right of the reference number to obtain similar {37, 3, 11, 66, 99, 100, 85}.
  4. At this time, both sides of 66 correspond to two paragraphs, which can be understood as two arrays {37,3,11} and {99100,85}.
  5. The recursive algorithm is used to sort the two sub arrays.

Implementation code

There are detailed comments in the code, so I won't explain it otherwise

package demo.demo66;

public class Quick {

	public static void main(String[] args) {
		int[] arr = { 66, 3, 11, 100, 99, 37, 85 };

		System.out.println("Before sorting:");
		for (int val : arr) {
			System.out.print(val + " ");
		System.out.println("\n After sorting:");
		for (int val : arr) {
			System.out.print(val + " ");
	//Quick sort entry
	private static void doQuick(int[] arr) {
		if (arr.length > 0) {
			doQuickSort(arr, 0, arr.length - 1);
	//Perform quick sort
	private static void doQuickSort(int[] arr, int start, int end) {
		if(start < end) {
			// Get intermediate cut-off point
			int middle = getMiddle(arr, start, end);
			//Low direction sort (recursive)
			doQuickSort(arr, start, middle - 1);
			//High order (recursive)
			doQuickSort(arr, middle + 1, end);

	// Divide the array into high and low parts
	private static int getMiddle(int[] arr, int start, int end) {
		//Get temporary data
		int tmp = arr[start];
		while (start < end) {
			//Starting from the far right, once it is found that there is something smaller than tmp, exit the while loop
			while (start < end && arr[end] > tmp) {
			//At this time, the end obtained is smaller than the temporary central axis and needs to be exchanged
			arr[start] = arr[end]; // Records smaller than the center axis move to the lower direction
			//Starting from the far left, once it is found that it is larger than tmp, exit this while loop
			while (start < end && arr[start] < tmp) {
			//At this time, the start obtained is larger than the temporary central axis. Exchange data
			arr[end] = arr[start]; // Records larger than the center axis are moved to the high end
		//Exchange center axis data to center axis position
		arr[start] = tmp;
		//Return to center axis position
		return start;


Operation results:

Before sorting:
66 3 11 100 99 37 85
After sorting:
3 11 37 66 85 99 100


This section summarizes the "quick sorting", hoping to be helpful to you. Please help [like] + [collection] + [punch in the comment area]. If you are interested in learning Java with brother Xiaoming, [pay attention to a wave] won't get lost.

Let me know you by punching in the comment area. Mingge will continue to pay attention to your learning progress!


✪ introduction to Java white 200 case series directory index
◄ previous article    65.Java sort() array sorting
► next    67. Select sort

Popular column recommendation

1.Java game series (Tetris, aircraft war, plant war, zombie, etc.)
2.JavaWeb project practice (library management, online examination, dormitory management system, etc.)
3. Wonderful examples of JavaScript (aircraft war, minesweeping, snake eating, verification code, etc.)
4. Introduction to Java Xiaobai 200 cases
5. Learn Java from zero, learn Java with interest, and learn Java from the perspective of King glory

Posted by colossus_09 on Sat, 25 Sep 2021 17:28:59 -0700