Java implementation of several sorting algorithms and java Sorting Algorithm
Many interview questions have asked about sorting algorithms. The intermediate algorithms and ideas are important. I have selected five common sorting algorithms and implemented them in Java. You can write a template to prevent future interviews. After reading the algorithm, you can implement it yourself.
1. Bubble Sorting: The large number is sunk, And the decimal number is floating up.
Package TestProject;/*** Bubble Sorting ** @ author xuhui **/public class SortAll {public static void main (String [] args) {int [] a =, 1, 2, 8, 6, 1}; bubbleSort (a); for (int I = 0; I <. length; I ++) {System. out. print (a [I] + "") ;}} public static void bubbleSort (int [] a) {int len =. length; // array length for (int I = 0; I <len; I ++) {// sink the maximum number of rows to the end of the array for (int j = 0; j <len-1; j ++) {if (a [j + 1] <a [j]) {// if the number behind the array is smaller than the previous one, int temp = a [j] will be sunk in the future. a [j] = a [j + 1]; a [j + 1] = temp ;}}}}}
1.1 Optimization of Bubble Sorting 1: Use a variable to record the position where each large number is sunk. The scalar value indicates that the array after the variable has been sorted.
Package TestProject;/*** bubble sort optimization * @ author xuhui **/public class SortAll {public static void main (String [] args) {int [] a =, 1, 2, 8, 6, 1}; bubbleSort_1 (a); for (int I = 0; I <. length; I ++) {System. out. print (a [I] + "") ;}} public static void bubbleSort_1 (int [] a) {int len =. length; // array length int I = len-1; while (I> 0) {int pos = 0; for (int j = 0; j <I; j ++) {// just sort a [0, 1 ,..., pos] if (a [j]> a [j + 1]) {int temp = a [j]; a [j] = a [j + 1]; a [j + 1] = temp; pos = j ;}} I = pos; // next stall, a [pos + 1 ,..., len-1] sorted }}}
1.2 Optimization of Bubble Sorting 2: sinking large numbers and floating decimal points down.
Package TestProject;/*** bubble sort optimization * @ author xuhui **/public class SortAll {public static void main (String [] args) {int [] a =, 1, 2, 8, 6, 1}; bubbleSort_2 (a); for (int I = 0; I <. length; I ++) {System. out. print (a [I] + "") ;}} public static void bubbleSort_2 (int [] a) {int len =. length; // array length int hight = len-1; int low = 0; while (low
2. Insert sorting: The general idea is to insert two arrays, an ordered array and an unordered array into an ordered series. Where a [0] as an ordered array, a [1,..., len-1] as an unordered array.
Package TestProject;/**** insert sorting * @ author xuhui **/public class SortAll {public static void main (String [] args) {int [] a =, 1, 2, 8, 6, 1}; insertSort (a); for (int I = 0; I <. length; I ++) {System. out. print (a [I] + "") ;}} public static void insertSort (int [] a) {int len =. length; // array length for (int I = 1; I <len; I ++) {int temp = a [I]; int j; for (j = I-1; j> = 0; j --) {// insert a [I] into a [0 ,.., i] if (a [j] <= temp) {break;} else {a [j + 1] = a [j]; // when an ordered array is inserted (from small to large), if the number of inserts is smaller than the current number, it is inserted forward, at this time, the number is moved one by one} a [j + 1] = temp; // The insert number is in the position of the ordered series }}}
3. Select sorting: place the smallest number of entries at the top of the array.
Package TestProject;/*** select sort * @ author xuhui **/public class SortAll {public static void main (String [] args) {int [] a = {0, 8, 1, 2, 8, 6, 1}; selectSort (a); for (int I = 0; I <. length; I ++) {System. out. print (a [I] + "") ;}} public static void selectSort (int [] a) {int len =. length; for (int I = 0; I <len; I ++) {int min = selectMin (a, I, len-1); int temp = a [min]; // change the minimum value of the unordered sequence to a [min] = a [I]; a [I] = temp;} public static int selectMin (int [], int s, int t) {int count = s; for (int I = s; I <= t; I ++) {if (a [I] <a [count]) count = I;} return count ;}}
3.1 select sorting optimization 1: Binary selection sorting. Change the maximum value of the unordered series to the back and the minimum value to the front each time.
Package TestProject;/*** select sort * @ author xuhui **/public class SortAll {public static void main (String [] args) {int [] a = {8, 1, 0, 2, 8, 6, 1}; selectSort (a); for (int I = 0; I <. length; I ++) {System. out. print (a [I] + "") ;}} public static void selectSort (int [] a) {int len =. length; int l = 0; int h = len-1; while (l
4. Heap sorting: first, create a maximum heap. The top element of the heap is the maximum value. Each time the top element of the heap is switched to the back of the array, an ordered array from small to large can be obtained in a loop.
Package TestProject;/*** heap sorting ** @ author xuhui **/public class SortAll {public static void main (String [] args) {int [] a =, 1, 2, 8, 6, 1}; int len =. length; for (int I = (len/2-1); I> = 0; I --) {// create a maximum heap maxHeapFixDown (a, I, len ); // create the maximum heap from the parent node with leaves, from bottom to top} for (int I =. length-1; I> = 0; I --) {// The top element of the heap is the maximum value of the heap. Every time you change the top element of the heap to the back of the array, the array is sorted from small to large in a loop. int temp = a [I]; a [I] = a [0]; a [0] = temp; maxHeapFixDown (, 0, I) ;}for (int I = 0; I <. length; I ++) {System. out. print (a [I] + "") ;}/ *** because we want to sort data from small to large, we need to create a maximum heap, starting from I to n-1, in this case, a [I]> a [I + 1 ,..., n-1] */public static void maxHeapFixDown (int [] a, int I, int n) {int temp = a [I]; int j = 2 * I + 1; while (j <n) {if (j + 1) <n & a [j] <a [j + 1]) j ++; if (a [j] <temp) break; a [I] = a [j]; I = j; j = 2 * j + 1 ;} a [I] = temp ;}}
5. Quick sorting: select the first element as the reference element, put the value smaller than this reference element in front, and put the value greater than this reference element in the back. The last position of each baseline element is the last position of the ordered series,
A [0 ,..., m-1] <a [m] <a [m + 1 ,.., n], and then from a [0 ,..., m-1 "Page, repeat the preceding operations.
Package TestProject;/*** fast ** @ author xuhui **/public class SortAll {public static void main (String [] args) {int [] a = {0, 8, 1, 2, 8, 6, 1}; int len =. length; quickSort (a, 0, len-1); for (int I = 0; I <. length; I ++) {System. out. print (a [I] + "") ;}} public static void quickSort (int [] a, int s, int t) {if (s <t) {int x = a [s]; int I = s; int j = t; while (I <j) {while (I <j & a [j]> = x) j --; int temp = a [I]; a [I] = a [j]; a [j] = temp; while (I <j & a [I] <= x) I ++; temp = a [j]; a [j] = a [I]; a [I] = temp;} quickSort (a, s, I-1); quickSort (a, I + 1, t );}}}
Here are all Java code implemented by referring to the algorithm description. It is difficult to understand the heap sorting. Some edge conditions may not be taken into account. If there are any errors, please correct them actively.