Question
Data Structures and Algorithms

Help please :)

POLJOPP 3. Consider QuickSort on the array Aſlanand assume that the pivot element x (used to split the array Allo:hil into tw
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The answer of the above question can be given as below:

/* C implementation of QuickSort,
Considering last Element as pivot */
#include <stdio.h>
#include <stdlib.h>


/*Swap Function */
void swap(int* firstVariable, int* secondvariable)
{
   int tempVariable = *firstVariable;
   *firstVariable = *secondvariable;
   *secondvariable = tempVariable;
}

/*Partitioning of Array into two Sub-Array
around Pivot Element */
int partition (int Array[], int left, int right)
{
/*Pivot Element - Array[right]*/
   int pivot = Array[right];
   int i = (left - 1);

   for (int j = left; j <= right- 1; j++)
   {
       if (Array[j] < pivot)
       {
       /*Increment the numbers Smaller than Pivot */
           i++;
           swap(&Array[i], &Array[j]);
       }
   }
   swap(&Array[i + 1], &Array[right]);
   return (i + 1);
}

void quickSort(int Array[], int left, int right)
{
   if (left < right)
   {
       /* Array[pivotIndex] is at it's right position */
       int pivotIndex = partition(Array, left, right);

       /*Independently Sort Elements before
       & after pivotIndex */
       quickSort(Array, left, pivotIndex - 1);
       printArray(Array,left,pivotIndex);

       quickSort(Array, pivotIndex + 1, right);
       printArray(Array,pivotIndex,right);
   }
}

void printArray(int Array[],int left,int right)
{
   int i;
   for (i=left; i < right; i++)
       printf("%d ", Array[i]);
   printf("\n");
}

int main()
{
   int Array[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};
   int sizeOfArray = sizeof(Array)/sizeof(Array[0]);
   quickSort(Array, 0, sizeOfArray-1);
   printf("\nSorted array: \n");
   printArray(Array, 0,sizeOfArray);
   return 0;
}
/*Screen Shot */

main.c X WNH F/* c implementation of Quicksort, Considering last Element as pivot */ #include <stdio.h> #include <stdlib.h> /

swap (Array[i], Array[j]); swap (&Array[i + 1), «Array[right]); return (i + 1); void quicksort(int Array[], int left, int rig

printf(%d, Array[i]); printf(\n); int main() int Array[] = {1,2,3,4,5,6,7,8, 9, 10, 11, 12, 13, 14}; int sizeofArray = si

Question 3)

(b) When Array[1:N] will be in sorted order then Execution will be slowest .

Assignment of Numbers:

[1,2,3,4,5,6,7,8,9,10,11,12,13,14]

Number Of Comparison = O(n^{2})

C:\Users\Amit Shawlcode_blocks quick_sort\bin\Debug quick_sort.exe 12 123 1234 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5

Question 3: a)

When Elements Are Stored in post Order Traversal of a Binary Tree optimal Execution.

Assignment of Numbers:

[1,3,2,5,4,7,9,8,11,13,12,6]

Number of Comparison = O(n log n)

11 2 3 1 2 3 4 5 12 11 12 9 11 12 7 9 11 12 6 7 9 11 12 Sorted array: 1 2 3 4 5 6 7 9 11 12 13 execution time: 0.132 s Proces

Please like the post and comment for any query related to the above problem

Thanks for liking the post !

Add a comment
Know the answer?
Add Answer to:
Data Structures and Algorithms Help please :) POLJOPP 3. Consider QuickSort on the array Aſlanand assume...
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
  • 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...

  • Walk through the operation of QuickSort when n = 7 and the input array is A...

    Walk through the operation of QuickSort when n = 7 and the input array is A = (11, 13, 12, 32, 31, 33, 20). (a) Count the number of comparisons in the walk through. using LAST ELEMENTS (LAST ANSWER WAS NOT USING LAST ELEMENT AS PIVOT) (b) Evaluate 7!, lg(7!) and 7 x lg(7). (c) Construct a best-case example for QuickSort with n = 15.

  • And the related algorithms: (20 points) Consider the following strategy for choosing a pivot element for...

    And the related algorithms: (20 points) Consider the following strategy for choosing a pivot element for the Partition subroutine of QuickSort, applied to an array A. .Let n be the number of elements of the array A. If n 24, perform an Insertion Sort of A and return. Otherwise: Choose 2n/2)| elements at random from n; let S be the new list with the chosen elements. Sort the list S using Insertion Sort and use the median m of S...

  • Someone help please 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...

  • program in python Randomness can be used to improve the performance of deterministic algorithms which need...

    program in python Randomness can be used to improve the performance of deterministic algorithms which need to make many choices. Rather than repeatedly making fixed, hard-coded choices, a pseudorandom number generator can be used to make dynamic, unbiased choices. If the benefits of "good" choices outweigh the costs of "bad" choices, a random selection of good and bad choices can improve the performance of an algorithm Let us explore this with the QUICKSELECT algorithm. Discovered by the influential computer science...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • Data Structures and Algorithms C++: I'm having a hard time getting my main.cpp part of the...

    Data Structures and Algorithms C++: I'm having a hard time getting my main.cpp part of the source code (shown below) to output the following: Inserting elements to array list: The list contains 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Deleting elements: The list contains: 2 4 6 8 10 12 14 16 18 20 22 24 this is a programming problem in...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Assignment 3 In this assignment, you will write a program that plot ASCII text approximations of...

    Assignment 3 In this assignment, you will write a program that plot ASCII text approximations of the Mandelbrot set. Problems Problem 1 Consider the function ?(?,?) defined as follows: ?(?,?)(?, ?) = (?2 − ?2 + ?, 2?? + ?) We define the orbit ?(?, ?) of a point (?, ?) to be an infinite list of items: ?(?, ?) = {(0, 0), ?(?,?)(0, 0), ?(?,?)(?(?,?)(0, 0)), ?(?,?)(?(?,?)(?(?,?)(0, 0))), …} In other words, the nth entry of the list ?(?,...

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