10. Direct select sort and reverse sort

Source: Internet
Author: User

 Packagecom.xxx.xxx;/*** 1. Direct Select Sort * 2. Reverse sort *@authorAdministrator **/ Public classdemo11 {//Direct Select Sort    /*** Direct selection sort is faster than bubble sort * Basic idea: Compare the specified sort position with other array elements to meet on the exchange * (Note: Here the bubble sort is not exchanged, instead of exchanging adjacent elements, instead of exchanging the element that satisfies the condition with the specified sort position) * With the bubbling row The direct selection of the order is much less than the number of exchanges*/     Public Static voidMain (string[] args) {//Direct Select Sort        intindex; int[] array = {63,4,24,1,3,15};//creating an array is a random order         for(inti=1;i<=array.length;i++) {//start at 1, judging is less than or equal toindex = 0;  for(intj = 1;j<= array.length-i;j++) {//start at 1, judging is less than or equal, length-1                if(Array[j] > Array[index]) {//If Judgmentindex =J; }            }            //swap two numbers on position array_sort.length-1 and Index_sort (maximum)            inttemp = Array[array.length-i];//Save the first element in a temporary variableArray[array.length-i] = Array[index];//Save the second one in the firstArray[index] = temp;//Save the temporary variable (that is, the first one) in a second element        }        //Traverse Output         for(intK:array) {System.out.print (">" +k); } System.out.println ("\ n--------------------------"); //2. Reverse Sort        /*** Basic idea: Replace the last element of the array with the first element, the penultimate element replaces the second element, and so on, until all the array elements are reversed instead of the reverse sort is replaced by the elements on either side of the array, so only half of the loop's length is required * If the array length is 7, then the for loop only needs to loop 3 times*/        int[] A = {10,20,30,40,50,60}; //iterate output after array inversion         for(intx:a) {System.out.print (x+ "\ T");        } System.out.println (); inttemp; intlen = a.length;//Get Array length         for(intI=0;I&LT;LEN/2; i++) {//according to the algorithm, length except 2temp = A[i];//ExchangeA[i] = a[len-1-i]; A[len-1-i] =temp; }        //iterate output after array inversion         for(intx:a) {System.out.print (x+ "\ T"); }    }}

10. Direct select sort and reverse sort

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.