Java data structure and algorithm of the odd and even sorting algorithm complete example _java

Source: Internet
Author: User

This paper illustrates the odd and even sorting algorithm of Java data structure and algorithm. Share to everyone for your reference, specific as follows:

Algorithm idea:

The basic idea is odd column row order, even row order, again odd row, again even row, until all orderly

For example,

arrays to be ranked [6 2 4 1 5 9]

For the first time, the odd sequence is compared with its neighbor pairs, such as 6 and 2, 4 and 1, and 5 and 9.

[6 2 4 1 5 9]

After the exchange becomes

[2 6 1 4 5 9]

Second comparison of even series, i.e. 6 and 1 ratio, 5 and 5 ratio

[2 6 1 4 5 9]

After the exchange becomes

[2 1 6 4 5 9]

The third is the odd sequence, and the 2,6,5 is chosen to compare them to their neighbor columns.

[2 1 6 4 5 9]

After Exchange

[1 2 4 6 5 9]

The four-trip couple series

[1 2 4 6 5 9]

One exchange

[1 2 4 5 6 9]

Specific code:

 public class Main {/** * @param args the command line arguments/public STA
    tic void Main (string[] args) {int[] numbers = new int[]{12,33,45,33,13,55,34,7,6};
    Main.oddeventsort (numbers);
    for (int i=0; i<numbers.length; i++) {System.out.print (numbers[i]+ "");
    } private static void Oddeventsort (int[] numbers) {int temp; for (int i=numbers.length-1; i>numbers.length/2-1; i--) {for (int j=1; j<=i; j+=2) {if (j==numbers.length
        -1) break;
          if (numbers[j]>numbers[j+1]) {temp = Numbers[j];
          NUMBERS[J] = numbers[j+1];
        NUMBERS[J+1] = temp;
        (int j=0; j<=i; j+=2) {if (j==numbers.length-1) break;
          if (numbers[j]>numbers[j+1]) {temp = Numbers[j];
          NUMBERS[J] = numbers[j+1];
        NUMBERS[J+1] = temp; }
      }
    }
  }
}

PS: It is said that this algorithm is more useful in the multi-core era

More about Java algorithms Interested readers can view the site topics: "Java data structure and algorithms tutorial", "Java Operation DOM node skills summary", "Java file and directory operation tips Summary" and "Java Cache operation Tips"

I hope this article will help you with Java programming.

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.