Skip to content

New Sorting algorithms #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
54 changes: 54 additions & 0 deletions src/java/main/BrickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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(" ");
}
}
85 changes: 85 additions & 0 deletions src/java/main/PancakeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import java.io.*;

class PancakeSort {

/* Reverses arr[0..i] */
static void flip(int arr[], int i)
{
int temp, start = 0;
while (start < i)
{
temp = arr[start];
arr[start] = arr[i];
arr[i] = temp;
start++;
i--;
}
}

// Returns index of the
// maximum element in
// arr[0..n-1]
static int findMax(int arr[], int n)
{
int mi, i;
for (mi = 0, i = 0; i < n; ++i)
if (arr[i] > arr[mi])
mi = i;
return mi;
}

// The main function that
// sorts given array using
// flip operations
static int pancakeSort(int arr[], int n)
{
// Start from the complete
// array and one by one
// reduce current size by one
for (int curr_size = n; curr_size > 1; --curr_size)
{
// Find index of the
// maximum element in
// arr[0..curr_size-1]
int mi = findMax(arr, curr_size);

// Move the maximum element
// to end of current array
// if it's not already at
// the end
if (mi != curr_size-1)
{
// To move at the end,
// first move maximum
// number to beginning
flip(arr, mi);

// Now move the maximum
// number to end by
// reversing current array
flip(arr, curr_size-1);
}
}
return 0;
}

/* Utility function to print array arr[] */
static void printArray(int arr[], int arr_size)
{
for (int i = 0; i < arr_size; i++)
System.out.print(arr[i] + " ");
System.out.println("");
}

/* Driver function to check for above functions*/
public static void main (String[] args)
{
int arr[] = {23, 10, 20, 11, 12, 6, 7};
int n = arr.length;

pancakeSort(arr, n);

System.out.println("Sorted Array: ");
printArray(arr, n);
}
}
64 changes: 64 additions & 0 deletions src/java/main/TreeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class GFG
{
class Node
{
int key;
Node left, right;

public Node(int item)
{
key = item;
left = right = null;
}
}
Node root;
GFG()
{
root = null;
}
void insert(int key)
{
root = insertRec(root, key);
}

/* A recursive function to
insert a new key in BST */
Node insertRec(Node root, int key)
{
if (root == null)
{
root = new Node(key);
return root;
}
if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);
return root;
}
void inorderRec(Node root)
{
if (root != null)
{
inorderRec(root.left);
System.out.print(root.key + " ");
inorderRec(root.right);
}
}
void treeins(int arr[])
{
for(int i = 0; i < arr.length; i++)
{
insert(arr[i]);
}

}
public static void main(String[] args)
{
GFG tree = new GFG();
int arr[] = {5, 4, 7, 2, 11};
tree.treeins(arr);
tree.inorderRec(tree.root);
}
}