Xiao Lian said. Original: Java implements two-dimensional array bubble sort

Source: Internet
Author: User

Novice practitioners-to review their knowledge, but also want to share experience with you:

Words don't say much, on the code


Public class bubble Sort {

/**
* @param admin
* @2017.12.4
*/
public static void Main (string[] args) {
Int[][] Array=new int[10][3]; Create a new two-dimensional array

Assigning an array (in the case of a date)
array[0][0]=2015;array[0][1]=10;array[0][2]=23;
array[1][0]=2016;array[1][1]=8;array[1][2]=14;
array[2][0]=2017;array[2][1]=3;array[2][2]=7;
array[3][0]=2018;array[3][1]=12;array[3][2]=19;
array[4][0]=2017;array[4][1]=9;array[4][2]=25;
Array[5][0]=2017;array[5][1]=6;array[5][2]=1;
array[6][0]=2017;array[6][1]=4;array[6][2]=3;
array[7][0]=2017;array[7][1]=4;array[7][2]=7;
array[8][0]=2019;array[8][1]=10;array[8][2]=6;
array[9][0]=2017;array[9][1]=12;array[9][2]=15;

int I, J, Exchange; Declare the variables you need to use, where Exchange is the equivalent of a container for swapping

Rearrange the data using the bubbling sort principle (the newest top of the date)
for (i = array.length-1; I >=1; i--) {
for (j = i; J <= Array.length-1; j + +) {
if (Array[j-1][0] < array[j][0]) {
Exchange = Array[j][0]; Array[j][0] = array[j-1][0];   ARRAY[J-1][0] = Exchange; Replacement order
Exchange = Array[j][1]; ARRAY[J][1] = array[j-1][1];   ARRAY[J-1][1] = Exchange; Replacement order
Exchange = array[j][2]; ARRAY[J][2] = array[j-1][2];   ARRAY[J-1][2] = Exchange; Replacement order

}else{
if (array[j-1][0] = = Array[j][0] && array[j-1][1] < array[j][1]) {
Exchange = Array[j][0]; Array[j][0] = array[j-1][0];   ARRAY[J-1][0] = Exchange; Replacement order
Exchange = Array[j][1]; ARRAY[J][1] = array[j-1][1];   ARRAY[J-1][1] = Exchange; Replacement order
Exchange = array[j][2]; ARRAY[J][2] = array[j-1][2];   ARRAY[J-1][2] = Exchange; Replacement order
}else{
if (array[j-1][0] = = Array[j][0] && array[j-1][1] = array[j][1] && array[j-1][2] < array[j][2]) {
Exchange = Array[j][0]; Array[j][0] = array[j-1][0];   ARRAY[J-1][0] = Exchange; Replacement order
Exchange = Array[j][1]; ARRAY[J][1] = array[j-1][1];   ARRAY[J-1][1] = Exchange; Replacement order
Exchange = array[j][2]; ARRAY[J][2] = array[j-1][2];   ARRAY[J-1][2] = Exchange; Replacement order
}
}
}
}
}

Print it out to see the effect
for (i=0;i<array.length;i++) {
SYSTEM.OUT.PRINTLN (array[i][0]+ "year" +array[i][1]+ "month" +array[i][2]+ "Day");
}
}
}

Show the effect:

Xiao Lian said. Original: Java implements two-dimensional array bubble sort

Related Article

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.