Java uses several classic algorithms to sort arrays.

Source: Internet
Author: User

Java uses several classic algorithms to sort arrays.

Sort arrays in Java

 

Package com. souvc. hibernate. exp; public class MySort {/*** Method Name: main </br> * Details: Java implements array sorting </br> * developer: liuhf </br> * Creation Time: 2016-3-22 </br> * @ param args * @ throws * @ since V1.0 */public static void main (String [] args) {int array [] = {45, 32, 54,12, 43,65, 11,3, 43,6, 33,90, 44,1, 178}; MySort mySort = new MySort (); mySort. insertSort (array); System. out. print ("insert sorting result:"); mySort. printArray (array); System. Out. println (); mySort. bubbleSort (array); System. out. print ("Bubble sorting result:"); mySort. printArray (array); mySort. qsort (array); System. out. println (); System. out. print ("quick sorting result:"); mySort. printArray (array); mySort. shellSort (array); System. out. println (); System. out. print ("Hill sorting result:"); mySort. printArray (array); mySort. selectSort (array); System. out. println (); System. out. print ("select sorting result:"); mySort. printArray (Rray);}/*** insert sort directly * Basic Idea: In the number of groups to be sorted, assume the preceding (n-1) [n> = 2] The number is already sorted. Now we need to insert the nth number into the previous ordered number so that the n number is also sorted. This repeats until the order */public void insertSort (int [] array) {int temp = 0; for (int I = 1; I <array. length; I ++) {int j = I-1; temp = array [I]; for (; j> = 0 & temp <array [j]; j --) {array [j + 1] = array [j]; // move the value greater than temp to a unit.} array [j + 1] = temp ;}} /* Bubble Sorting * Basic Idea: In the number of a group to be sorted, the number of all the numbers in the range not sorted yet, from top to bottom, compare and adjust the adjacent two numbers in sequence, so that a large number will sink and a small number will go up. That is, when the numbers of two adjacent parties are compared and their sorting and sorting requirements are opposite, they are exchanged. */Public void bubbleSort (int [] array) {int temp; for (int I = 0; I <array. length; I ++) {// Number of integers for (int j = 0; j <array. length-i-1; j ++) {// compare times if (array [j]> array [j + 1]) {temp = array [j]; array [j] = array [j + 1]; array [j + 1] = temp ;}}}/*** Quick Sort * Basic Idea: 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 divided two parts. * @ Param array */public void qsort (int array []) {if (array. length> 1) {_ qsort (array, 0, array. length-1) ;}}/*** Quick Sort * @ param array */private void _ qsort (int [] array, int low, int high) {if (low 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.