Quick sort - java implementation

quicksort

    Its basic idea is: divide the data to be sorted into two independent parts by one sorting, all the data in one part is smaller than all the data in the other part, and then use this method to quickly perform the two parts of the data respectively. Sorting, the entire sorting process can be performed recursively , so that the entire data becomes an ordered sequence .

The basic steps are:
1. Set the keyword, divide it into two arrays, put the smaller than the keyword on one side, and put the larger one on the other side
2. We choose to set the rightmost end of the array as the keyword

3. Recursively implement quick sorting, and quickly sort each subarray by recursion

Let's look at an example:

    There is a set of int type data: arr[ 6 ] = { 9 , 2 , 7 , 3 , 8 , 6 } ;

    Then when doing quick sort:

    First set two pointers, move towards the middle from the far left and far right, stop when they are larger (smaller) than the keyword, then switch positions, and continue to move towards the middle. Get the sort entry point (the ith position).

    The first round: 6 is the keyword, and the array is divided (not sorted): (the substring in the curly brackets)

                {2, 3} and {9, 7, 8} and then insert 6 into the middle ith (2) position,

         Result: {2, 3}, 6, {9, 7, 8}

    Second round: perform a quick sort from 0 to i (select 3 as a keyword); perform a quick sort from i +1 to arr.length - 1 (select 8 keywords)

         Result: {2}, 3, 6, {7}, 8, {9}

    Results of the third round: Quickly sort the 3 substrings (same as above)

         Results: 2, 3, 6, 7, 8, 9

Quicksort is complete.

Then the next code implementation: (java)

//quick sort
// Divide into two arrays and quickly sort each sub-array by recursion
//1. Set the keyword, put it on one side if it is smaller than the keyword, and put the bigger one on the other side
//2. Set the rightmost end of the array as the keyword
//3. Recursive implementation of quick sort
public class QuickSort {
	
	//divide the array
	public static int part(long[] arr , int left , int right , long point) {
		//two pointers
		int leftptr = left - 1;
		int rightptr = right;
		
		while(true) {
			//Start looking from the far left and the far right, put the one larger than the point on the right, and put the one smaller than the point on the left
			while(leftptr < rightptr && arr[++leftptr] < point);
			while(leftptr < rightptr && arr[--rightptr] > point);
			
			// found and exchanged, and then continue to find
			if(leftptr >= rightptr) {
				break;
			}
			else {//Exchange
				long tmp = arr[leftptr];
				arr[leftptr] = arr[rightptr];
				arr[rightptr] = tmp;
			}
		}
		//After the array is divided, insert the keyword into the middle
		long tmp = arr[leftptr];
		arr[leftptr] = arr[right];
		arr[right] = tmp;
		
		//return to entry point
		return leftptr;
	}
	
	public static void sort(long[] arr , int left , int right) {
		if (right <= left) {
			return;
		}
		else {
			//Set the rightmost key as the keyword
			long point = arr[right];
			//Get the entry point and divide at the same time
			int part = part(arr, left, right, point);
			// recursively sort the left array
			sort(arr, left, part - 1);
			//sort right
			sort(arr, part + 1, right);
		}
		
	}
	
	// print the array
	public static void displayArr(long[] arr) {
		for(int i = 0 ; i < arr.length ; i++) {
			System.out.print(arr[i] + " ");
		}
		System.out.println();
	}
}

Test class: Here, 10 numbers are randomly generated for sorting

public class TestQuickSort {

	public static void main(String[] args) {
		// TODO auto-generated method stub
		long[] arr = new long[10];
		for(int i = 0 ; i < arr.length ; i++) {
			arr[i] = (long) (Math.random() * 99);
		}
		long point = arr[arr.length - 1];
		
		QuickSort.displayArr(arr);
		
//		QuickSort.part(arr, 0, arr.length - 1, point);
		
//		QuickSort.displayArr(arr);
		
		QuickSort.sort(arr, 0, arr.length - 1);
		
		QuickSort.displayArr(arr);
	}
}

Guess you like

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