Note: Marge sort and Shell sort is missing….
Class: Array
import [Link].*;
public abstract class Array {
String s;
int arr[];
Scanner input = new Scanner([Link]);
public Array(){
[Link]("Input the size of array");
int length = [Link]();
arr = new int[length];
public void input() {
[Link]("input " + [Link] + " values for array");
for(int x = 0; x < [Link]; x++)
arr[x] = [Link]();
public abstract void sort();
public void display() {
[Link]("Output");
for(int x = 0; x < [Link]; x++)
[Link](arr[x]);
}
Class: BubbleSort
public class BubbleSort extends Array {
public void sort() {
int x, y;
for(x = 0; x < [Link]-1; x++)
for(y = 0; y < [Link]-1; y++)
if(arr[y] > arr[y+1]) {
int temp = arr[y];
arr[y] = arr[y+1];
arr[y+1] = temp;
}
Class: SelectionSort
public class SelectionSort extends Array{
public void sort() {
for(int y=0; y < [Link] - 1; y++) {
int hold = y;
for(int x = y; x < [Link]; x++)
if(arr[hold] > arr[x]) {
int temp = hold;
hold = x;
x = temp;
}
int temp = arr[hold];
arr[hold] = arr[y];
arr[y]= temp;
}
Class: InsertionSort
public class InsertionSort extends Array{
public void sort() {
for(int y=1; y < [Link]; y++)
for(int x=y; x > 0; x--)
if(arr[x] < arr[x-1]) {
int temp = arr[x];
arr[x] = arr[x-1];
arr[x-1] = temp;
}
}
Class: QuickSort
public class QuickSort extends Array {
public void sort() {
Qsort(arr, 0, [Link] - 1);
public void Qsort(int arr[], int low, int high) {
int pi;
if(low < high) {
pi = partation(arr, low, high);
Qsort(arr, low, pi-1);
Qsort(arr, pi+1, high);
private int partation(int arr[], int low, int high) {
int i = low - 1;
int pivot = arr[high];
for(int j = low; j <= high - 1; j++)
if(arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i + 1;
}
}
Class: MargeSort
public class MargeSort extends Array {
public void sort() {
[Link]("marge sort is not availabe");
}
Class: ShellSort
public class ShellSort extends Array{
public void sort() {
[Link]("shell sort is not availabe");
}
Class: Execute
import [Link].*;
public class Execute {
public static void main(String args[]) {
Array obj = null;
[Link]("input"
+ "\n 1 for bubble sort,"
+ "\n 2 for selection sort,"
+ "\n 3 for insertion sort,"
+ "\n 4 for quick sort,"
+ "\n 5 for marge sort,"
+ "\n 6 for shell sort");
Scanner input = new Scanner([Link]);
switch([Link]()) {
case 1:
obj = new BubbleSort();
break;
case 2:
obj = new SelectionSort();
break;
case 3:
obj = new InsertionSort();
break;
case 4:
obj = new QuickSort();
break;
case 5:
obj = new MargeSort();
break;
case 6:
obj = new ShellSort();
break;
default:
[Link]("unexcepted input, terminating the program");
[Link](0);
break;
[Link]();
[Link]();
[Link]();
[Link]();
[Link](0);