Question

Language is Java. Create an insertion, selection, quick, and merge sort algorithm that sorts 6 9...

Language is Java.

Create an insertion, selection, quick, and merge sort algorithm that sorts 6 9 8 12 3 1 7

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PLEASE GIVE IT A THUMBS UP

nikhil@nikhil-Vostro-15-3568: -/Desktop/CODE File Edit View Search Terminal Help nikhil@nikhil-Vostro-15-3568:-/Desktop/CODE$

class A{
   static void insertionSort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

static void selectionSort(int arr[])
{
int n = arr.length;   
for (int i = 0; i < n-1; i++)
{   
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;   
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

static int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1);
for (int j=low; j<high; 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;
}
static void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi-1);
quickSort(arr, pi+1, high);
}
}

static void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int [n1];
int R[] = new int [n2];   
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];   
int i = 0, j = 0;   
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}   
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}   
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
static void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
int m = (l+r)/2;   
mergeSort(arr, l, m);
mergeSort(arr , m+1, r);
merge(arr, l, m, r);
}
}

static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
  
System.out.println();
}

public static void main(String[] args) {
   int arr[] = {6 ,9 ,8 ,12 ,3 ,1 ,7};
   insertionSort(arr);
   System.out.print("Insertion Sort: ");
   printArray(arr);
   int arr1[] = {6 ,9 ,8 ,12 ,3 ,1 ,7};
   selectionSort(arr1);
   System.out.print("Selection Sort: ");
   printArray(arr1);
   int arr2[] = {6 ,9 ,8 ,12 ,3 ,1 ,7};
   quickSort(arr2,0,6);
   System.out.print("Quick Sort: ");
   printArray(arr2);
   int arr3[] = {6 ,9 ,8 ,12 ,3 ,1 ,7};
   mergeSort(arr3,0,6);
   System.out.print("Merge Sort: ");
   printArray(arr3);
}
}

Matrix.h X index.html x main.js * ~/Desktop/CODE/A.java (CODE) - Sublime Text (UNREGISTERED) File Edit Selection Find View Go

File Edit Selection Find View Goto ~/Desktop/CODE/A.java (CODE) - Sublime Text (UNREGISTERED) Tools Project Preferences Help

Add a comment
Know the answer?
Add Answer to:
Language is Java. Create an insertion, selection, quick, and merge sort algorithm that sorts 6 9...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT