Please find the required program along with its output. Please see the comments against each line to understand the step.
import java.util.Arrays;
class Test{
public static void main(String[] args) {
double result[] = partition(new double[]{10,7,45,-44,33,1});
System.out.println("Result Array : "+Arrays.toString(result));
}
public static double[] partition(double a[]){
double[] lesser = new double[a.length]; //declare lesser array
double[] greater = new double[a.length]; //declare greater array
double pivot = a[0]; //get the pivot
int li = 0, gi = 0; //initialize lesser and greater indexes
for(int i=1; i<a.length; i++){ //iterate through all elements in a
if(a[i]<pivot){ //if current iteration element is less than pivot, add it to lesser array and update the index li
lesser[li] = a[i];
li++;
} else if(a[i] >= pivot){ //else if current iteration element is greater than or equal to pivot, add it to greater array and update the index gi
greater[gi] = a[i];
gi++;
}
}
double[] result = new double[a.length]; //initialize the result array
int k=0;
for(int i=0; i<li; i++) { //iterate through index 0 to li(will contain no:of elements in lesser array) of lesser array and add it to new result array
result[k++] = lesser[i];
}
result[k++] = pivot; //add pivot element to the result array
for(int i=0; i<gi; i++) { //iterate through index 0 to gi(will contain no:of elements in greater array) of greater array and add it to new result array
result[k++] = greater[i];
}
return result; //return the result array
}
}
--------------------------------------------------------------
OUTPUT:
Result Array : [7.0, -44.0, 1.0, 10.0, 45.0, 33.0]
Answer with simple java code and comments This homework deals with the problem of partitioning an...
(7 pts) Problem 1 Consider this algorithm for partitioning an array a: 1. Create two new lesser and greater arrays. These will be used to store the elements of a that are sor > the pivot element, respectively. 2. Loop through all elements of a besides the pivot. If the element is the pivot, copy it into lesser. If the element is > the pivot, copy it into greater. 3. Create a new result array, of the same length as...
Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...
Assume that you are sorting an array of 8 elements with quick sort. You just finished the first pass and the array looks like below. Which statement is true for the pivot value? 4 8 12 16 18 20 22 24 QUICKSORT ALGORITHM Quicksort selects a specific value called a pivot and rearranges the array into two parts (called partioning). If the array is randomly ordered, it does not matter which element is the pivot. For simplicity, the first element...
_______________________________________________________________________________________________
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...
I want you to now remember the following partitioning problem we considered a while ago. You are given a list L of length n and asked to partition the elements of L into two sublists L_1 and L_2 such that (i) n/3 lessthanorequalto |L_1|, |L_2| lessthanorequalto 2n/3 and (ii) all elements in L_1 are less than or equal to all elements in L_2. We designed a simple, randomized (Las Vegas) algorithm for this problem that ran in O(n) expected time....
c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
/* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Student { public: // default constructor Student() { studentID = 0; studentFirst = "First"; studentMiddle = "Middle"; studentLast = "Last"; } void SetStudentID(int number) { studentID = number; } void SetStudentFirst(string name) { studentFirst = name; } void SetStudentMiddle(string name) { studentMiddle =...
I am trying to implement this version of quicksort in C/c++ but I am getting stuck. Below is how the algorithm is supposed to work. This is the partitioning scheme I am trying to implement. 17, -10, 7, 19, 21, 23, -13, 31, 59 # ^ ^ start 17, -10, 7, 19, 21, 23, -13, 31, 59 # ^ ^ move left pointer to first element larger than pivot. 3 compares 17, -10, 7, 19, 21, 23, -13, 31, 59...