

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- Insertion Sort
- C++ Program to Implement Insertion Sort
- Insertion Sort in C#
- Insertion Sort List C++
- Java Program for Cocktail Sort
Advertisements