this is the page no. 255 ..
can some one please help me with please. just give a way to do it
assuming that we have add algs4.jar.
thank you!!
import java.lang.management.*;
public class Sort {
public static void main (String[] args) {
ThreadMXBean bean = ManagementFactory.getThreadMXBean(); // NOTE
int N = Integer.parseInt(args[0]);
System.out.println ("Generating " + N + " random
numbers");
int[] a = new int[N];
for (int i=0; i<N; ++i) a[i] =
(int)(Math.random()*(1<<30));
// To test a particular sorting algorithm, I make a copy of the
original
// array, and then record the current "user time". After running
the
// sort, I compute the elapsed time by taking the difference
between the
// time and the time from before the sort.
if (false)
{
int[] c = new int[N];
for (int i=0; i<N; ++i) c[i] = a[i];
long t = bean.getCurrentThreadUserTime(); // NOTE (t is a *long*)
insertionSort(c);
System.out.printf ("Insertion sort took %f seconds.\n", //
NOTE
(bean.getCurrentThreadUserTime()-t) / 1e9);
}
// Here is a section of code that you could use to start a
mergesort.
// Merge Sort
{
int[] c = new int[N];
for (int i=0; i<N; ++i) c[i] = a[i];
long t = bean.getCurrentThreadUserTime(); // NOTE (t is a *long*)
mergeSort(c);
System.out.printf ("Mergesort took %f seconds.\n", // NOTE
(bean.getCurrentThreadUserTime()-t) / 1e9);
}
// Quick Sort - Feynman Liang 2-18-2012
{
int[] c = new int[N];
for (int i=0; i<N; ++i) c[i] = a[i];
long t = bean.getCurrentThreadUserTime(); // NOTE (t is a *long*)
quickSort(c);
System.out.printf ("Quicksort took %f seconds.\n", // NOTE
(bean.getCurrentThreadUserTime()-t) / 1e9);
}
// Extend testing times over <multiple> loops (set to false
to
// disable)
if (false) {
int multiple = 100;
{
int[] c = new int[N];
long t = bean.getCurrentThreadUserTime(); // NOTE (t is a
*long*)
for (int counter = 0; counter < multiple; counter++) {
for (int i=0; i<N; ++i) c[i] = a[i];
insertionSort(c);
}
System.out.printf ("Insertion sort * %d took %f seconds.\n",
(multiple),
(bean.getCurrentThreadUserTime()-t) / 1e9);
}
{
int[] c = new int[N];
long t = bean.getCurrentThreadUserTime(); // NOTE (t is a
*long*)
for (int counter = 0; counter < multiple; counter++) {
for (int i=0; i<N; ++i) c[i] = a[i];
mergeSort(c);
}
System.out.printf ("Mergesort * %d took %f seconds.\n",
(multiple),
(bean.getCurrentThreadUserTime()-t) / 1e9);
}
{
int[] c = new int[N];
long t = bean.getCurrentThreadUserTime(); // NOTE (t is a
*long*)
for (int counter = 0; counter < multiple; counter++) {
for (int i=0; i<N; ++i) c[i] = a[i];
quickSort(c);
}
System.out.printf ("Quicksort * %d took %f seconds.\n",
(multiple),
(bean.getCurrentThreadUserTime()-t) / 1e9);
}
}
}
/////////////////////////////////////////////////////////////////////////
// This method prints the contents of an array. You might use it
during
// debugging.
/////////////////////////////////////////////////////////////////////////
public static void print(int[] a) {
for (int i=0; i<a.length; ++i)
System.out.println (a[i]);
System.out.println();
}
/////////////////////////////////////////////////////////////////////////
// This method compares the contents of two arrays. You might use
it
// during debugging to compare the results of two different
algorithms.
/////////////////////////////////////////////////////////////////////////
public static void check(int[] a, int[] b) {
for (int i=0; i<a.length; ++i)
if (a[i] != b[i])
throw new RuntimeException ("Error in sorting results");
}
/////////////////////////////////////////////////////////////////////////
// Here's the insertion sort.
/////////////////////////////////////////////////////////////////////////
public static void insertionSort(int[] a) {
int n = a.length;
for (int i=1; i<n; ++i) {
int t = a[i];
int j = i-1;
while (j >= 0 && t < a[j]) {
a[j+1] = a[j];
j--;
}
a[j+1] = t;
}
}
/////////////////////////////////////////////////////////////////////////
// MergeSort
/////////////////////////////////////////////////////////////////////////
public static int[] mergeSort(int[] a) {
// pre: array of integers
// post: sorted in non-desceasing order
int len = a.length;
int blocksize = 1;
while (blocksize < len) {
// invariant: a is in sorted blocks of size
// blocksize/2
int lo = 0;
while (lo < (len - blocksize)) {
// invariant: a[0..lo-1] is sorted in blocks of
// size blocksize
int hi = lo + 2 * blocksize - 1;
// check to see if we've run off a[]
if ((len - 1) < hi)
hi = len - 1;
inPlaceMerge(a,
blocksize,
lo,
hi);
lo = lo + 2*blocksize;
}
blocksize = blocksize * 2;
}
// returns merged sorted array (or single array)
return a;
}
public static void inPlaceMerge(int[] a, int blocksize, int lo,
int hi) {
// pre: two sorted subarrays leftn and right
// post: a combined array sorted in non-decreasing order
int[] merged = new int[hi-lo+1];
int i = lo, j = lo + blocksize, k = 0;
while (i <= lo + blocksize - 1 && j <= hi) {
// invariant: merged[] contains all keys < a[i..lo +
// blocksize-1] and a[j..hi] sorted in non-decreasing
if (a[i] < a[j]) {
merged[k] = a[i];
i++;
}
else {
merged[k] = a[j];
j++;
}
k++;
}
// After i or j runs off its half, copy other remaining half
while(i <= lo + blocksize - 1) {
merged[k] = a[i];
i++;
k++;
}
while(j <= hi) {
merged[k] = a[j];
j++;
k++;
}
// Copy merged back in to a[lo..hi]
for (k = 0; k < merged.length; k++)
a[lo+k] = merged[k];
}
/////////////////////////////////////////////////////////////////////////
// quick Sort
/////////////////////////////////////////////////////////////////////////
public static int[] quickSort(int[] a) {
// pre: a = array of ints
// post: returns a sorted in non-decreasing order
// pass a to recursive qsort method
qsort(a, 0, a.length-1);
return a;
}
public static void qsort(int[] a, int lo, int hi) {
// pivot = median in small array, median of 3 in length >
7
int mid = (lo+hi)/2;
if ((hi - lo + 1) > 7) {
mid = medofthree(a, lo, mid, hi);
}
int pivot = a[mid];
int i = lo, j = hi;
while (i <= j) {
// Invariant: subarray [lo..i-1] contains keys < pivot
// and [j+1..hi] contains keys > pivot, [i-1..j+1]
// contains unpartitioned elements and pivots themselves
// so that i and j converge around the pivot
// set i to index of leftmost key > pivot
while (a[i] < pivot) i++;
// set j to rightmost index of key < pivot
while (a[j] > pivot) j--;
// if not overlapped, swap the two indexes
if (i <= j) {
swap(a, i++, j--);
}
}
// Recursively sort sub-partitions
if (lo < i-1) qsort(a, lo, i-1);
if (i < hi) qsort(a, i, hi);
}
public static void swap(int[] a, int i, int j) {
// swaps key at index i with key at index j in array a
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static int medofthree(int[] a, int lo, int mid, int hi)
{
// finds the median of lo, mid, and hi
if (a[lo] < a[mid]) {
if (a[mid] < a[hi]) return mid;
else {
if (a[lo] < a[hi]) return hi;
else return lo;
}
}
else {
if (a[mid] > a[hi]) return mid;
else {
if (a[lo] > a[hi]) return hi;
else return lo;
}
}
}
}
//I have implemented for merge insertion and quick sort for other just add the rest of the sorting methods and n values and this code outputs a comparision of n different key with time multiplited by 100 like this,
N Insertion*100 Merge*100 Quick*100
500 & 0.01 & 0.09 & 0.07
1000 & 0.03 & 0.03 & 0.01
2500 & 0.12 & 0.05 & 0.04
5000 & 0.49 & 0.09 & 0.08
10000 & 1.99 & 0.19 & 0.12
25000 & 12.5 & 0.53 & 0.41
this is the page no. 255 .. can some one please help me with please. just...
Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...
Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...
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...
Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...
Please write C++ code Description: As a senior software engineer, I would like to determine and prove which sorting algorithm is the most efficient between quick sort, merge sort, and heap sort so that I can recommend it to the development team for inclusion in the code base of an upcoming code optimization project. Acceptance Criteria: Well documented, executable C++ source files including classes for each of the aforementioned sorting algorithms. In addition, each sorting algorithm class implementation should be...
Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: return mlist else: mid=len(mlist)//2 return merge(mergesort(mlist[:mid]),mergesort(mlist[mid:])) Problem 1 (30 points) stable merge sort Consider the following unsorted list of integers containing duplicates where the relative position of each duplicated integer in the unsorted list is noted as a subscript. For example, 1, is at a smaller index than 12. The subscript is ignored when comparing two values since it would not actually...
Can I get some help with this question for c++ if you can add some comments too to help understand that will be much appreciated. Code: #include <cstdlib> #include <getopt.h> #include <iostream> #include <string> using namespace std; static long comparisons = 0; static long swaps = 0; void swap(int *a, int *b) { // add code here } void selectionSort(int *first, int *last) { // add code here } void insertionSort(int *first, int *last) { // add code here }...
?PLEASE READ CAREFULLY QUESTION #1 I’m doing a project which requires you to implement 4 sorting algorithms. Bubble sort pair-wise, Bubble sort list-wise a.k.a selection sort, merge sort, and quick sort. These 4 sorting methods takes in an array of strings and sorts them alphabetically from a-z. I have all 4 sorting algorithms working fine, but I still need to fill out the table. There’s only one section I need help filling out. I basically need help filling out the...
Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...
Interfaces 1. What is inside an interface definition? What does a class do to an interface and what keyword is involved? How does a class do this to an interface (what should we find in the class)? Can an interface have a generic parameter? How do you instantiate an interface as an object? What methods can’t you use on an interface type? Abstract Data Types 2. What does an ADT define? Does an ADT specify programming language and/or data structures...