Question

Implement quicksort and bucket sort. Use the code in your book to help; the partition in...

Implement quicksort and bucket sort. Use the code in your book to help; the partition in quicksort is tricky. Make sure your implementations are correct — it is easy to gain some confidence in the correctness of your code by writing a program which creates arrays filled with random numbers, sorts them, and then checks that they are sorted.

Then time your code (using clock() or similar methods) on both methods for arrays filled with random integers of the following sizes (where n is the number of elements to be sorted, and m is the maximum integer value of the items in the input array):

1) n is very large,

2)m is very small n is very small, m is very large

3)Find values of n and m where both implementations take (approximately) equal time.

Discuss where quicksort performs best, and where bucket sort performs best.

A clock time of 0 isn't very meaningful. When timing your code, try doing the same thing many times (like 100 times or more) to get more accurate results. Show both your code and your results. Experiment with the values of m and n and with your code to get reasonable, interesting, reportable results.

Note that the quicksort code in the book is incorrect if you remove the dependence on insertionSort. If you include insertion sort, the code is correct. If you want to eliminate insertion sort, then you should do the following: surround line 27 in quicksort (the "restore pivot" step) with an if block:

if (left + 1 < right) {

/* 27 */ swap(a[i], a[right - 1]); // Restore pivot

}

Language is C++

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

Results:

n m bucket_time qucik_sort_time

100001 99001 0.001054 4.19768
10 100000 0.00027 1e-06
100000 10 9e-05 0.458895
100000 3000 0.000143 0.032271

//CODE:

#include<bits/stdc++.h>
#include<utility>
#include<time.h>
using namespace std;
void BucketSort(int A[],int size,int Buckets)
{
int bucket[Buckets]={0};
int i,j,k;
for(i=0;i<size;i++)
++bucket[A[i]];
for(i=0,j=0;j<Buckets;j++)
{
for(k=bucket[j];k>0;k--)
A[i++]=j;
}
}
void InsertionSort(int A[],int size)
{

int i,j,k;
for(i=1;i<size;i++)
{
k=A[i];
j=i;
while(A[j-1]>k && j>=1)
{
A[j]=A[j-1];
j--;
}
}
}
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}

int partition (int arr[], int low, int high)
{
int pivot = arr[high];   
int i = (low - 1);
  
for (int j = low; j <= high- 1; j++)
{
if (arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
  

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);
}
}


//-(100000*(100-loop))+1;
//-(100000*loop)+1;
int main()
{

srand(time(0));
int loop=0;
while(loop<1)
{
int m=3000;
int n=100000;
int i=0,A[n];
for(int i=0;i<n;i++)
{
A[i]=rand()%m+1;
}
clock_t BucketSortTime;
clock_t quickSortTime;
BucketSortTime = clock();
BucketSort(A,n,m);
BucketSortTime = clock()-BucketSortTime;
quickSortTime = clock();
quickSort(A, 0, n-1);
quickSortTime = clock()-quickSortTime;
cout<<n<<" "<<m<<" "<<(float)BucketSortTime/CLOCKS_PER_SEC<<" "
<<(float)quickSortTime/CLOCKS_PER_SEC<<endl;
loop++;
}
}
  

Add a comment
Know the answer?
Add Answer to:
Implement quicksort and bucket sort. Use the code in your book to help; the partition in...
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
  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort...

    This program should test the running time of these algorithms: Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort Heap Sort You have access to the implementation of all of these sorting algorithms, and you may use what is provided in your text directly. Be sure to cite the source of these implementations in the header of your program. Please maintain the property that these sorting algorithms sort arrays in ascending order. For this homework, you will write a...

  • Algorithms and Basic Data Structures

    This part involves writing and testing code. You are to make an experiment with 3 sorting algorithms partially given on Blackboard: Insertion sort, Mergesort, Quicksort. First, create a positive integer array of 10000 (ten thousand) elements with random values in it. Then, run the algorithms on this array by recording their running times. That is, take note of the time just before the sorting starts, and just after the sorting finishes, and record the difference. For a complete experiment, do...

  • [Code in C] Help me with this. Not sure what to do. 1 Couting Sort You...

    [Code in C] Help me with this. Not sure what to do. 1 Couting Sort You may have learned some sorting algorithms - such as bubble sort ad quicksort in CS 110 and CS 210. This homework is about counting sort. Let n be the number of elements to be sorted. Bubble sort and quicksort assume tha time, which one is larger and which one is smaller. They make no assumption on the values of the elements t we can...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • Implement and compare sorting algorithms. The task is to sort a list of integers using 5...

    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...

  • HW60.1. Array Quicksort You've done partition so now it's time to finish Quicksort. Create a publ...

    HW60.1. Array Quicksort You've done partition so now it's time to finish Quicksort. Create a public non-final class named Quicksort that extends Partitioner. Implement a public static method void quicksort (int] values) that sorts the input array of ints in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. You should sort the array in place, which is why your function is declared to return void. If...

  • PROGRAM DESCRIPTION Implement the combined O(n) radix/bucket sort as described in class. (i.e. divide the input...

    PROGRAM DESCRIPTION Implement the combined O(n) radix/bucket sort as described in class. (i.e. divide the input by radix, bucket sort (with no insertion sort step) once for each radix starting from the least significant. Make sure that your overall implementation is O(n) NPUT The input to your program will an unspecified number of entries. Each entry is a non-negative integer containing nine (zero padded) digits ( this means that the integer may have either leading or trailing zeros), one per...

  • The objective of this Assignment is to compare the performance of some standard SORT algorithms. You...

    The objective of this Assignment is to compare the performance of some standard SORT algorithms. You should look at MergeSort, QuickSort, and Insertion Sort; you can also look at an additional Sort mechanism that isn't covered in class or that isn't covered in the book for extra credit. You will have to find the right implementations in Java so that you can make your comparisons. Things you will be comparing are the raw running time and the time complexity in...

  • The code is in python. The file does not have to be the data set from...

    The code is in python. The file does not have to be the data set from HW2. What I need is a code combing selection sort and merge sort. The 2-1 is what the problem in refers to as Q1. d) [10 points] Write a code combining both selection sort and mergesort algorithms (NOT insertion and merge sorts) as described in Q1) to find out the most optimal k minimizing time efficiency of your algorithms using 1 M data set...

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