
- 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 Comb Sort
The Comb Sort in Java eliminates the smaller values situated to the end of the list and the inversions are removed oby by one. Let us see an example −
Example
import java.util.Arrays; public class Demo{ void comb_sort(int nums[]){ int len_gap = nums.length; float shrink_val = 1.3f; boolean swap = false; while (len_gap > 1 || swap) { if (len_gap > 1) { len_gap = (int)(len_gap / shrink_val); } swap = false; for (int i = 0; len_gap + i < nums.length; i++){ if (nums[i] > nums[i + len_gap]) { swap(nums, i, i + len_gap); swap = true; } } } } private static void swap(int nums[], int x, int y) { Integer temp = nums[x]; nums[x] = nums[y]; nums[y] = temp; } public static void main(String args[]){ Demo ob = new Demo(); int nums[] = {6, 78, 90, -12, -45, 0, -1, 45}; System.out.println("The original array contains "); System.out.println(Arrays.toString(nums)); ob.comb_sort(nums); System.out.println("The sorted array is "); System.out.println(Arrays.toString(nums)); } }
Output
The original array contains [6, 78, 90, -12, -45, 0, -1, 45] The sorted array is [-45, -12, -1, 0, 6, 45, 78, 90]
A class named Demo contains the ‘comb_sort’ function. Here, the length of the array is defined and if this length is greater than 1, a new ‘len_gap’ is defined that is the length of array divided by 1.3f.
This array is iterated over and the elements in the array are compared and if the element is greater than the element plus a specific ‘len_gap’, the elements are swapped. After this, a simple bubble sort is also performed on the elements. In the main function, the array is defined and an instance of Demo class is defined and the ‘comb_sort’ function is called on the array.
- Related Articles
- C++ Program for Comb Sort?
- C++ Program for the Comb Sort?
- Comb Sort
- Java Program for Radix Sort
- Java Program for Pigeonhole Sort
- Java Program for Cocktail Sort
- Java Program for Counting Sort
- Java Program for Bitonic Sort
- Java Program for Gnome Sort
- Java Program for Stooge Sort
- Java Program for Binary Insertion Sort
- Java Program for Recursive Bubble Sort
- Java Program for Recursive Insertion Sort
- Java Program for Iterative Merge Sort
- Java Program for Iterative Quick Sort

Advertisements