JAVA a quicksort implementation code

  • 2020-06-12 09:04:19
  • OfStack

There are many kinds of sorting methods: insertion sort, bubble sort, heap sort, merge sort, selection sort, counting sort, radix sort, bucket sort, quick sort, etc

Here is the main introduction to the method of quicksort, which I have read several articles before I understand:

1. First, select a random number from the set of data to be sorted as the cardinality: key;
2. Then sort the Numbers by putting the smaller ones to the left of key and the larger ones to the right of key, depending on the requirements (small to large, or large to small).
3, recursive to group, in the second step in the group of Numbers, divided into several groups to sort it

It might be easier to understand if you look at the code


package com.itheima;
/**
 * 4 ,   How many ways are there to sort? Please list. with JAVA implementation 1 Three quick sorts .
 *  Sorting methods are: bubble sort, quick sort, select sort, insert sort... 
 * 
 * @author 281167413@qq.com
 */
public class Test4 {
	static int count = 0;
	
	public static void main(String[] args) {
		int values[] = { 5, 4, 8, 3, 7, 2, 1, 9, 0, 6 };
		qsort(values, 0, (values.length - 1));
		System.out.printf("\n\n The sorted results are: ");
		for (int i = 0; i < values.length; i++) {
			System.out.printf("%d ", values[i]);
		}
	}
	public static void qsort(int values[], int left, int right) {
		int tmp = 0;
		System.out.printf("\n This is the first %d Results of secondary sorting: ", count);
		count++;
		for (int i = 0; i < values.length; i++) {
			System.out.printf("%d ", values[i]);
		}
		
		if (left < right) {
			tmp = partition(values, left, right);
			qsort(values, left, tmp);
			qsort(values, tmp + 1, right);
		}
	}
	public static int partition(int values[], int left, int right) {
		int i = 0, j = 0;
		int key = 0, tmp = 0;
		if (null == values) {
			return 0;
		}
		i = left;
		j = right;
		key = values[left];
		//  this while Loops can implement the sort of the first 1 Step: grouping 
		while (i < j) {
			while (values[j] > key) {
				--j;
			}
			tmp = values[i];
			values[i] = values[j];
			values[j] = tmp;
			while (values[i] < key) {
				i++;
			}
			tmp = values[i];
			values[i] = values[j];
			values[j] = tmp;
		}
		return i;
	}
}

Here are the results of each sort:


 This is the first 0 Results of secondary sorting: 5 4 8 3 7 2 1 9 0 6 
 This is the first 1 Results of secondary sorting: 0 4 1 3 2 5 7 9 8 6 
 This is the first 2 Results of secondary sorting: 0 4 1 3 2 5 7 9 8 6 
 This is the first 3 Results of secondary sorting: 0 4 1 3 2 5 7 9 8 6 
 This is the first 4 Results of secondary sorting: 0 2 1 3 4 5 7 9 8 6 
 This is the first 5 Results of secondary sorting: 0 1 2 3 4 5 7 9 8 6 
 This is the first 6 Results of secondary sorting: 0 1 2 3 4 5 7 9 8 6 
 This is the first 7 Results of secondary sorting: 0 1 2 3 4 5 7 9 8 6 
 This is the first 8 Results of secondary sorting: 0 1 2 3 4 5 7 9 8 6 
 This is the first 9 Results of secondary sorting: 0 1 2 3 4 5 7 9 8 6 
 This is the first 10 Results of secondary sorting: 0 1 2 3 4 5 7 9 8 6 
 This is the first 11 Results of secondary sorting: 0 1 2 3 4 5 7 9 8 6 
 This is the first 12 Results of secondary sorting: 0 1 2 3 4 5 7 9 8 6 
 This is the first 13 Results of secondary sorting: 0 1 2 3 4 5 6 7 8 9 
 This is the first 14 Results of secondary sorting: 0 1 2 3 4 5 6 7 8 9 
 This is the first 15 Results of secondary sorting: 0 1 2 3 4 5 6 7 8 9 
 This is the first 16 Results of secondary sorting: 0 1 2 3 4 5 6 7 8 9 
 This is the first 17 Results of secondary sorting: 0 1 2 3 4 5 6 7 8 9 
 This is the first 18 Results of secondary sorting: 0 1 2 3 4 5 6 7 8 9 

 The sorted results are: 0 1 2 3 4 5 6 7 8 9 

Related articles: