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<, 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