Quick sort for advanced sorting

Before writing quick sort, we can write about division. The purpose of division is that the array is basically ordered. How to make the array basically ordered? This requires us to select a value as the pivot in advance, as the key value for dividing the array, so that large elements and small elements are distributed on both sides of the pivot, which requires us to have a general understanding of the elements in the array in advance.

//divide algorithm

public int parititon(int low, int pivot, int up) {//low is the first element of the array, and pivot is our pivot
		int left = low-1;
		int right = up;
		while(true)
		{
            //An array starts to traverse the elements from both sides. When the element on the left encounters a larger than the pivot (we record it as a), it jumps out, followed by the right side. When the element on the right encounters a smaller than the pivot (remarked as b), it jumps out
			while(array[++left]<pivot);
			//As can be seen in the complete program below, the pivot is to take the rightmost element of the array, so a limit is added when traversing from right to left, that is, right>0, and from left to right, you don't need to worry about it. , it has been said before that the pivot is reasonable (will be described later), and the difference in pivot value will affect the determination of the boundary
			while(array[--right]>pivot&&right>0);
			//When jumping out of the loop, first judge whether the left value is greater than or equal to the right, it means that the traversal has ended and you can jump out of the loop, otherwise, swap a, b
			if(left>=right)
				break;
			else
				swap(left,right);
				
		}
		return left;
	}
Based on the division algorithm, quicksort uses recursion to continuously divide the original array, and the divided array calls the division algorithm until all the arrays are sequenced. The pivot takes the rightmost element of the array every time, and at the end of calling the division algorithm, it needs to be exchanged with the larger element on the right to obtain a more reasonable pivot. In fact, this is not a good way to take the pivot value. One is to take the three-item pivot. Before sorting, make a comparison of the front, middle and back, and get a more reasonable pivot. You can try it and just make some small changes in the following code.

package csnd;



public class QuickSort {
	int array[];
	int nElems;
	public QuickSort() {
		nElems = 0;
	}
	public QuickSort(int max)
	{
		array = new int[max];
	}
	public void insert(int j)
	{
		array [nElems] = j;
		nElems++;//Note that when the last element is inserted, the nElems value will also increase by one
	}
	public void display(){
		for(int i=0;i<nElems;i++)
			System.out.print(array[i]+" ");
		
		System.out.println();
	}
	private void quickSort() {
		// TODO Auto-generated method stub
		requickSort (0, nElems-1);

	}
	public void requickSort(int low,int up) {
		if(low>=up)
		{
			return;
		}
		else
		{
			int pivot = array[up];
			int partition = parititon(low,pivot,up);
			requickSort(low, partition-1);
			requickSort(partition+1, up);
		}

	}
	public int parititon(int low, int pivot, int up) {
		int left = low-1;
		int right = up;
		while(true)
		{
			while(array[++left]<pivot);
			
			while(array[--right]>pivot&&right>0);
			
			if(left>=right)
				break;
			else
				swap(left,right);
				
		}
		swap(left,up);//At this time, exchange with the rightmost element to get a new array
		return left;
	}
	private void swap(int left, int right) {
		int temp = array[left];
		array[left]= array[right];
		array[right]=temp;
		
	}
	public static void main(String[] args) {
		QuickSort theArray = new QuickSort(10);
		for(int i=0;i<10;i++)
		{
			int n=(int)(Math.random()*(10));
			theArray.insert(n);
		}
		System.out.println("Array before sorting");
		theArray.display();
		System.out.println("sorted array");
		theArray.quickSort();
		theArray.display();
	}
	}


Array before sorting
3 9 1 5 2 6 2 0 0 0
Array after sorting
0 0 0 1 2 2 3 5 6 9



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325902912&siteId=291194637