Java Sorting Methods

  1. Sort an Array in Java
  2. Sort an ArrayList in Java
  3. Bubble Sort in Java
  4. Selection Sort in Java
  5. Insertion Sort in Java
  6. Shell Sort in Java
  7. Merge Sort in Java
  8. Heap Sort in Java
  9. Quick Sort in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Sort an Array in Java


Sort_Array.java

import java.util.Arrays;
import java.util.Scanner;
 
public class Sort_Array {
 
	Scanner scan;
	int[] num;
	int n;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Sort an Array");
		
		System.out.println("\nEnter 'n' value :");
		n = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the numbers :");
		num = new int[n];
		
		for(int i=0; i<n; i++) {
			
			num[i] = Integer.parseInt(scan.nextLine());
		}
	}
	
	void sort() {
		
		System.out.println("\nBefore Sorting");
		
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + num[i]);
		}
		
		Arrays.sort(num);
		
		System.out.println("\n\nAfter Sorting");
		
		System.out.println("\nAscending Order");
		
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + num[i]);
		}
		
		System.out.println("\nDescending Order");
 
		for(int i=n-1; i>=0; i--) {
			
			System.out.print(" " + num[i]);
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]){
		
		Sort_Array obj = new Sort_Array();
		
		obj.getVal();
		obj.sort();
	}
}

Sample Output

Sort an Array

Enter 'n' value :
5
Enter the numbers :
3
6
5
2
4

Before Sorting
 3 6 5 2 4

After Sorting

Ascending Order
 2 3 4 5 6
Descending Order
 6 5 4 3 2




B. Sort an ArrayList in Java


Sort_ArrayList.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
 
public class Sort_ArrayList {
 
	Scanner scan;
	List<Integer> list;
	int n;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		list = new ArrayList<Integer>();
		
		System.out.println("Sort an ArrayList");
		
		System.out.println("\nEnter 'n' value :");
		n = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the numbers :");
		
		for(int i=0; i<n; i++) {
			
			list.add(Integer.parseInt(scan.nextLine()));
		}
	}
	
	void sort() {
		
		System.out.println("\nBefore Sorting");
		
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + list.get(i));
		}
		
		Collections.sort(list);
		
		System.out.println("\n\nAfter Sorting");
		
		System.out.println("\nAscending Order");
		
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + list.get(i));
		}
		
		System.out.println("\nDescending Order");
 
		for(int i=n-1; i>=0; i--) {
			
			System.out.print(" " + list.get(i));
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]){
		
		Sort_ArrayList obj = new Sort_ArrayList();
		
		obj.getVal();
		obj.sort();
	}
}

Sample Output

Sort an ArrayList

Enter 'n' value :
5
Enter the numbers :
6
3
5
2
4

Before Sorting
 6 3 5 2 4

After Sorting

Ascending Order
 2 3 4 5 6
Descending Order
 6 5 4 3 2




C. Bubble Sort in Java


Sort_Bubble.java

import java.util.Scanner;
 
public class Sort_Bubble {
	
	int numbers[] = null;
	int n = 0;
	
	Scanner scan = new Scanner(System.in);
	
	void getNumbers() {
		
		System.out.println("Bubble Sort");
		System.out.println("\nEnter n value :");
		n = Integer.parseInt(scan.nextLine());
		
		numbers = new int[n];
		
		System.out.println("Enter the Numbers :");
		
		for(int i=0; i<n; i++) {
			
			numbers[i] = Integer.parseInt(scan.nextLine());
		}
	}
	
	void BubbleSort() {
		
		System.out.println("\nBefore Sorting :");
		
		for(int i=0; i<n; i++) {
			
			System.out.print(numbers[i] + " ");
		}
		
		/* Bubble Sort Code Start */
		
		for(int i = 0; i < n; i++) {
			
			for(int j = 1; j < (n-i); j++) {
				
				if(numbers[j-1] > numbers[j]) {
					
					int temp = numbers[j-1];
					numbers[j-1] = numbers[j];
					numbers[j] = temp;
				}
			}
		}
		
		/* Bubble Sort Code End */
		
		System.out.println("\n \nAfter Sorting");
		System.out.println("\nAscending Order :");
 
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + numbers[i]);
		}
		
		System.out.println("\nDescending Order :");
 
		for(int i=n-1; i>=0; i--) {
			
			System.out.print(" " + numbers[i]);
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Sort_Bubble obj = new Sort_Bubble();
		
		obj.getNumbers();
		obj.BubbleSort();
	}
}

Sample Output

Bubble Sort

Enter n value :
5
Enter the Numbers :
6
3
5
2
4

Before Sorting :
6 3 5 2 4 
 
After Sorting

Ascending Order :
 2 3 4 5 6
Descending Order :
 6 5 4 3 2




D. Selection Sort in Java


Sort_Selection.java

import java.util.Scanner;
 
public class Sort_Selection {
	
	int numbers[] = null;
	int n = 0;
	
	Scanner scan = new Scanner(System.in);
	
	void getNumbers() {
		
		System.out.println("Selection Sort");
		System.out.println("\nEnter n value :");
		n = Integer.parseInt(scan.nextLine());
		
		numbers = new int[n];
		
		System.out.println("Enter the Numbers :");
		
		for(int i=0; i<n; i++) {
			
			numbers[i] = Integer.parseInt(scan.nextLine());
		}
	}
	
	void SelectionSort() {
		
		System.out.println("\nBefore Sorting :");
		
		for(int i=0; i<n; i++) {
			
			System.out.print(numbers[i] + " ");
		}
		
		/* Selection Sort Code Start */
 
		for(int i=0; i<n; i++) {
			
			int index_of_min = i;
			
			for(int j=i; j<n; j++) {
				
				if(numbers[index_of_min] > numbers[j]) {
					
					index_of_min = j;
				}
			}
			
			int temp = numbers[i];
			numbers[i] = numbers[index_of_min];
			numbers[index_of_min] = temp;
		}
		
		/* Selection Sort Code End */
 
		System.out.println("\n \nAfter Sorting");
		System.out.println("\nAscending Order :");
 
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + numbers[i]);
		}
		
		System.out.println("\nDescending Order :");
 
		for(int i=n-1; i>=0; i--) {
			
			System.out.print(" " + numbers[i]);
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Sort_Selection obj = new Sort_Selection();
		
		obj.getNumbers();
		obj.SelectionSort();
	}
}

Sample Output

Selection Sort

Enter n value :
5
Enter the Numbers :
6
3
5
2
4

Before Sorting :
6 3 5 2 4 
 
After Sorting

Ascending Order :
 2 3 4 5 6
Descending Order :
 6 5 4 3 2




E. Insertion Sort in Java


Sort_Insertion.java

import java.util.Scanner;
 
public class Sort_Insertion {
	
	int numbers[] = null;
	int n = 0;
	
	Scanner scan = new Scanner(System.in);
	
	void getNumbers() {
		
		System.out.println("Insertion Sort");
		System.out.println("\nEnter n value :");
		n = Integer.parseInt(scan.nextLine());
		
		numbers = new int[n];
		
		System.out.println("Enter the Numbers :");
		
		for(int i=0; i<n; i++) {
			
			numbers[i] = Integer.parseInt(scan.nextLine());
		}
	}
	
	void InsertionSort() {
		
		System.out.println("\nBefore Sorting :");
		
		for(int i=0; i<n; i++) {
			
			System.out.print(numbers[i] + " ");
		}
		
		/* Insertion Sort Code Start */
 
		for (int i = 1; i < n; i++) {
			
			int j = i;
			int B = numbers[i];
			
			while ((j > 0) && (numbers[j-1] > B)) {
				
				numbers[j] = numbers[j-1];
				j--;
			}
			
			numbers[j] = B;
		}
		
		/* Insertion Sort Code End */
 
		System.out.println("\n \nAfter Sorting");
		System.out.println("\nAscending Order :");
 
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + numbers[i]);
		}
		
		System.out.println("\nDescending Order :");
 
		for(int i=n-1; i>=0; i--) {
			
			System.out.print(" " + numbers[i]);
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Sort_Insertion obj = new Sort_Insertion();
		
		obj.getNumbers();
		obj.InsertionSort();
	}
}

Sample Output

Insertion Sort

Enter n value :
5
Enter the Numbers :
6
3
5
2
4

Before Sorting :
6 3 5 2 4 
 
After Sorting

Ascending Order :
 2 3 4 5 6
Descending Order :
 6 5 4 3 2




F. Shell Sort in Java


Sort_Shell.java

import java.util.Scanner;
 
public class Sort_Shell {
	
	int numbers[] = null;
	int n = 0;
	
	Scanner scan = new Scanner(System.in);
	
	void getNumbers() {
		
		System.out.println("Shell Sort");
		System.out.println("\nEnter n value :");
		n = Integer.parseInt(scan.nextLine());
		
		numbers = new int[n];
		
		System.out.println("Enter the Numbers :");
		
		for(int i=0; i<n; i++) {
			
			numbers[i] = Integer.parseInt(scan.nextLine());
		}
	}
	
	void ShellSort() {
		
		System.out.println("\nBefore Sorting :");
		
		for(int i=0; i<n; i++) {
			
			System.out.print(numbers[i] + " ");
		}
		
		/* Shell Sort Code Start */
 
		int inner, outer, temp;
		int h = 1;
		
		while (h > 0) {
		
			for (outer = h; outer < n; outer++) {
				
				temp = numbers[outer];
				inner = outer;
 
				while (inner > h - 1 && numbers[inner - h] >= temp) {
					
					numbers[inner] = numbers[inner - h];
					inner -= h;
				}
				
				numbers[inner] = temp;
			}
			
			h = (h - 1) / 3; // decrease h
		}
				
		/* Shell Sort Code End */
 
		System.out.println("\n \nAfter Sorting");
		System.out.println("\nAscending Order :");
 
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + numbers[i]);
		}
		
		System.out.println("\nDescending Order :");
 
		for(int i=n-1; i>=0; i--) {
			
			System.out.print(" " + numbers[i]);
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Sort_Shell obj = new Sort_Shell();
		
		obj.getNumbers();
		obj.ShellSort();
	}
}

Sample Output

Shell Sort

Enter n value :
5
Enter the Numbers :
6
3
5
2
4

Before Sorting :
6 3 5 2 4 
 
After Sorting

Ascending Order :
 2 3 4 5 6
Descending Order :
 6 5 4 3 2




G. Merge Sort in Java


Sort_Merge.java

import java.util.Scanner;
 
public class Sort_Merge {
	
	public static void main(String args[]) {
		
		int n;
		int[] num;
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Merge Sort");
		
		System.out.println("\nEnter 'n' value :");
		n = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the numbers :");
		num = new int[n];
		
		for(int i=0; i<n; i++) {
			
			num[i] = Integer.parseInt(scan.nextLine());
		}
		
		System.out.println("\nBefore Sorting");
		
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + num[i]);
		}
		
		// Merge Sort Event..
		MergeSort(num, 0, n-1);
		
		System.out.println("\n\nAfter Sorting");
		
		System.out.println("\nAscending Order");
 
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + num[i]);
		}
		
		System.out.println("\nDescending Order");
 
		for(int i=n-1; i>=0; i--) {
			
			System.out.print(" " + num[i]);
		}
	}
 
	private static void MergeSort(int[] num, int i, int j) {
		
		int low = i;
		int high = j;
		
		if (low >= high) {
			
			return;
		}
 
		int middle = (low + high) / 2;
		
		MergeSort(num, low, middle);
		MergeSort(num, middle + 1, high);
		
		int end_low = middle;
		int start_high = middle + 1;
		
		while ((low <= end_low) && (start_high <= high)) {
			
			if (num[low] < num[start_high]) {
				
				low++;
			} 
			else {
				
				int Temp = num[start_high];
				
				for (int k = start_high- 1; k >= low; k--) {
					
					num[k+1] = num[k];
				}
				
				num[low] = Temp;
				low ++;
				end_low ++;
				start_high ++;
			}
		}
	}
}

Sample Output

Merge Sort

Enter 'n' value :
5
Enter the numbers :
6
3
5
2
4

Before Sorting
 6 3 5 2 4

After Sorting

Ascending Order
 2 3 4 5 6
Descending Order
 6 5 4 3 2




H. Heap Sort in Java


Sort_Heap.java

import java.util.Scanner;
 
public class Sort_Heap {
	
	public static void main(String args[]) {
	
		int n;
		int num[];
		Scanner scan = new Scanner(System.in);		
 
		System.out.println("Heap Sort");
		
		System.out.println("\nEnter 'n' value :");
		n = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the numbers :");
		
		num = new int[n];
 
		for(int i=0; i<n; i++) {
			num[i] = Integer.parseInt(scan.nextLine());
		}
		
		System.out.println("\nBefore Sorting");
		
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + num[i]);
		}
		
		System.out.println("\n\nAfter Sorting");
 
		for(int i=n; i>1; i--) {
			
			HeapSort(num, i - 1);
		}
		
		System.out.println("\nAscending Order");
 
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + num[i]);
		}
		
		System.out.println("\nDescending Order");
 
		for(int i=n-1; i>=0; i--) {
			
			System.out.print(" " + num[i]);
		}
	}
 
	private static void HeapSort(int array[], int bound) {
		
		int left, right, middle, root, temp;
		root = (bound-1) / 2;
		
		for(int i=root; i>=0; i--) {
			
			for(int j=root; j>=0; j--) {
				
				left = (2*j) + 1;
				right = (2*j) + 2;
				
				if((left <= bound) && (right <= bound)) {
					
					if(array[right] >= array[left])
						middle = right;
					else
						middle = left;
				}
				else {
					
					if(right > bound)
						middle = left;
					else
						middle = right;
				}
 
				if(array[j] < array[middle]) {
					
					temp = array[j];
					array[j] = array[middle];
					array[middle] = temp;
				}
			}
		}
		
		temp = array[0];
		array[0] = array[bound];
		array[bound] = temp;
	}
}

Sample Output

Heap Sort

Enter 'n' value :
5
Enter the numbers :
6
3
5
2
4

Before Sorting
 6 3 5 2 4

After Sorting

Ascending Order
 2 3 4 5 6
Descending Order
 6 5 4 3 2




I. Quick Sort in Java


Sort_Quick.java

import java.util.Scanner;
 
public class Sort_Quick {
	
	public static void main(String args[]) {
	
		int n;
		int num[];
		Scanner scan = new Scanner(System.in);		
 
		System.out.println("Quick Sort");
		
		System.out.println("\nEnter 'n' value :");
		n = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the numbers :");
		num = new int[n];
		
		for(int i=0; i<n; i++) {
			
			num[i] = Integer.parseInt(scan.nextLine());
		}
		
		System.out.println("\nBefore Sorting");
		
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + num[i]);
		}
		
		// Quick Sort Event..
		QuickSort(num, 0, n-1);
		
		System.out.println("\n\nAfter Sorting");
		
		System.out.println("\nAscending Order");
 
		for(int i=0; i<n; i++) {
			
			System.out.print(" " + num[i]);
		}
		
		System.out.println("\nDescending Order");
 
		for(int i=n-1; i>=0; i--) {
			
			System.out.print(" " + num[i]);
		}
	}
 
	private static void QuickSort(int[] num, int i, int j) {
		
		int low = i;
		int high = j;
		
		if (low >= j) {
			
			return;
		}
		
		int mid = num[(low + high) / 2];
		
		while (low < high) {
			
			while (low<high && num[low] < mid) {
				
				low++;
			}
			
			while (low<high && num[high] > mid) {
				
				high--;
			}
			
			if (low < high) {
				
				int T = num[low];
				num[low] = num[high];
				num[high] = T;
			}
		}
		
		if (high < low) {
			
			int T = high;
			high = low;
			low = T;
		}
		
		QuickSort(num, i, low);
		QuickSort(num, low == i ? low+1 : low, j);
	}
}

Sample Output

Quick Sort

Enter 'n' value :
5
Enter the numbers :
6
3
5
2
4

Before Sorting
 6 3 5 2 4

After Sorting

Ascending Order
 2 3 4 5 6
Descending Order
 6 5 4 3 2






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com