Skip to content

Commit 3cd0315

Browse files
authored
Merge pull request #110 from rohits301/rohit
Added some sorting algorithms in Java
2 parents bea52ec + da5fb6c commit 3cd0315

File tree

4 files changed

+298
-0
lines changed

4 files changed

+298
-0
lines changed

Java/BitonicSort.java

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* this program
2+
works only when size of input is a power of 2 */
3+
public class BitonicSort
4+
{
5+
/* The parameter dir indicates the sorting direction,
6+
ASCENDING or DESCENDING; if (a[i] > a[j]) agrees
7+
with the direction, then a[i] and a[j] are
8+
interchanged. */
9+
void compAndSwap(int a[], int i, int j, int dir)
10+
{
11+
if ( (a[i] > a[j] && dir == 1) ||
12+
(a[i] < a[j] && dir == 0))
13+
{
14+
// Swapping elements
15+
int temp = a[i];
16+
a[i] = a[j];
17+
a[j] = temp;
18+
}
19+
}
20+
21+
/* It recursively sorts a bitonic sequence in ascending
22+
order, if dir = 1, and in descending order otherwise
23+
(means dir=0). The sequence to be sorted starts at
24+
index position low, the parameter cnt is the number
25+
of elements to be sorted.*/
26+
void bitonicMerge(int a[], int low, int cnt, int dir)
27+
{
28+
if (cnt>1)
29+
{
30+
int k = cnt/2;
31+
for (int i=low; i<low+k; i++)
32+
compAndSwap(a,i, i+k, dir);
33+
bitonicMerge(a,low, k, dir);
34+
bitonicMerge(a,low+k, k, dir);
35+
}
36+
}
37+
38+
/* This funcion first produces a bitonic sequence by
39+
recursively sorting its two halves in opposite sorting
40+
orders, and then calls bitonicMerge to make them in
41+
the same order */
42+
void bitonicSort(int a[], int low, int cnt, int dir)
43+
{
44+
if (cnt>1)
45+
{
46+
int k = cnt/2;
47+
48+
// sort in ascending order since dir here is 1
49+
bitonicSort(a, low, k, 1);
50+
51+
// sort in descending order since dir here is 0
52+
bitonicSort(a,low+k, k, 0);
53+
54+
// Will merge wole sequence in ascending order
55+
// since dir=1.
56+
bitonicMerge(a, low, cnt, dir);
57+
}
58+
}
59+
60+
/*Caller of bitonicSort for sorting the entire array
61+
of length N in ASCENDING order */
62+
void sort(int a[], int N, int up)
63+
{
64+
bitonicSort(a, 0, N, up);
65+
}
66+
67+
/* A utility function to print array of size n */
68+
static void printArray(int arr[])
69+
{
70+
int n = arr.length;
71+
for (int i=0; i<n; ++i)
72+
System.out.print(arr[i] + " ");
73+
System.out.println();
74+
}
75+
76+
// Driver function
77+
public static void main(String args[])
78+
{
79+
int a[] = {3, 7, 4, 8, 6, 2, 1, 5};
80+
int up = 1;
81+
BitonicSort ob = new BitonicSort();
82+
ob.sort(a, a.length,up);
83+
System.out.println("\nSorted array");
84+
printArray(a);
85+
}
86+
}

Java/CycleSort.java

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import java.util.*;
2+
import java.lang.*;
3+
4+
class CycleSort {
5+
// Function sort the array using Cycle sort
6+
public static void cycleSort(int arr[], int n)
7+
{
8+
// count number of memory writes
9+
int writes = 0;
10+
11+
// traverse array elements and put it to on
12+
// the right place
13+
for (int cycle_start = 0; cycle_start <= n - 2; cycle_start++) {
14+
// initialize item as starting point
15+
int item = arr[cycle_start];
16+
17+
// Find position where we put the item. We basically
18+
// count all smaller elements on right side of item.
19+
int pos = cycle_start;
20+
for (int i = cycle_start + 1; i < n; i++)
21+
if (arr[i] < item)
22+
pos++;
23+
24+
// If item is already in correct position
25+
if (pos == cycle_start)
26+
continue;
27+
28+
// ignore all duplicate elements
29+
while (item == arr[pos])
30+
pos += 1;
31+
32+
// put the item to it's right position
33+
if (pos != cycle_start) {
34+
int temp = item;
35+
item = arr[pos];
36+
arr[pos] = temp;
37+
writes++;
38+
}
39+
40+
// Rotate rest of the cycle
41+
while (pos != cycle_start) {
42+
pos = cycle_start;
43+
44+
// Find position where we put the element
45+
for (int i = cycle_start + 1; i < n; i++)
46+
if (arr[i] < item)
47+
pos += 1;
48+
49+
// ignore all duplicate elements
50+
while (item == arr[pos])
51+
pos += 1;
52+
53+
// put the item to it's right position
54+
if (item != arr[pos]) {
55+
int temp = item;
56+
item = arr[pos];
57+
arr[pos] = temp;
58+
writes++;
59+
}
60+
}
61+
}
62+
}
63+
64+
// Driver program to test above function
65+
public static void main(String[] args)
66+
{
67+
int arr[] = { 1, 8, 3, 9, 10, 10, 2, 4 };
68+
int n = arr.length;
69+
cycleSort(arr, n);
70+
71+
System.out.println("After sort : ");
72+
for (int i = 0; i < n; i++)
73+
System.out.print(arr[i] + " ");
74+
}
75+
}

Java/PigeonholeSort.java

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.lang.*;
2+
import java.util.*;
3+
4+
public class PigeonholeSort
5+
{
6+
public static void pigeonhole_sort(int arr[],
7+
int n)
8+
{
9+
int min = arr[0];
10+
int max = arr[0];
11+
int range, i, j, index;
12+
13+
for(int a=0; a<n; a++)
14+
{
15+
if(arr[a] > max)
16+
max = arr[a];
17+
if(arr[a] < min)
18+
min = arr[a];
19+
}
20+
21+
range = max - min + 1;
22+
int[] phole = new int[range];
23+
Arrays.fill(phole, 0);
24+
25+
for(i = 0; i<n; i++)
26+
phole[arr[i] - min]++;
27+
28+
29+
index = 0;
30+
31+
for(j = 0; j<range; j++)
32+
while(phole[j]-->0)
33+
arr[index++]=j+min;
34+
35+
}
36+
37+
public static void main(String[] args)
38+
{
39+
PigeonholeSort sort = new PigeonholeSort();
40+
int[] arr = {8, 3, 2, 7, 4, 6, 8};
41+
42+
System.out.print("Sorted order is : ");
43+
44+
sort.pigeonhole_sort(arr,arr.length);
45+
46+
for(int i=0 ; i<arr.length ; i++)
47+
System.out.print(arr[i] + " ");
48+
}
49+
50+
}

Java/TreeSort.java

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
2+
class TreeSort
3+
{
4+
// Class containing left and
5+
// right child of current
6+
// node and key value
7+
class Node
8+
{
9+
int key;
10+
Node left, right;
11+
12+
public Node(int item)
13+
{
14+
key = item;
15+
left = right = null;
16+
}
17+
}
18+
19+
// Root of BST
20+
Node root;
21+
22+
// Constructor
23+
TreeSort()
24+
{
25+
root = null;
26+
}
27+
28+
// This method mainly
29+
// calls insertRec()
30+
void insert(int key)
31+
{
32+
root = insertRec(root, key);
33+
}
34+
35+
/* A recursive function to
36+
insert a new key in BST */
37+
Node insertRec(Node root, int key)
38+
{
39+
40+
/* If the tree is empty,
41+
return a new node */
42+
if (root == null)
43+
{
44+
root = new Node(key);
45+
return root;
46+
}
47+
48+
/* Otherwise, recur
49+
down the tree */
50+
if (key < root.key)
51+
root.left = insertRec(root.left, key);
52+
else if (key > root.key)
53+
root.right = insertRec(root.right, key);
54+
55+
/* return the root */
56+
return root;
57+
}
58+
59+
// A function to do
60+
// inorder traversal of BST
61+
void inorderRec(Node root)
62+
{
63+
if (root != null)
64+
{
65+
inorderRec(root.left);
66+
System.out.print(root.key + " ");
67+
inorderRec(root.right);
68+
}
69+
}
70+
void treeins(int arr[])
71+
{
72+
for(int i = 0; i < arr.length; i++)
73+
{
74+
insert(arr[i]);
75+
}
76+
77+
}
78+
79+
// Driver Code
80+
public static void main(String[] args)
81+
{
82+
TreeSort tree = new TreeSort();
83+
int arr[] = {5, 4, 7, 2, 11};
84+
tree.treeins(arr);
85+
tree.inorderRec(tree.root);
86+
}
87+
}

0 commit comments

Comments
 (0)