Skip to content

Commit 1366d1c

Browse files
authored
Merge pull request #116 from diwakar1593/main
Create CocktailSort.java
2 parents 8e096b3 + 360f0af commit 1366d1c

File tree

4 files changed

+228
-102
lines changed

4 files changed

+228
-102
lines changed

CocktailSort.java

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Java program for implementation of Cocktail Sort
2+
public class CocktailSort
3+
{
4+
void cocktailSort(int a[])
5+
{
6+
boolean swapped = true;
7+
int start = 0;
8+
int end = a.length;
9+
10+
while (swapped == true)
11+
{
12+
// reset the swapped flag on entering the
13+
// loop, because it might be true from a
14+
// previous iteration.
15+
swapped = false;
16+
17+
// loop from bottom to top same as
18+
// the bubble sort.
19+
for (int i = start; i < end - 1; ++i)
20+
{
21+
if (a[i] > a[i + 1]) {
22+
int temp = a[i];
23+
a[i] = a[i + 1];
24+
a[i + 1] = temp;
25+
swapped = true;
26+
}
27+
}
28+
29+
// if nothing moved, then array is sorted.
30+
if (swapped == false)
31+
break;
32+
33+
// otherwise, reset the swapped flag so that it
34+
// can be used in the next stage
35+
swapped = false;
36+
37+
// move the end point back by one, because
38+
// item at the end is in its rightful spot
39+
end = end - 1;
40+
41+
// from top to bottom, doing the
42+
// same comparison as in the previous stage
43+
for (int i = end - 1; i >= start; i--)
44+
{
45+
if (a[i] > a[i + 1])
46+
{
47+
int temp = a[i];
48+
a[i] = a[i + 1];
49+
a[i + 1] = temp;
50+
swapped = true;
51+
}
52+
}
53+
54+
// increase the starting point, because
55+
// the last stage would have moved the next
56+
// smallest number to its rightful spot.
57+
start = start + 1;
58+
}
59+
}
60+
61+
/* Prints the array */
62+
void printArray(int a[])
63+
{
64+
int n = a.length;
65+
for (int i = 0; i < n; i++)
66+
System.out.print(a[i] + " ");
67+
System.out.println();
68+
}
69+
70+
// Driver code
71+
public static void main(String[] args)
72+
{
73+
CocktailSort ob = new CocktailSort();
74+
int a[] = { 5, 1, 4, 2, 8, 0, 2 };
75+
ob.cocktailSort(a);
76+
System.out.println("Sorted array");
77+
ob.printArray(a);
78+
}
79+
}

PigeonholeSort.java

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

bubblesort.java

+27-35
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
1-
public class BubbleSortExample {
2-
static void bubbleSort(int[] arr) {
3-
int n = arr.length;
4-
int temp = 0;
5-
for(int i=0; i < n; i++){
6-
for(int j=1; j < (n-i); j++){
7-
if(arr[j-1] > arr[j]){
8-
//swap elements
9-
temp = arr[j-1];
10-
arr[j-1] = arr[j];
11-
arr[j] = temp;
12-
}
13-
14-
}
15-
}
16-
17-
}
18-
public static void main(String[] args) {
19-
int arr[] ={3,60,35,2,45,320,5};
20-
21-
System.out.println("Array Before Bubble Sort");
22-
for(int i=0; i < arr.length; i++){
23-
System.out.print(arr[i] + " ");
24-
}
25-
System.out.println();
26-
27-
bubbleSort(arr);//sorting array elements using bubble sort
28-
29-
System.out.println("Array After Bubble Sort");
30-
for(int i=0; i < arr.length; i++){
31-
System.out.print(arr[i] + " ");
32-
}
33-
34-
}
35-
}
1+
import java.util.Scanner;
2+
public class BubbleSort {
3+
public static void main(String[] args){
4+
Scanner sc = new Scanner(System.in);
5+
System.out.println("Enter the number of elements");
6+
int n = sc.nextInt();
7+
8+
int[] arr = new int[n];
9+
System.out.println("Enter the elements");
10+
for(int i = 0 ; i<n;i++){
11+
arr[i]=sc.nextInt();
12+
}
13+
for(int i = 0 ; i<n;i++){
14+
for(int j=0;j<n-i-1;j++){
15+
if(arr[j]> arr[j+1]){
16+
int temp = arr[j];
17+
arr[j]= arr[j+1];
18+
arr[j+1]= temp;
19+
}
20+
}
21+
}
22+
System.out.println("sorted array is ");
23+
for(int i = 0;i<n;i++){
24+
System.out.println(arr[i]+" ");
25+
}
26+
}}
27+

heapsort.java

+68-67
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,72 @@
1-
// Java program for implementation of Heap Sort
1+
import java.util.*;
22
public class HeapSort {
3-
public void sort(int arr[])
4-
{
5-
int n = arr.length;
6-
7-
// Build heap (rearrange array)
8-
for (int i = n / 2 - 1; i >= 0; i--)
9-
heapify(arr, n, i);
10-
11-
// One by one extract an element from heap
12-
for (int i = n - 1; i >= 0; i--) {
13-
// Move current root to end
14-
int temp = arr[0];
15-
arr[0] = arr[i];
16-
arr[i] = temp;
17-
18-
// call max heapify on the reduced heap
19-
heapify(arr, i, 0);
3+
public void sort(int arr[])
4+
{
5+
int N = arr.length;
6+
7+
for (int i = N / 2 - 1; i >= 0; i--)
8+
heapify(arr, N, i);
9+
10+
for (int i = N - 1; i > 0; i--) {
11+
12+
int x = arr[0];
13+
arr[0] = arr[i];
14+
arr[i] = x;
15+
16+
heapify(arr, i, 0);
17+
}
18+
}
19+
20+
void heapify(int arr[], int N, int i)
21+
{
22+
int largest = i;
23+
int l = 2 * i + 1;
24+
int r = 2 * i + 2;
25+
26+
if (l < N && arr[l] > arr[largest])
27+
largest = l;
28+
29+
if (r < N && arr[r] > arr[largest])
30+
largest = r;
31+
32+
if (largest != i) {
33+
int swap = arr[i];
34+
arr[i] = arr[largest];
35+
arr[largest] = swap;
36+
37+
heapify(arr, N, largest);
38+
}
2039
}
21-
}
22-
23-
// To heapify a subtree rooted with node i which is
24-
// an index in arr[]. n is size of heap
25-
void heapify(int arr[], int n, int i)
26-
{
27-
int largest = i; // Initialize largest as root
28-
int l = 2 * i + 1; // left = 2*i + 1
29-
int r = 2 * i + 2; // right = 2*i + 2
30-
31-
// If left child is larger than root
32-
if (l < n && arr[l] > arr[largest])
33-
largest = l;
34-
35-
// If right child is larger than largest so far
36-
if (r < n && arr[r] > arr[largest])
37-
largest = r;
38-
39-
// If largest is not root
40-
if (largest != i) {
41-
int swap = arr[i];
42-
arr[i] = arr[largest];
43-
arr[largest] = swap;
44-
45-
// Recursively heapify the affected sub-tree
46-
heapify(arr, n, largest);
40+
static void printArray(int arr[])
41+
{
42+
int N = arr.length;
43+
44+
for (int i = 0; i < N; ++i)
45+
System.out.print(arr[i] + " ");
46+
System.out.println();
47+
}
48+
49+
public static void main(String args[])
50+
{
51+
int i, n, arr[];
52+
53+
Scanner s = new Scanner(System.in);
54+
System.out.println("Enter no. of elements in aray:");
55+
n = s.nextInt();
56+
57+
arr = new int[n];
58+
59+
System.out.println("Enter " + n + " integers:");
60+
61+
for (i = 0;i<n; i++)
62+
arr[i] = s.nextInt();
63+
64+
HeapSort ob = new HeapSort();
65+
ob.sort(arr);
66+
67+
System.out.println("Sorted array is");
68+
printArray(arr);
4769
}
48-
}
49-
50-
/* A utility function to print array of size n */
51-
static void printArray(int arr[])
52-
{
53-
int n = arr.length;
54-
for (int i = 0; i < n; ++i)
55-
System.out.print(arr[i] + " ");
56-
System.out.println();
57-
}
58-
59-
// Driver program
60-
public static void main(String args[])
61-
{
62-
int arr[] = { 12, 11, 13, 5, 6, 7 };
63-
int n = arr.length;
64-
65-
HeapSort ob = new HeapSort();
66-
ob.sort(arr);
67-
68-
System.out.println("Sorted array is");
69-
printArray(arr);
70-
}
70+
71+
7172
}

0 commit comments

Comments
 (0)