
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program for Binary Insertion Sort
Binary insertion sort uses the binary search to find the right position to insert an element at a specific index at every iteration. First, the location where the element needs to be inserted is found. Then, the elements are shifted to the next right location. Now, the specific element is placed in the position.
Following is the Java code for Binary Insertion Sort −
Example
public class Demo{ void Cocktail_Sort(int my_arr[]){ boolean swapped = true; int start = 0; int end = my_arr.length; while (swapped == true) { swapped = false; for (int i = start; i < end - 1; ++i) { if (my_arr[i] > my_arr[i + 1]) { int temp = my_arr[i]; my_arr[i] = my_arr[i + 1]; my_arr[i + 1] = temp; swapped = true; } } if (swapped == false) break; swapped = false; end = end - 1; for (int i = end - 1; i >= start; i--) { if (my_arr[i] > my_arr[i + 1]) { int temp = my_arr[i]; my_arr[i] = my_arr[i + 1]; my_arr[i + 1] = temp; swapped = true; } } start = start + 1; } } void print_values(int my_arr[]){ for (int i = 0; i < my_arr.length; i++) System.out.print(my_arr[i] + " "); System.out.println(); } public static void main(String[] args){ Demo my_object = new Demo(); int my_arr[] = { 6, 8, 34, 21, 0, 1, 98, 64, 6}; System.out.println("The array contains "); for (int i = 0; i < my_arr.length; i++) System.out.print(my_arr[i] + " "); System.out.println(); my_object.Cocktail_Sort(my_arr); System.out.println("The array after implementing cocktail sort is : "); my_object.print_values(my_arr); } }
Output
The array contains 6 8 34 21 0 1 98 64 6 The array after implementing cocktail sort is : 0 1 6 6 8 21 34 64 98
- Related Articles
- Python Program for Binary Insertion Sort
- Java Program for Recursive Insertion Sort
- Python Program for Insertion Sort
- Python Program for Recursive Insertion Sort
- C Program for Recursive Insertion Sort
- Binary Insertion Sort in C++
- Java program to implement insertion sort
- Insertion sort in Java.
- Insertion Sort in Python Program
- C++ Program Recursive Insertion Sort
- C++ Program to Implement Insertion Sort
- Insertion Sort
- Java Program for Radix Sort
- Java Program for Pigeonhole Sort
- Java Program for Stooge Sort

Advertisements