Java Novice Note 7

Source: Internet
Author: User

1. Minimum number

1.0public class SortDemo1 {public     static void Main (string[] args) {   int[] a = {2,5,4,6,8,1,3,9};       int min = a[0];//the minimum number   int k = 0;   int temp;   for (int i = 0; i < a.length; i++) {             System.out.print (a[i] + "\ t");  }   The minimum number for       (int j = 0; J < A.length; J + +) {             if (min > A[j]) {                 k = j; min = a[j];}   }   temp = a[0];   A[0] = a[k];   A[K] = temp;         System.out.println ("\ n min:" + min);  System.out.println ("index:" + K);  System.out.println ("a[5]:" + a[k]);  System.out.println ("a[0]:" + a[0]);  for (int i = 0; i < a.length; i++) {             System.out.print (a[i] + "\ t");}}}        

2. Minimum number of arrays

1.1public class SortDemo2 {public static void main (string[] args) {int[] a = {0,2,5,2,5,0};   int min = a[0];//the minimum number int k = 0;   int temp;   System.out.print ("Array data:");  for (int i = 0; i < a.length; i++) {System.out.print (a[i] + "\ t");  }//Minimum number for (int i = 0; i < a.length-1; i++) {k = i;//default current minimum value//minimum value from remaining element for (int j = i; J < A.length;                          J + +) {//The starting value of the inner layer if (A[k] > A[j]) {//a[k] value is the default minimum value k = j;   }}//data exchange if the current value is minimal without swapping if (k! = i) {temp = a[i];//0  0 K = = I a[i] = a[k];   System.out.print (". temp =" + temp + ": a[k] =" + A[k] + ": a[i] =" + a[i]);             A[K] = temp;       }/* System.out.print ("\ n");  for (int x = 0, x < A.length, + x + +) {System.out.print (a[x] + "\ t");   } */} System.out.print ("\ n");  System.out.print ("Array data:"); for (int i = 0; i < A.length;  i++) {System.out.print (a[i] + "\ t"); }        }}

3. Sorting, large number of rear

1.1public class SortDemo3 {public     static void Main (string[] args) {   int[] a = {7,5,6,3,2,1,4,9,8,6};         int temp;   System.out.print ("Array data:");   for (int i = 0; i < a.length; i++) {             System.out.print (a[i] + "\ t");                                                     }   The adjacent elements compare  8 for       (int i = 0; i < a.length-1; i++) {   //The maximum number of each search is placed at the last for       (int j = 0; J < a.length-1 I J + +) {//0-3                if (A[j] > A[j+1]) {//Interchange temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}                                                }                        System.out.print ("\ n");   System.out.print ("Array data:");  for (int i = 0; i < a.length; i++) {                      System.out.print (a[i] + "\ t");}}}        

Java Novice Note 7

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.