Skip to content

Commit

Permalink
Merge pull request ZoranPandovski#1069 from pandyamarut/patch-5
Browse files Browse the repository at this point in the history
Create odd_even_sort.java
  • Loading branch information
ZoranPandovski authored Oct 2, 2018
2 parents 98215ef + 84846aa commit e36112c
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions sort/odd_even_openmp/odd_even_sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

// Java Program to implement
// Odd-Even / Brick Sort
import java.io.*;

class GFG
{
public static void oddEvenSort(int arr[], int n)
{
boolean isSorted = false; // Initially array is unsorted

while (!isSorted)
{
isSorted = true;
int temp =0;

// Perform Bubble sort on odd indexed element
for (int i=1; i<=n-2; i=i+2)
{
if (arr[i] > arr[i+1])
{
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
isSorted = false;
}
}

// Perform Bubble sort on even indexed element
for (int i=0; i<=n-2; i=i+2)
{
if (arr[i] > arr[i+1])
{
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
isSorted = false;
}
}
}

return;
}
public static void main (String[] args)
{
int arr[] = {34, 2, 10, -9};
int n = arr.length;

oddEvenSort(arr, n);
for (int i=0; i < n; i++)
System.out.print(arr[i] + " ");

System.out.println(" ");
}
}

0 comments on commit e36112c

Please sign in to comment.