package Sort;
// Defines a class to perform insertion and quicksort
public class InsertionQuickStep
{
// Method for insertion sort
public static void insertionSort(int array[])
{
//Stores the length of array
int len = array.length;
//Loops till length minus one times
for (int c = 1; c < len; ++c)
{
//Stores i index position value as key value
int key = array[c];
int d = c - 1;
// Move elements of array[0..i-1], that are greater than key, to one position ahead of their current position
while (d >= 0 && array[d] > key)
{
System.out.println(array[d] + " > " + key);
// Swaps each element of the array one position to right
array[d + 1] = array[d];
d = d - 1;
}//End of while
//Insert the key value at d plus one position
array[d+1] = key;
}//End of for
}//End of method
// Recursive method to apply quick sort
static void Quicksort(int[] A, int p, int r)
{
// Checks if left is less than right
if (p < r)
{
// Partition to find the "right place" for the leftmost element.
int q = Partition (A, p, r);
// Recurse on the left side:
Quicksort(A, p, q - 1);
// Recurse on the right side:
Quicksort(A, q + 1, r);
}// End of if condition
}// End of method
// Method to perform partition
static int Partition (int[] array, int p, int r)
{
// Stores the right index position of the array data as partition element
int x = array[r];
// Stores the start -1
int i = p - 1;
// Loops till right is greater than or equals to left
for (int j = p; j <= r - 1; j++)
{
System.out.println(array[j] + " > " + x);
// Checks if current index position of the array is less than or equals to x value
if (array[j] <= x)
{
// Switch with swap position
i++;
// Calls the method to swap i index position with j index position
swap (array, i, j);
// Shift swap position rightwards:
}// End of if condition
}// End of for loop
// Calls the method to swap i+1 index position with r (last) index position
swap (array, i + 1, r);
// Returns i value added to one for next position
return i + 1;
}// End of method
// Method to swap
static void swap (int[] array, int i, int j)
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}// End of method
// Method to display array contents
static void show(int A[])
{
// Loops till length of the array
for(int x = 0; x < A.length; x++)
// Displays each element
System.out.printf("%4d", A[x]);
}// End of method
// main function definition
public static void main(String pyari[])
{
// Creates an array
int arrayIns[] = {23, 45, 10, 2, 89, 62, 76, 33};
int arrayQui[] = {23, 45, 10, 2, 89, 62, 76, 33};
System.out.println("\n ********* Before Insertion Sort ********* ");
show(arrayIns);
System.out.println("\n ********* Comparison of Insertion Sort ********* ");
// Calls the method to perform insertion sort
insertionSort(arrayIns);
System.out.println("\n ********* After Insertion Sort ********* ");
show(arrayIns);
System.out.println("\n\n ********* Before Quick Sort ********* ");
show(arrayQui);
System.out.println("\n ********* Comparison of Quick Sort ********* ");
// Calls the method to perform sorting
Quicksort(arrayQui, 0, arrayQui.length-1);
System.out.println("\n ********* After Quick Sort ********* ");
show(arrayQui);
}// End of main
}// End of class
Sample Output:
********* Before Insertion Sort *********
23 45 10 2 89 62 76 33
********* Comparison of Insertion Sort *********
45 > 10
23 > 10
45 > 2
23 > 2
10 > 2
89 > 62
89 > 76
89 > 33
76 > 33
62 > 33
45 > 33
********* After Insertion Sort *********
2 10 23 33 45 62 76 89
********* Before Quick Sort *********
23 45 10 2 89 62 76 33
********* Comparison of Quick Sort *********
23 > 33
45 > 33
10 > 33
2 > 33
89 > 33
62 > 33
76 > 33
23 > 2
10 > 2
10 > 23
89 > 45
62 > 45
76 > 45
62 > 89
76 > 89
62 > 76
********* After Quick Sort *********
2 10 23 33 45 62 76 89
4. (7 pts) Let A be an array of 5 integers, whose contents are as follows:...
Someone help please Let A be an array of 5 integers, whose contents are as follows: 3, 2, 1, 5, 4 We will apply quick sort to sort this array. Show all of the element-wise comparisons made by the algorithm in the correct order. Here an element-wise comparison means the comparison of one element of the array with another element of the array or the key set in a particular step of the algorithm. Since the algorithm may move the...
4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...
_______________________________________________________________________________________________ java language-trace instructions". (20 points) Show the contents of the array below, once the contents of the array below, once the "pivot" element is placed at its location after each call of the "Partition” algorithm, in the process of running Quick-Sort on said array. Arrange the data in ascending order (Trom Arrange the data in ascending order (from smallest to largest value). Always select the first element of the partition as "pivot" in data cat B. Apply sorting on...
The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...
JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...
Show the contents of the array below, once the “pivot” element is placed at its appropriate location after each call of the “Partition” algorithm, in the process of running Quick-Sort on said array. Arrange the data in ascending order (from smallest to largest value). Always select the first element of the partition as “pivot” Apply sorting on the following data set 19, 20, 1, 13, 16, 5, 4, 9, 14, 7 Index 0 1 2 3 4 5 6 7...
You want to sort (in increasing order) the following array of integers using quicksort as we have described it and used it in class. You are asked to specifically show your steps and the resulting array after one pass of quicksort. Show and explain each of your steps. Note 1: in case you are not using the algorithm presented and traced in class, you are expected to show all your steps accompanied with algorithm instructions and variables' values. Note 2:...
Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...
In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...
Introduction BUBBLE SORT: Step-by-step example Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in bold are being compared. Three passes will be required First Pass: ( 5 1 4 2 8 ) → ( 1 5 4 2 8 ). Here, algorithm compares the first two elements, swaps since 5 (15428)→(14528). Swap since 5 > 4 (14528)→(14258). Swap...