Please help with this JAVA Assignment and show output if you
Please help with this JAVA Assignment and show output if you can please
Complete required scripts based on eclipse and troubleshoot the scripts until the tasks are done.
1. Implement linear-sort, merge-sort and quick-sort (write the code for them).
Solution
Linear or Insertion Sort
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
class LinearSort
{
public static void sort( int values[] )
{
int N = values.length;
int i, j, temp;
for (i = 1; i< N; i++)
{
j = i;
temp = values[i];
while (j > 0 && temp < values[j-1])
{
values[j] = values[j-1];
j = j-1;
}
values[j] = temp;
}
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
int n, i;
System.out.println(\"Enter number of elements\");
n = scan.nextInt();
int values[] = new int[n];
System.out.println(\"\ Enter elements\");
for (i = 0; i < n; i++)
values[i] = scan.nextInt();
sort(values);
System.out.println(\"\ Sorted Elements\");
for (i = 0; i < n; i++)
System.out.print(values[i]+\" \");
System.out.println();
}
}
Output:
Enter number of elements
6
Enter elements
34 56 12 8 9 23
Sorted Elements
8 9 12 23 34 56
Merge Sort
import java.util.Scanner;
public class Mergesort
{
public static void mergesort(int[] a, int low, int high)
{
int N = high - low;
if (N <= 1)
return;
int mid = low + N/2;
mergesort(a, low, mid);
mergesort(a, mid, high);
int[] temp = new int[N];
int i = low, j = mid;
for (int k = 0; k < N; k++)
{
if (i == mid)
temp[k] = a[j++];
else if (j == high)
temp[k] = a[i++];
else if (a[j]<a[i])
temp[k] = a[j++];
else
temp[k] = a[i++];
}
for (int k = 0; k < N; k++)
a[low + k] = temp[k];
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
int n, i;
System.out.println(\"Enter number of elements\");
n = scan.nextInt();
int arr[] = new int[ n ];
System.out.println(\"\ Enter elements\");
for (i = 0; i < n; i++)
arr[i] = scan.nextInt();
mergesort(arr, 0, n);
System.out.println(\"\ Sorted Order:\");
for (i = 0; i < n; i++)
System.out.print(arr[i]+\" \");
System.out.println();
}
}
Output:
Enter number of elements
5
Enter elements
3 45 1 2 9
Sorted Order:
1 2 3 9 45
Quick Sort:
import java.util.Scanner;
class Quicksort
{
public static void quicksort(int[] values)
{
quickquicksort(values, 0, values.length - 1);
}
public static void quickquicksort(int values[], int low, int high)
{
int i = low, j = high;
int temp;
int pivot = values[(low + high) / 2];
while (i <= j)
{
while (values[i] < pivot)
i++;
while (values[j] > pivot)
j--;
if (i <= j)
{
temp = values[i];
values[i] = values[j];
values[j] = temp;
i++;
j--;
}
}
if (low < j)
quickquicksort(values, low, j);
if (i < high)
quickquicksort(values, i, high);
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
int n, i;
System.out.println(\"Enter number of elements\");
n = scan.nextInt();
int values[] = new int[ n ];
System.out.println(\"\ Enter elements\");
for (i = 0; i < n; i++)
values[i] = scan.nextInt();
quicksort(values);
System.out.println(\"\ qSorted Elements \");
for (i = 0; i < n; i++)
System.out.print(values[i]+\" \");
System.out.println();
}
}
Output:
Enter number of elements
8
Enter elements
89 23 90 56 13 71 46 13
Sorted Elements
13 13 23 46 56 71 89 90



