Java programming example code based on three algorithm questions of quick sorting, java example
Overview
Quick sorting is an update of the Bubble sorting we have learned before. They all belong to the exchange sorting class and are sorted by continuous comparison and movement. Quick sorting is a very efficient sorting algorithm, which increases the distance between Record Comparison and moving, and moves records with large keywords from the front to the back, A record with a small keyword moves directly from the back to the front, reducing the total number of comparisons and moves. At the same time, the idea of "divide and conquer" is used to split the big one into small ones and small ones into smaller ones. The principle is as follows: for a given set of records, select a benchmark element, generally, the first or last element is selected. After a scan, the columns to be sorted are divided into two parts, one of which is smaller than the benchmark element and the other is greater than or equal to the benchmark element, at this time, the reference element is in the correct position after sorting, and then uses the same method to recursively sort the two parts until all records in the sequence are ordered.
1. Minimum k count
Enter n numbers to find the minimum k number. For example, if you enter 4, 5, 2, 7, 3, 8, the minimum number is 1, 2, 3, 4.
The O (n)-based algorithm can solve this problem using the Partion function. If the k Number is adjusted based on the array, so that all numbers smaller than the k number are on the left of the array, and all numbers larger than the k array are on the right of the array, in this way, the k numbers on the left of the array are the smallest k numbers, which are not necessarily ordered.
Import java. util. role; public class Main {public static void main (String [] args) {role in = new role (System. in); int n = in. nextint (); int k = in. nextint (); int [] num = new int [n]; int [] out = new int [k]; for (int I = 0; I <n; I ++) {num [I] = in. nextint () ;}boolean B = GetMinK (n, num, k, out); if (B) {for (int I = 0; I <k; I ++) {System. out. print (out [I] + "") ;}} public static Boolean GetMinK (int uiInputNum, int [] pInputArray, int UiK, int [] pOutputArray) {if (pInputArray = null | pOutputArray = null | uiK> uiInputNum | uiInputNum <= 0 | uiK <= 0) {return false;} int start = 0; int end = uiInputNum-1; int index = partition (pInputArray, start, end); while (index! = UiK-1) {if (index> uiK-1) {// index on the right of the K-1 end = index-1; index = partition (pInputArray, start, end );} else {start = index + 1; index = partition (pInputArray, start, end) ;}}for (int I = 0; I <uiK; I ++) {pOutputArray [I] = pInputArray [I];} return true;} // partion partition function, returns the index value index public static int partition (int [] a, int start, int end) of the first element of array a. {int privot = a [start]; int I = start; int j = end; while (I <j) {while (I <j & privot <= a [j]) {j --;} swap (a, I, j); while (I <j & privot> = a [I]) {I ++;} swap (a, I, j );} return I;} public static void swap (int [] a, int I, int j) {int t = a [I]; a [I] = a [j]; a [j] = t ;}}
2. the number that appears more than half in the array
There is a number in the array that appears more than half the length of the array. Please find this number. For example, 1, 2, 3, 2, 2, 5, 4, 2, and number 2 appear 5 times in the array. If the length exceeds half of the length of the array, 2 is output.
Inspired by quick sorting, in quick sorting, select a number in the array and adjust the order of the numbers in the array so that all the numbers smaller than the selected number are on the left of the array, a number that is larger than the selected number is placed on the right of the selected number.
If the subscript of the selected number is n/2, the number is the median in the array.
Import java. util. role; public class Main {public static void main (String [] args) {role in = new role (System. in); int n = in. nextint (); int [] num = new int [n]; for (int I = 0; I <n; I ++) {num [I] = in. nextint ();} int B = GetHalfNum (n, num); if (B! =-1) {System. out. println (B) ;}} public static int GetHalfNum (int uiInputNum, int [] pInputArray) {if (pInputArray = null | uiInputNum <= 0) {return-1 ;} int middle = uiInputNum> 1; // The length of half int start = 0; int end = uiInputNum-1; int index = partition (pInputArray, start, end); while (index! = Middle) {// if the length is not equal to half, the median if (index> middle) {end = index-1; index = partition (pInputArray, start, end) ;}else {start = index + 1; index = partition (pInputArray, start, end) ;}} return pInputArray [index]; // index = middle} public static int partition (int [] a, int start, int end) {int privot = a [start]; int I = start; int j = end; while (I <j) {while (I <j & privot <= a [j]) {j --;} swap (a, I, j); while (I <j & privot> = a [I]) {I ++;} swap (a, I, j) ;}return I ;} public static void swap (int [] a, int I, int j) {int t = a [I]; a [I] = a [j]; a [j] = t ;}}
3. Find the minimum k number in the array.
For example, in an array of 4th, and 6, the value of is 5.
Import java. util. role; public class Main {public static void main (String [] args) {role in = new role (System. in); int n = in. nextint (); int k = in. nextint (); int [] num = new int [n]; // int [] out = new int [k]; for (int I = 0; I <n; I ++) {num [I] = in. nextint ();} int B = GetMinK (n, num, k); if (B! =-1) {System. out. println (B) ;}} public static int GetMinK (int uiInputNum, int [] pInputArray, int uiK) {if (pInputArray = null | uiK> uiInputNum | uiInputNum <= 0 | uiK <= 0) {return-1;} int start = 0; int end = uiInputNum-1; int index = partition (pInputArray, start, end); while (index! = UiK-1) {// if the index is not the location of the K-1 if (index> uiK-1) {end = index-1; index = partition (pInputArray, start, end );} else {start = index + 1; index = partition (pInputArray, start, end) ;}} return pInputArray [index]; // return the value of this position} public static int partition (int [] a, int start, int end) {int privot = a [start]; int I = start; int j = end; while (I <j) {while (I <j & privot <= a [j]) {j --;} swap (a, I, j); while (I <j & privot> = a [I]) {I ++;} swap (a, I, j) ;}return I ;} public static void swap (int [] a, int I, int j) {int t = a [I]; a [I] = a [j]; a [j] = t ;}}
Summary
The above is all the content of the sample code for Java programming based on the three algorithm questions of quick sorting. I hope it will help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!