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: ";
int length = (sizeof(theArray)/sizeof(theArray[0]));
int first = 0;
int last = length - 1;
//Display array of unsorted items
for(int count = 0; count < length; count++)
{
cout << theArray[count] << " ";
}
cout << "Sort list of 25 items: " << endl;
//Call quickSort() method to sort array
quickSort(theArray, first, last);
cout << "Sorted List: \n" << endl;
for(int count = 0; count < length; count++)
{
cout << theArray[count] << " " << "\n";
}
string stringArray[] = { "g", "r", "e", "w", "a", "r", "d", "s", "d", "f", "v", "r", "z", "w", "b",
"q", "p", "a", "z", "s", "l", "p", "u", "d", "x"};
length = (sizeof(stringArray)/sizeof(stringArray[0]));
first = 0;
last = length - 1;
// Display unsorted string array
cout << "List of 25 items: " << endl;
for (int count = 0; count < length; count++)
{
cout <
}
cout << "Sort list of 25 string items: " << endl;
//Call quickSort() to start string array
quickSort(stringArray, first, last);
cout << "Sorted List: " << endl;
for(int count = 0; count < length; count++)
{
cout << stringArray[count] << " " << "\n";
}
return 0;
}
-------------------------------------------------------------------------------------------------quickSort.cpp
#include "insertionSort.cpp"
#include
using namespace std;
template
void quickSort(ItemType theArray[], int first, int last)
{
const int MIN_SIZE = 4;
if ((last - first + 1) < MIN_SIZE)
{
cout << "Calling insertionSort with size " << last - first + 1 << endl;
insertionSort(theArray, first, last);
}
else
{
// Create the partition: S1 | Pivot | S2
int pivotIndex = partition(theArray, first, last);
int leftSize = pivotIndex - first;
int rightSize = last - pivotIndex;
cout << "Calling quickSort with left size " << leftSize <<
"and right size " << rightSize << endl;
// Sort subarrays S1 and S2
quickSort(theArray, first, pivotIndex - 1);
quickSort(theArray, pivotIndex + 1, last);
} // end if
} // end quickSort
template
int partition(ItemType theArray[], int first, int last)
{
//Choose pivot and reposition it
int mid = first + (last - first) / 2;
sortFirstMiddleLast(theArray, first, mid, last);
swap(theArray[mid], theArray[last-1]);
int pivotIndex = last - 1;
ItemType pivot = theArray[pivotIndex];
//Determine the regions S1 and S2
int indexFromLeft = first + 1;
int indexFromRight = last - 2;
bool done = false;
while(!done)
{
//Locate first entry on left that is >= pivot
while(theArray[indexFromLeft] < pivot)
{
indexFromLeft = indexFromLeft + 1;
}
//Locate first entry on the right that is <= pivot
while(theArray[indexFromRight] > pivot)
{
indexFromRight = indexFromRight - 1;
}
if(indexFromLeft < indexFromRight)
{
swap(theArray[indexFromLeft], theArray[indexFromRight]);
indexFromLeft = indexFromLeft + 1;
indexFromRight = indexFromRight - 1;
}
else
{
done = true;
}
}
//Place pivot in proper position between S1 and S2 and mark its new location
swap(theArray[pivotIndex], theArray[indexFromLeft]);
pivotIndex = indexFromLeft;
return pivotIndex;
}
template
void sortFirstMiddleLast(ItemType theArray[], int first, int mid, int last)
{
if (theArray[first] > theArray[mid])
{
swap(theArray[first], theArray[mid]);
}
if (theArray[mid] > theArray[last])
{
swap(theArray[mid], theArray[last]);
}
if (theArray[first] > theArray[mid])
{
swap(theArray[first], theArray[mid]);
}
}
//function to swap array elements
template
void swap(ItemType &x, ItemType &y)
{
ItemType t = x;
x = y;
y = t;
}
-------------------------------------------------------------------------------------------------inserationSort.cpp
template
void insertionSort(ItemType theArray[], int first, int last)
{
// unsorted = first index of the unsorted region,
// loc = index of insertion in the sorted region,
// nextItem = next item in the unsorted region.
// Initially, sorted region is theArray[0],
// unsorted region is theArray[1..n-1].
// In general, sorted region is theArray[0..unsorted-1],
// unsorted region theArray[unsorted..n-1]
for (int unsorted = first + 1; unsorted <= last; unsorted++)
{
// At this point, theArray[first..unsorted-1] is sorted.
// Find the right position (loc) in theArray[first..unsorted]
// for theArray[unsorted], which is the first entry in the
// unsorted region; shift, if necessary, to make room
ItemType nextItem = theArray[unsorted];
int loc = unsorted;
while ((loc > first) && (theArray[loc - 1] > nextItem))
{
// Shift theArray[loc - 1] to the right
theArray[loc] = theArray[loc - 1];
loc--;
} // end while
// At this point, theArray[loc] is where nextItem belongs
theArray[loc] = nextItem; // Insert nextItem into sorted region
} // end for
} // end insertionSort
I just want to know that if taking length =
sizeof(array)/sizeof(array[0]) is right.
Because I think that array[0] is an integer and hence
sizeof(array[0]) will only give the size of an integer so
length=sizeof(array)/sizeof(array[0]) wont give
the size of the array. And hence the length of the array should be
just sizeof(array) and not sizeof(array)/sizeof(array[0]).
Kindly revert back on this, so we can further discuss the answer.
C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...
quickSort function. Calling previous functions already implemented. This is a bit different type of quicksort function where my partition function has 3 parameters instead of 4. So I need to write a function quicksort that actually puts all 3 of my functions together and finalizes the sorting process "There should be almost nothing done besides calling the other 3 functions and recursively calling itself (except to check for basecase) class quicksort { private: int left; int right; int* array; ...
Language C++ (Please include a short description & Screenshot of output) Implement a Priority queue using a SORTED list. Use Quick sort after adding a new node. Example of quick sort below. Adopt to your program the code below. #include <iostream> void quickSort(int a[ ], int first, int last); int pivot(int a[], int first, int last); void swap(int& a, int& b); void swapNoTemp(int& a, int& b); void print(int array[], const int& N); using namespace std; int main() { int test[]...
C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...
C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)" The objective of the assignment is to revise the public method add in class template LinkedBag so that the new node is inserted at the end of the linked chain instead of at the beginning. This is the code I have: linkedBag-driver.cpp BagInterface.hpp LinkedBag.hpp Node.hpp int main) LinkedBag<string> bag; cout << "Testing array-based Set:" << endl; cout << "The initial bag is...
c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...
//Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...
//CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm. #include <iostream> using std::cout; using std::endl; template<class T> void sort(T a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //The assignment and < operator work for values of type T. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <=... <= a[numberUsed -...
I want to compare the runtimes and swap operations times among quick Sort, selection Sort and shell Sort here is my code: But when I create a 1000,000 size array, I can't get the result of the operations times and runtime. what's wrong with my code? and I also want to copy the array. Because I want to use same array for three sort. And for the shell Sort, I haven't learn it in my class. Can anyone help me...
Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...