Java implementation Parity ordering

Source: Internet
Author: User
Tags comparable

Odd-even ordering:

The first scan selects all the odd data pairs, compared to the adjacent even, A[j] and a[j+1],j are odd (j=1,3,5 ...), j<n-1

The second round of scanning selects all pairs of even data items, compared to adjacent odd numbers, A[j] and a[j+1],j are even (j=0,2,4 ...), j<n-1

The time complexity of odd-even ordering is O (n^2)

/** * odd and even sort * @author stone * */public class Oddevensort {public static void main (string[] args) {int len = 100;integer[] ary = new Integer[len]; Random random = new random (), for (int j = 0; J < Len; J + +) {Ary[j] = random.nextint (1000);} /* * The minimum number of exchanges is also 1 times, the largest is (n^2-n)/2 times *///ary=new integer[]{10,9,8,7,6,5,4,3,2,1}; Number of test exchanges//ary=new integer[]{1,2,3,4,5,6,7,8,10,9}; Test interchange Count System.out.println ("-------------before sorting");p rintary (ary); Long start = System.currenttimemillis (); ORDERASC (ary ); Long end = System.currenttimemillis (); System.out.println ("Sorting Time:" + (End-start) + "millisecond");p rintary (ary); SYSTEM.OUT.PRINTLN ("-----ORDERASC in ascending order------comparison:" + Comparecount + ", Number of Exchanges:" + Changecount);} static int comparecount = 0;//comparison static int changecount = 0;//Interchange/** * First-round scan selects all odd data item pairs, compared to adjacent even, A[j] and a[j+1],j are odd (j= 1,3,5 ...), j<n-1 * Second round scan selects all pairs of even data items, compared to adjacent odd numbers, A[j] and a[j+1],j are even (j=0,2,4 ...), j<n-1 * @param ary */public static <t E Xtends comparable<? Super t>> void Orderasc (t[] ary) {int len = Ary.length; Boolean Unsort = True, Oddsort = False, Evensort = False;while (unsort) {int J = 0;evensort = Scan (ary, J, len); j = 1;odd sort = Scan (ary, J, len); unsort = Oddsort | | evensort;//, if False, indicates that a qualifying comparison does not}}public static <t extends Comparable&lt, regardless of the parity sequence. Super T>> Boolean scan (t[] ary, int j, int len) {Boolean unsort = False;for (; j < len-1; J + = 2) {if (ary[j].c Ompareto (Ary[j + 1]) > 0) {t t = ary[j];ary[j] = ary[j + 1];ary[j + 1] = T;changecount++;unsort = true;} comparecount++;} return unsort;}  private static void Printary (object[] ary) {int len = ary.length;for (int j = 0; J < Len; J + +) {System.out.print (Ary[j] + " ");} System.out.println ("");}}


Java implementation Parity ordering

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.