Question

In: Computer Science

Use Quicksort algorithm to sort the following sequences: a.10, 80, 3, 19, 14, 7, 5, 12...

Use Quicksort algorithm to sort the following sequences:

a.10, 80, 3, 19, 14, 7, 5, 12

b.Choose your sequence with 100different(random)integer numbers

c.Choose your sequence with 1000different(random)integer numbers

Please include your pseudocode, input sequence, and output in the report. Also, you need to analyze the computational time for different partition methods(e.g., best/worst/average cases)for sequences b and c.

Will rate!

design analysis and algorithms

Solutions

Expert Solution

#include<stdio.h> 
#include <stdlib.h>

//  function to swap two elements 

void swap(int* a, int* b) 
{ 
        int t = *a; 
        *a = *b; 
        *b = t; 
} 

/* this function place the pivot element at its correct position in the sorted array, and places all smaller than pivot 
to the left of pivot and all greater elements to right of pivot */


int partition (int arr[], int low, int high) 
{ 
        int j;
        int pivot = arr[high]; // pivot element
        int i = (low - 1); // Index of the smaller element 

        for (j = low; j <= high- 1; j++) 
        { 
                // If current element is smaller than the pivot elemment
                if (arr[j] < pivot) 
                { 
                        i++; // increment index of smaller element 
                        swap(&arr[i], &arr[j]); 
                } 
        } 
        swap(&arr[i + 1], &arr[high]); 
        return (i + 1); 
} 


//arr[] is the Array to be sorted, 
//low is the Starting index, 
//high high Ending index 


void quickSort(int arr[], int low, int high) 
{ 
        if (low < high) 
        { 
                /* pi is the partitioning index of the array and arr[p] is now 
                at right place */
                int pi = partition(arr, low, high); 

                // Separately sort elements before 
                // partition and after partition 
                quickSort(arr, low, pi - 1); 
                quickSort(arr, pi + 1, high); 
        } 
} 

// Print array 

void printArray(int arr[], int size) 
{ 
        int i; 
        for (i=0; i < size; i++) 
                printf("%d ", arr[i]); 
        printf("\n"); 
} 

// Main program 
int main() 
{ 
    
    int arr[100], c, n;

    printf("Hundred random numbers in [1,1000]\n");   //  first generated 100 random numbers between 1 to 1000 and stored in the array

    for (c = 1; c <= 100; c++) 
    {
        n = rand() % 100 + 1;
        arr[c] = n;
    }
    
    for (c = 1; c <= 100; c++) 
    {
        printf("%d\t", arr[c]);
    }
    printf("\n");

        int arr_size = sizeof(arr)/sizeof(arr[0]); 
        printf("Size of the randomly generated array is %d", arr_size);
        quickSort(arr, 0, arr_size-1); 
        printf("\nSorted array: \n"); 
        printArray(arr, arr_size);  
} 

The above program will randomly generate 100 numbers and will sort them using quick sort. It can be used to sort part b. and c. of the question.

I/P and O/P

Hundred random numbers in [1,1000]
42 68 35 1 70 25 79 59 63 65 6 46 82 28 62
92 96 43 28 37 92 5 3 54 93 83 22 17 19
96 48 27 72 39 70 13 68 100 36 95 4 12 23
34 74 65 42 12 54 69 48 45 63 58 38 60 24
42 30 79 17 36 91 43 89 7 41 43 65 49 47
6 91 30 71 51 7 2 94 49 30 24 85 55 57
41 67 77 32 9 45 40 27 24 38 39 19 83 30
42
Size of the randomly generated array is 100
Sorted array:
1 2 3 4 5 6 6 7 7 9 12 12 13 17 17 19 19 22 23 24 24 24 25 27 27 28 28 30 30 30 30 32 34 35 36 36 37 38 38 39 39 40 41 41 42 42 42 43 43 43 45 45 46 47 48 48 49 49 51 54 54 55 57 58 59 60 62 63 63 65 65 65 67 68 68 69 70 70 71 72 74 77 79 79 82 83 83 85 89 91 91 92 92 93 94 95 96 96 100

---------------------------------------------------------------------------------------------------------------------------------------------------------

/* C implementation QuickSort */
#include<stdio.h> 

// function to swap two elements 
void swap(int* a, int* b) 
{ 
        int t = *a; 
        *a = *b; 
        *b = t; 
} 

/* this function place the pivot element at its correct position in
the sorted array,and places all smaller than pivot to the left of 
pivot and all greater elements to right of pivot */

int partition (int arr[], int low, int high) 
{ 
        int pivot = arr[high]; // pivot 
        int i = (low - 1); // Index of smaller element 

        for (int j = low; j <= high- 1; j++) 
        { 
                // If current element is smaller than the pivot element 
                if (arr[j] < pivot) 
                { 
                        i++; // increment the index of smaller element 
                        swap(&arr[i], &arr[j]); 
                } 
        } 
        swap(&arr[i + 1], &arr[high]); 
        return (i + 1); 
} 

//arr[] is the Array to be sorted, 
//low is the Starting index, 
//high high Ending index 

void quickSort(int arr[], int low, int high) 
{ 
        if (low < high) 
        { 
                /* pi is partitioning index, arr[p] is now 
                at right place */
                int pi = partition(arr, low, high); 

                // Separately sort elements before 
                // partition and after partition 
                quickSort(arr, low, pi - 1); 
                quickSort(arr, pi + 1, high); 
        } 
} 

/* Print the array */
void printArray(int arr[], int size) 
{ 
        int i; 
        for (i=0; i < size; i++) 
                printf("%d ", arr[i]); 
        printf("\n"); 
} 

//   Main program 

int main() 
{ 
        int arr[] = {10, 80, 3, 19, 14, 7, 5, 12};    //  given array in question
        int n = sizeof(arr)/sizeof(arr[0]); 
        quickSort(arr, 0, n-1); 
        printf("Sorted array: \n"); 
        printArray(arr, n); 
        return 0; 
} 

The above program will sort part a. of the question.

I/P and O/P

Given array:

10, 80, 3, 19, 14, 7, 5, 12

Sorted array:                                                                                                       

3 5 7 10 12 14 19 80


Related Solutions

1a)Use the Lomuto's quicksort algorithm to sort the following list of integers in nondecreasing order for...
1a)Use the Lomuto's quicksort algorithm to sort the following list of integers in nondecreasing order for the first pivot item. Use pointers i and j for swapping items (if necessary) for each step leading to your answer. Note do the problem just for the first pivot item. 123,34,189,56,150,12,9,24 1b)Use the same list of numbers and use the Hoare's quicksort algorithm using pointers l and r and updating the pointers i and j and swapping items. Do the problem just for...
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7...
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7 In java please.
Table - 02 Hardwood Concentration 5% 10% 15% 20% 7 12 14 19 8 17 18...
Table - 02 Hardwood Concentration 5% 10% 15% 20% 7 12 14 19 8 17 18 25 15 13 19 22 11 18 17 23 9 19 16 18 10 15 18 20 A manufacturer of paper used for making grocery bags is interested in improving the tensile strength of the product. Product engineering thinks that tensile strength is a function of the hardwood concentration in the pulp and that the range of hardwood concentrations of practical interest is between...
Promotional expenses(x) Sales(y 7 12 10 14 9 13 4 5 11 15 5 7 3...
Promotional expenses(x) Sales(y 7 12 10 14 9 13 4 5 11 15 5 7 3 4 a) draw the scatter plot and draw the line of best fit b) Calculate and interpret the correlation between promotional expenses and sales C) Calculate the regression equation( calculate the slope and intercept of the regression line d)Interpret the slop coefficient of regression equation e)Using the regression equation calculate the sales volume with respect to promotional expense of 4. f) Obtain the coefficient...
Consider the following sample:                 x 9 6 7 5 8 y 19 14 16 12...
Consider the following sample:                 x 9 6 7 5 8 y 19 14 16 12 15              Calculate the coefficient of correlation. Does correlation coefficient provide information about the relationship between x and y? Explain. Find the linear regression line. Find y, if x is 10.
Use the sample space (5, 6, 7, 8, 9, 10, 11, 12, 13, 14) to find...
Use the sample space (5, 6, 7, 8, 9, 10, 11, 12, 13, 14) to find the probability for a random selected a) P(integer) b) P(less than 10 | less than 13) c) P(greater than 8 | less than 11) d) P(greater than 7 | greater 12)
Match No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14...
Match No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Player A 8 42 56 68 91 123 12 46 57 137 5 80 14 10 19 Player B 38 44 46 59 57 61 48 42 51 39 58 41 55 45 68 1. For the given data set representing the runs scored by two players in last 15 matches, conduct the following analysis: i. Which average you will use to summarize...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
Use Booth’s algorithm to multiply -5 and 7.
Use Booth’s algorithm to multiply -5 and 7.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT