I am here telling you about Java programming.I am telling here about sorting programming as given below:
Program 1: Bubble Sort
Use your package;
Import Scanner
class BubbleSort {
public static void main(String []args) {
int n, i, j, temp;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to Bubble sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter array " + n + " integers");
for (i = 0; i < n; i++)
array[i] = in.nextInt();// for array input by user.
for (i = 0; i < ( n - 1 ); i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
System.out.println("Sorted list of numbers");
for (i = 0; i < n; i++)
System.out.println(array[i]);
}
}
Program 1: Insertion Sort
Use your package;
Import Scanner
Program 3: Selection Sort
Use your package;
Import Scanner
Program 1: Insertion Sort
Use your package;
Import Scanner
public class MyInsertionSort {
public static void main(String[] args) {
int[] arr = {9,8,18,5,96,45,2,74 };
insertionSort(arr);
}
private static void printNumbers(int[] arr) {
for (int i = 0; i < input.length; i++) {
System.out.print(arr[i] + ", ");
}
System.out.println("\n");
}
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int key = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > key ) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = key;
printNumbers(array);
}
}
}
Use your package;
Import Scanner
Selection:
public class SelectionSort {
public static int[] SSelectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++)
if (arr[j] < arr[index])
index = j;
int temp = arr[index];
arr[index] = arr[i];
arr[i] = temp;
}
return arr;
}
public static void main(String a[]){
int[] arr1 = {12,5,98,48,78,96,6,2};
int[] arr2 = SSelectionSort(arr1);
for(int i:arr2){
System.out.print(i);
System.out.print(", ");
}
}
}
Use your package;
Import Scanner
public class QuickSort {
private int array[];
private int length;
public void sort(int[] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
//move index to next position on both sides
i++;
j--;
}
}
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
private void exchangeNumbers(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String a[]){
QuickSort sorter = new QuickSort();
int[] input = {87,74,47,85,76,5,15,2};
sorter.sort(input);
for(int i:input){
System.out.print(i);
System.out.print(" ");
}
}
}
Program 4: Merg Sort
Use your package;
Import Scanner
public class MergeSort {
private int[] array;
private int[] tempArr;
private int length;
public static void main(String a[]){
int[] inputArr = {3,9,8,2,4,7,78,62};
MergeSort mm = new MergeSort();
mm.sort(inputArr);
for(int i:inputArr){
System.out.print(i);
System.out.print(" ");
}
}
public void sort(int inputArr[]) {
this.array = inputArr;
this.length = inputArr.length;
this.tempArr = new int[length];
doMergeSort(0, length - 1);
}
private void doMergeSort(int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
doMergeSort(lowerIndex, middle);
doMergeSort(middle + 1, higherIndex);
mergeParts(lowerIndex, middle, higherIndex);
}
}
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
for (int i = lowerIndex; i <= higherIndex; i++) {
tempArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempArr[i] <= tempArr[j]) {
array[k] = tempArr[i];
i++;
} else {
array[k] = tempArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempArr[i];
k++;
i++;
}
}
}