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

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 */...
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...
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...
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...
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source...
PYTHON ONLY NO JAVA! PLEASE INCLUDE PSEUDOCODE AS WELL! Program 4: Design (pseudocode) and implement (source code) a program (name it LargestOccurenceCount) that read from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the loop). The program should ignore any negative input and should continue to read user inputs until 0 is entered. The program should display the largest value and...
Concerned that some patrons find his menu a little too cumbersome making them take longer to...
Concerned that some patrons find his menu a little too cumbersome making them take longer to decide what to order, Joe Giancarlo decides to test a streamlined menu. He randomly selects 8 families of four to dine (for free) in one of his pizzerias on two consecutive Saturday evenings. On the first Saturday, the patrons receive the current menu and the streamlined menu on the second Saturday. Decision time is measured (in minutes) from the time the patrons are seated...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
Write pseudocode to implement the paranoid algorithm. (You vs 2 other players)
Write pseudocode to implement the paranoid algorithm. (You vs 2 other players)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT