Java array sorting (bubbling, vertical) inversion

Source: Internet
Author: User

Package Lianxi;public class Maopao {public static void main (string[] args) {int[] i=new int[]{45,67,54,35,26,76,85}; int a=0;a<i.length;a++) {for (int b=0;b<i.length;b++) {if (i[a]<i[b]) {int temp=i[a];i[a]=i[b];i[b]=temp;}}} Go (i); } public static void Go (int[] l) {for (int o=0;o<l.length;o++) {System.out.print (l[o]+ "");}}}

The bubble sort is to use the first layer of two for loops to loop through each loop and compare the second loop with the rest of the array until the loop is finished, and the sort is done.

Package day24;//Direct Sort public class Paixu {public static void main (string[] args) {int[] i=new int[]{45,35,67,86,46,25,47,37 , 85};//for (int m=0;m<i.length;m++) {int indexmax=m;for (int n=m+1;n<i.length;n++) {if (I[indexmax]<i[n]) { indexmax=n;//Replace the first index in the loop with the index of the maximum value}}int temp=i[m];i[m]=i[indexmax];//assigns the maximum value to the first value in the loop. i[indexmax]=temp;//Place the first value in a different position}xunhuan (i);} public static void Xunhuan (int[] t) {for (int l=0;l<t.length;l++) {System.out.println (t[l]);}}}

Direct ordering is to take the number to compare to the position should be placed, until the end of the cycle

Package lianxi;//Reverse Sort public class Fanzhuan {public static void main (string[] args) {int[] i=new int[]{5,4,8,3,9,5,23};int lm=i.length;for (int a=0;a<lm/2;a++) {int temp=i[i.length-a-1];i[i.length-a-1]=i[a];i[a]=temp;} Go (i);} public static void Go (int[] s) {for (int b=0;b<s.length;b++) {System.out.println (s[b]);}}}

The inverse of the array, so that the number of cycles is less than half the length of the array, and then the number of times corresponding to the index value of the array value is exchanged with the pair.

 

Package Lianxi;public class Shuzu {string[] xzname = {"Aries", "Taurus", "Gemini", "Cancer", "Lion", "Virgin", "Libra", "Scorpio", "Archer", "Capricorn", "Aquarius", " Pisces "}; String[] Xzdate = {"March 21-April 20", "April 21-May 20", "May 21-June 21", "June 22-July 22", "July 23-August 22", "August 23-September 22" "," September 23-October 22 "," October 23-November 21 "," November 22-December 21 "," December 22-January 19 "," January 20-February 18 "," February 19-March 20 "}; Package Lianxi;import Java.util.scanner;public class Testshuzu {public static void main (string[] args) {Shuzu sz=new Shuz U (); Scanner sc=new Scanner (system.in); System.out.println ("Please enter the constellation name and separate it with a comma");  String X_name=sc.nextline (); if (X_name.length () ==2) {for (int i=0;i<sz.xzname.length;i++) {if (X_name.equals (Sz.xzname[i])) {//   property. Equals is the value of the attribute System.out.print (Sz.xzdate[i]); }}}} else if (X_name.length () >2) {string[] Y=x_name.split (","); for (int w=0;w<y.length;w++) {String ow = null; for (int q=0;q<sz.xzname.length;q++)  {if (Y[w].equals (Sz.xzname[q])) {ow=sz.xzdate[q];} } System.out.println (OW); }} sc.close (); }}

Enter the constellation name, enter the corresponding month, you can enter multiple constellation query, the main idea is that string can be divided into several arrays with split method

Java array sorting (bubbling, vertical) inversion

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.