Skip to content

Commit

Permalink
Merge pull request #416 from salif-04/master
Browse files Browse the repository at this point in the history
Added OddEvenSort
  • Loading branch information
ambujraj authored Oct 5, 2018
2 parents 86eea7e + c424597 commit 0a2ddef
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions sorting/OddEvenSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
class OddEven
{
private long[] a;
private int nElems;
public OddEven(int max)
{
a = new long[max];
nElems = 0;
}
public void insert(long value)
{
a[nElems] = value;
nElems++;
}
public void display()
{
for(int j=0; j<nElems; j++)
System.out.print(a[j] + " ");
System.out.println();
}
public void oddEvenSort()
{
int i,j,k=0;
while(k<nElems/2)
{
for(i=0;i<nElems-1;i+=2)
if(a[i]>a[i+1])
swap(i,i+1);
for(j=1;j<nElems-1;j+=2)
if(a[j]>a[j+1])
swap(j,j+1);
k++;
}
}
private void swap(int one, int two)
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
}
class OddEvenSort
{
public static void main(String[] args)
{
int maxSize = 100;
OddEven arr;
arr = new OddEven(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
System.out.println("Before Sort : ");
arr.display();
arr.oddEvenSort();
System.out.println("After Sort : ");
arr.display();
}
}

0 comments on commit 0a2ddef

Please sign in to comment.