Java sorting algorithms are: Insert sort (direct insert sort, hill Sort), select sort (direct selection sort, heap sort), swap sort (bubble sort, quick sort), merge sort, base sort
The comparison of the Java sorting algorithm, each algorithm has its own advantages and disadvantages, for the choice of what sort algorithm, which should be based on the size of the problem and whether the data is ordered to choose a better sorting algorithm.
The following is the summary code for the various sorting algorithms:
Bubble Sort:
Package Com.wj.interview;import Java.util.arrays;import java.util.scanner;//write a bubbling program in Java that requires 10 integers to be entered, and the output sort result public Class InterviewSort1 {/** * @param args *///get input stream public static Scanner Getscnner () {Scanner scanner=new Scanner (system.in) ; return scanner;} Returns the input integer public static int getInt (Scanner Scanner) {int num=scanner.nextint (); return num;} /* Exchange Sort the idea is: 22 compare the keywords to be sorted, find two records in reverse order to exchange sort *, until there is no reverse order of the records. The exchange sort has a bubbling sort and a quick sort * bubble sort belongs to the interchange sort and is a stable sorting algorithm. *///the function for bubbling sorting, the best time to bubble sort is O (n), best and Average O (n^2) public static void Sortarray (int array[]) {int temp;//interchange intermediate variable int count=0;// The number of times the loop is Boolean flag=false;//flag variable, if the array is ordered, then flag is true not looping for (int i=0;i<array.length-1&&!flag;i++) { flag=true;for (int j=0;j<array.length-i-1;j++) {count++;if (array[j]>array[j+1]) {temp = array[j];array[j]= Array[j+1];array[j+1]=temp;flag=false;}}} System.out.println ("Count:" +count+ ";" +arrays.tostring (array));} public static void Main (string[] args) {//TODO auto-generated method Stub//int array[]=new int[]{ 2,1,6,5,44,34,7,22,12,10};int Array[]=new int[10]; Scanner Sc=getscnner (); System.out.println ("Please enter 10 integers to sort:"); for (int i=0;i<10;i++) {Array[i]=getint (SC);} System.out.println ("10 numbers you have entered for:"); System.out.println (arrays.tostring (array)); System.out.println ("--------------------"); Sortarray (array); SYSTEM.OUT.PRINTLN ("The result after sorting is:"); System.out.println (arrays.tostring (array));}}
Quick sort:
Package Com.wj.interview;import Java.util.arrays;import java.util.scanner;//write a bubbling program in Java that requires 10 integers to be entered, and the output sort result public Class InterviewSort1 {/** * @param args *///get input stream public static Scanner Getscnner () {Scanner scanner=new Scanner (system.in) ; return scanner;} Returns the input integer public static int getInt (Scanner Scanner) {int num=scanner.nextint (); return num;} /* Exchange Sort the idea is: 22 compare the keywords to be sorted, find two records in reverse order to exchange sort *, until there is no reverse order of the records. The exchange sort has a bubbling sort and a quick sort * bubble sort belongs to the interchange sort and is a stable sorting algorithm. *///the function for bubbling sorting, the best time to bubble sort is O (n), best and Average O (n^2) public static void Sortarray (int array[]) {int temp;//interchange intermediate variable int count=0;// The number of times the loop is Boolean flag=false;//flag variable, if the array is ordered, then flag is true not looping for (int i=0;i<array.length-1&&!flag;i++) { flag=true;for (int j=0;j<array.length-i-1;j++) {count++;if (array[j]>array[j+1]) {temp = array[j];array[j]= Array[j+1];array[j+1]=temp;flag=false;}}} System.out.println ("Count:" +count+ ";" +arrays.tostring (array));} Fast sorting is a binary tree structure of the Exchange sorting algorithm//fast sorting algorithm for the best case of Time complexity O (NLBN), the worst is O (n*n), the average time complexity is O (NLBN)//fast sorting algorithm for the worst case space complexity of O (n), The fast sorting algorithm is an unstable sort algorithm public static void QuickSort (int array[],int low,int hight) {//low is a low-end subscript for an array, hight an array of high-end subscript int I=low,j=hight;int Temp=array[low ];//takes the first element as the standard data element while (I<J) {while (I<j&&temp<=array[j]) {//scans the right end of the array j--;} if (i<j) {array[i]=array[j];i++;} while (I<j&&temp>array[i]) {//scan i++ on the left side of the array;} if (i<j) {array[j]=array[i];j--;}} Array[i]=temp;if (low<i) {quickSort (array,low,i-1);//left Dial Hand collection recursively}if (i
Output Result:The 10 numbers you have entered are:
[2, 1, 6, 5, 44, 34, 7, 22, 12, 10]
--------------------
count:35; [1, 2, 5, 6, 7, 10, 12, 22, 34, 44]
The result of bubbling sorting is:
[1, 2, 5, 6, 7, 10, 12, 22, 34, 44]
---------------------------
The result of a quick sort is:
[1, 2, 5, 6, 7, 10, 12, 22, 34, 44]
Direct Selection Sort:
Package Com.wj.interview;import Java.util.arrays;import java.util.scanner;//Write a selection sorter in Java and ask for 10 integers to be entered. Output sort result public class InterviewSort2 {/** * @param args *///get input stream public static Scanner Getscnner () {Scanner scanner=new Scann ER (system.in); return scanner;} Returns the input integer public static int getInt (Scanner Scanner) {int num=scanner.nextint (); return num;} /* Select Sort is a simple and intuitive sorting algorithm. It works as follows: first find the smallest element in an unordered sequence, store it in the starting position of the sorting sequence, and then continue looking for the smallest element from the remaining unsorted elements, placing * at the end of the sort sequence. And so on, until all the elements are sorted. * Select sort has direct selection sorting and heap sorting. * *///to select the sorting function//Direct Select sorting algorithm time complexity is O (n^2), spatial complexity is O (1), direct selection of the ordering is unstable algorithm public static void Sortarray (int array[]) {int small, temp;for (int i=0;i<array.length-1;i++) {small=i;//Set the first Data element keyword minimum for (int j=i+1;j<array.length;j++) {// Look for the smallest data element if (Array[j]<array[small]) {small=j;//remembers the lowest element subscript}}if (small!=i) {//when the lowest element of the following table is not swapped for position temp=array[i];array[ i]=array[small];array[small]=temp;}}} public static void Main (string[] args) {//TODO auto-generated method Stubint array[]=new int[]{2,1,6,5,44,34,7,22,12,10} ;/*int Array[]=new int[10]; Scanner sc=Getscnner (); System.out.println ("Please enter 10 integers to sort:"); for (int i=0;i<10;i++) {Array[i]=getint (SC);} */system.out.println ("10 numbers you have entered for:"); System.out.println (arrays.tostring (array)); System.out.println ("--------------------"); Sortarray (array); System.out.println ("Direct selection of the result after sorting is:"); System.out.println (arrays.tostring (array));}}
Output Result:The 10 numbers you have entered are:
[2, 1, 6, 5, 44, 34, 7, 22, 12, 10]
--------------------
The result of direct selection sorting is:
[1, 2, 5, 6, 7, 10, 12, 22, 34, 44]
Insert Sort algorithm:
Package Com.wj.interview;import Java.util.arrays;import java.util.scanner;//write an Insert sort program in Java that requires 10 integers to be entered, Output sort result public class InterviewSort3 {/** * @param args *///get input stream public static Scanner Getscnner () {Scanner scanner=new Scann ER (system.in); return scanner;} Returns the input integer public static int getInt (Scanner Scanner) {int num=scanner.nextint (); return num;} /* Basic idea of inserting sorting: from the initial ordered subset, the new data elements are constantly inserted into the appropriate position of the ordered sub-sets, so that the number of data elements in the * sub-collection is increasing, and when the sub-collection equals the collection, the insertion sort algorithm ends. The commonly used insertion sort has a direct insert sort and a hill sort of two. * * Select sort has direct selection sorting and heap sorting. * *//* The basic idea of direct insertion sorting: Sequentially inserts the data element to be sorted into the appropriate position of the sorted data sub-collection by the size of its key value. The number of data elements for a sub-collection starts with only one * data element, which is incremented successively, and is sorted when the child collection size is eventually the same as the collection size. * The best case time complexity is O (n), the best case is O (n^2), the original data elements set closer to the order, the direct insertion of the order of the time more efficient * space complexity is O (1), direct insertion is a stable sorting algorithm. * */public static void Sortarray (int array[]) {int temp,j;for (int i=0;i<array.length-1;i++) {//n-1 secondary Insert temp=array[i+ 1];j=i;while (J>-1&&temp<array[j]) {//Find the appropriate location array[j+1]=array[j];j--;} array[j+1]=temp;//inserting}}////Please write a hill sort program in Java, ask for 10 integers, output sort results */* The basic idea of the sort of Hill: divide the data elements to be sorted into groups, sort the data elements in the same group with the direct insertion method The number of groups is reduced by successive *When all the data elements are sorted within a group, the sorting process is finished. The hill sort is called narrowing the incremental sort. * * The time complexity of hill sorting is much better than the time complexity of inserting the sorting algorithm directly, and the time of the hill sort depends on the number of increments of the order and the increment. If the value is reasonable, the time complexity of the hill sorting algorithm is about O (n (LBN) * (LBN)) and the space complexity is O (1); * The hill sorting algorithm is sorted by incremental grouping, so the hill sort algorithm is an unstable sorting algorithm * */public static void shell (int array[],int d[]) {//Sort data Elements array[] with the hill sort method, d[] The increment value for the sort int span,temp,j;//for (int m=0;m<d.length;m++) {///loop number of times the number of groups span=d[m];//each increment value for (int k=0;k<span;k++) { The group is directly inserted in the sort, the difference is not to increase the 1 each time but increase spanfor (int i=k;i<array.length-span;i=i+span) {temp=array[i+span];j=i;while (j>-1 &&temp<array[j]) {Array[j+span]=array[j];j=j-span;} array[j+span]=temp;}}}} public static void Main (string[] args) {//TODO auto-generated method Stubint array[]=new int[]{2,1,6,5,44,34,7,22,12,10} ; int array2[]=new int[]{2,1,6,5,44,34,7,22,12,10};int d[]=new int[]{4,3,2,1};/*int array[]=new int[10]; Scanner Sc=getscnner (); System.out.println ("Please enter 10 integers to sort:"); for (int i=0;i<10;i++) {Array[i]=getint (SC);} */system.out.println ("10 numbers you have entered for:"); System.out.println (arrays.tostring (array)); SysteM.out.println ("--------------------"); Sortarray (array); SYSTEM.OUT.PRINTLN ("The result of direct insert sorting is:"); System.out.println (arrays.tostring (array)); System.out.println ("-----------------------"); Shell (array2,d); System.out.println ("Hill-sort Result:"); System.out.println (arrays.tostring (Array2));}}
Output Result:The 10 numbers you have entered are:
[2, 1, 6, 5, 44, 34, 7, 22, 12, 10]
--------------------
The result of the direct insert sort is:
[1, 2, 5, 6, 7, 10, 12, 22, 34, 44]
-----------------------
Result of Hill Sort:
[1, 2, 5, 6, 7, 10, 12, 22, 34, 44]
Merge sort:
Package Com.wj.interview;import Java.util.arrays;import java.util.scanner;//Write a merge sort program in Java that requires 10 integers to be entered. Output sort result public class InterviewSort4 {/** * @param args *///get input stream public static Scanner Getscnner () {Scanner scanner=new Scann ER (system.in); return scanner;} Returns the input integer public static int getInt (Scanner Scanner) {int num=scanner.nextint (); return num;} /* Merge sort basic idea: Set the array A has n data elements, initially they as the formula n length of 1 ordered sub-array, * then start from the first ordered sub-array, the adjacent ordered sub-array 22 is merged to get the whole [N/2] length of 2 of the new ordered sub-array (when n is odd, The last new ordered Subarray has a length of 1). 22 * Merging of these new ordered sub-arrays. This is repeated until a sequential array of length n is obtained. * *//* a two-way merge sorting algorithm is as follows: * */public static void merge (int array[],int swap[],int k) {//k is the length of an ordered subarray, once two-way merge sorted ordered sub-sequence stored in the array swap int lowfirst=0;//the first ordered Subarray has a lower bound of 0int hightfirst;//the first ordered Subarray upper bound int lowseconde;//the upper bound of the second subarray int hightseconde;//the lower bound int of the second ordered number of integers m=0 , I,j;while (lowfirst+k<=array.length-1) {lowseconde=lowfirst+k;//calculates the lower bound of the second ordered sub-array hightfirst=lowseconde-1;// Computes the upper bound of the first ordered sub-array//computes the upper bound of the second ordered Subarray hightseconde= (lowseconde+k-1<=array.length-1)? lowseconde+k-1:array.length-1;// Two ordered sub-arrays merged for (i=lowfirst,j=lowseconde;i< =hightfirst&&j<=hightseconde;m++) {if (Array[i]<=array[j]) {swap[m]=array[i];i++;} else{swap[m]=array[j];j++;}} Sub-array 2 has been merged, the remaining elements of sub-array 1 are stored in the array swap while (I<=hightfirst) {swap[m]=array[i];m++;i++;} Sub-array 1 has been merged, the remaining elements of sub-array 2 are stored in the array swap while (J<=hightseconde) {swap[m]=array[j];m++;j++;} lowfirst=hightseconde+1;} Put only one set of data elements in the original array into the array swap for (i=lowfirst;i<array.length;i++,m++) {swap[m]=array[i];}} The two-way merge sorting algorithm is as follows://Two-way merge sort time complexity is O (nlbn), spatial complexity is O (n), two-way merge sort is not only time complexity is O (NLBN) but also is a stable sorting algorithm//(this is the largest feature of the two-way merge sorting algorithm) public static void MergeSort (int array[]) {int k=1;//merge length starting from 1 int swap[]=new int[array.length];while (k<array.length) {merge (array,swap,k);//Call the merge function for (int i=0;i<array.length;i++) {array[i]=swap[i];//to place the element from the temporary array swap back into the array of arrays}k=k*2;// Merge length double}}public static void Main (string[] args) {//TODO auto-generated method Stubint array[]=new int[]{2,1,6,5,44,34,7,2 2,12,10};int array2[]=new int[]{2,1,6,5,44,34,7,22,12,10,-2,90,-3};int d[]=new int[]{4,3,2,1};/*int array[]=new int[ 10]; Scanner Sc=getscnner (); System.out.println ("Please enter 10 integers to sort:"); for (int i=0;i<10;i++) {Array[i]=getint (SC);} */system.out.println ("10 numbers you have entered for:"); System.out.println (arrays.tostring (array)); System.out.println ("--------------------"); MergeSort (array2); SYSTEM.OUT.PRINTLN (The result of merge sort is: "); System.out.println (arrays.tostring (array2)); System.out.println ("-----------------------");}}
Output Result:The 10 numbers you have entered are:
[2, 1, 6, 5, 44, 34, 7, 22, 12, 10]
--------------------
The result of the merge sort is:
[-3,-2, 1, 2, 5, 6, 7, 10, 12, 22, 34, 44, 90]
-----------------------
This article original, from: http://blog.csdn.net/j903829182/article/details/38566457