Question

In: Computer Science

C++, Take this 3 pseudocode and transform them into C++ then implement them making a quick...

C++, Take this 3 pseudocode and transform them into C++ then implement them making a quick sort program using the function of the pseudocode that you changed to C++, also in the main the user need to put the array that he wants to do quick sort with. plz comment everything you do in details this is for a presentation

1. A first draft of pseudocode for the quick sort algorithm follows:

// Sorts theArray[first..last].
quickSort(theArray: ItemArray, first: integer, last: integer): void
if (first < last)
{
Choose a pivot item p from theArray[first..last]
Partition the items of theArray[first..last] about p
// The partition is theArray[first..pivotIndex..last]
quickSort(theArray, first, pivotIndex - 1) // Sort S 1
quickSort(theArray, pivotIndex + 1, last) // Sort S 2
}
// If first >= last, there is nothing to do

2. Pseudocode for function sortFIrstMidLast

if (theArray[first] > theArray[mid])
Interchange theArray[first] and theArray[mid]
if (theArray[mid] > theArray[last])
Interchange theArray[mid] and theArray[last]
if (theArray[first] > theArray[mid])
Interchange theArray[first] and theArray[mid]

3. The following pseudocode describes the partitioning algorithm for an array of at least four entries:
// Partitions theArray[first..last].
partition(theArray: ItemArray, first: integer, last: integer): integer
// Choose pivot and reposition it
mid = first + (last - first) / 2
sortFirstMiddleLast(theArray, first, mid, last)
Interchange theArray[mid] and theArray[last – 1]
pivotIndex = last - 1
pivot = theArray[pivotIndex]
// Determine the regions S 1 and S 2
indexFromLeft = first + 1
indexFromRight = last - 2
done = false
while (not done)
{
// Locate first entry on left that is ≥ pivot
while (theArray[indexFromLeft] < pivot)
indexFromLeft = indexFromLeft + 1
// Locate first entry on right that is ≤ pivot
while (theArray[indexFromRight] > pivot)
indexFromRight = indexFromRight - 1
if (indexFromLeft < indexFromRight)
{
Move theArray[firstUnknown] into S1
Interchange theArray[indexFromLeft] and theArray[indexFromRight]
indexFromLeft = indexFromLeft + 1
indexFromRight = indexFromRight - 1
}
else
done = true
}
// Place pivot in proper position between S 1 and S 2 , and mark its new location
Interchange theArray[pivotIndex] and theArray[indexFromLeft]
pivotIndex = indexFromLeft
return pivotIndex

Solutions

Expert Solution

Quick Sort Program in C++

#include <iostream>

using namespace std;

#include<iostream>
#include<cstdlib>

using namespace std;

// Swapping two values.
void swap(int *a, int *b)
{
   int temp;
   temp = *a;
   *a = *b;
   *b = temp;
}

// Partitioning the array on the basis of values at high as pivot value.
int Partition(int a[], int low, int high)
{
   int pivot, index, i;
   index = low;
   pivot = high;

   // Getting index of pivot.
   for(i=low; i < high; i++)
   {
       if(a[i] < a[pivot])
       {
           swap(&a[i], &a[index]);
           index++;
       }
   }
   // Swapping value at high and at the index obtained.
   swap(&a[pivot], &a[index]);

   return index;
}

// Random selection of pivot.
int RandomPivotPartition(int a[], int low, int high)
{
   int pvt, n, temp;
   n = rand();
   // Randomizing the pivot value in the given subpart of array.
   pvt = low + n%(high-low+1);

   // Swapping pvt value from high, so pvt value will be taken as pivot while partitioning.
   swap(&a[high], &a[pvt]);

   return Partition(a, low, high);
}

// Implementing QuickSort algorithm.
int QuickSort(int a[], int low, int high)
{
   int pindex;
   if(low < high)
   {
       // Partitioning array using randomized pivot.
       pindex = RandomPivotPartition(a, low, high);
       // Recursively implementing QuickSort.
       QuickSort(a, low, pindex-1);
       QuickSort(a, pindex+1, high);
   }
   return 0;
}

int main()
{
   int n, i;
   cout<<"\nEnter the number of data element to be sorted: ";
   cin>>n;

   int arr[n];
   for(i = 0; i < n; i++)
   {
       cout<<"Enter element "<<i+1<<": ";
       cin>>arr[i];
   }

   QuickSort(arr, 0, n-1);

   // Printing the sorted data.
   cout<<"\nSorted Data ";
   for (i = 0; i < n; i++)
           cout<<"->"<<arr[i];

   return 0;
}


Enter the number of data element to be sorted: 4
Enter element 1: 23
Enter element 2: 56
Enter element 3: 12
Enter element 4: 3

Sorted Data ->3->12->23->56


Related Solutions

Write pseudocode for quick find algorithm anf quick union algorithm Write pseudocode for quick find algorithm...
Write pseudocode for quick find algorithm anf quick union algorithm Write pseudocode for quick find algorithm and quick union algorithm
I need this in C# with pseudocode Exercise #2: Design and implement a programming (name it...
I need this in C# with pseudocode Exercise #2: Design and implement a programming (name it EvenOdd) that uses a while loop to determine and print out all even numbers between 50 and 100 on a single line, separated by commas. Then another while loop in the same program to print out all odd numbers between 50 and 100 on a new line, separated by commas. Document your code and properly label the outputs as shown below. Even numbers between...
Code in C++ Learning Outcomes Implement two stacks and use them to implement an infix to...
Code in C++ Learning Outcomes Implement two stacks and use them to implement an infix to prefix expression convertor Stacks A stack is an abstract data type which uses a sequential container and limits access to that container to one end. You may enter or remove from the container, but only at one end. Using the Linked List data structure from your last homework assignment, implement a Stack of type string. The Stack should only have one data member: the...
IN C++ LANGUAGE PLEASE::: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source...
IN C++ LANGUAGE PLEASE::: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2]
Write a C-program to implement the Reader/Writer model for the attached pseudocode modified to produce only...
Write a C-program to implement the Reader/Writer model for the attached pseudocode modified to produce only one million output instead of an infinite amount. Use pthreads and Linux semaphores. Include ten readers with one writer. The writer should produce one million outputs. Verify that the readers receive an equal share of the information produced. Meaning each reader receives about 100,000 of information. Explain your results regardless of the balance. No use mutex or monitor functions. ****PSEUDOCODE**** /* program readersandwriters */...
Use the median of 3 partitioning algorithm (given in the next page) to implement quick sort....
Use the median of 3 partitioning algorithm (given in the next page) to implement quick sort. This algorithm chooses the pivot element as the median of the 3 elements namely: A[p], A[r], and A[(p+r)/2].(Java language) Quicksort(A,p,r) 1 if p 2 N = r- p +1 3 m = Median3(A, p, p + N/2 , r) 4 exchange A[m] with A[r] 5 q = Partition (A,p,r) 6 Quicksort(A,p, q-1) 7 Quicksort(A,q+1,r)
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ......
Implement the following pseudocode in Python Consider the following pseudocode for a sorting algorithm: STOOGESORT(A[0 ... n - 1]) if (n = 2) and A[0] > A[1] swap A[0] and A[1] else if n > 2 m = ceiling(2n/3) STOOGESORT(A[0 ... m - 1]) STOOGESORT(A[n - m ... n - 1]) STOOGESORT(A[0 ... m - 1])
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds...
LargestTwo problem: Based upon the pseudocode code below, implement in C++ a divide-and-conquer algorithm that finds the largest and second largest value from a vector of ints. void LargestTwo (vector l, int left, int right, int & largest, int & secondLargest) • Please write comment for your functions, similar to the one in the pseudocode, to include both pre- and post-conditions. • Comment on the logic of your code, e.g., what is true after the two recursive calls? • Answer...
JAVA ONLY. Program 3: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source...
JAVA ONLY. Program 3: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2] Document your code, properly label the input prompts, and organize the outputs as shown in the following sample runs. Note: for C++, #include and then...
write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT