Quick Sort with Insertion Sort - c++

I've been working on this code for hours. The goal is to write an optimized QuickSort (with Insertion sort) on an array of pointers (which point to objects that can be compared). Insertion sort is supposed to be used with array sizes < 4.
So far, I have insertion sort working when I pass in an array < 4.
The quicksort is supposed to use the middle index as a pivot, and move everything < the pivot to the left of pivot, and everything > the pivot to the right of pivot.
I'm not even sure my overall approach to quickSort is correct. This is my first attempt at writing a quick sort. I could really use a nudge in the right direction here. The code that's commented out is something I've already tried.
If anything is unclear, let me know. Thanks for the help!
void quickSort(Comparable ** array, int fromIndex, int toIndex)
{
while (fromIndex < toIndex)
{
if ((toIndex - fromIndex +1 ) < 4)
{
insertionSort(array, fromIndex, toIndex);
break;
}
else
{
int pivotIndex = partition(array, fromIndex, toIndex);
quickSort(array, fromIndex, pivotIndex - 1);
quickSort(array, pivotIndex + 1, toIndex);
}
}
}
int partition(Comparable ** array, int fromIndex, int toIndex)
{
//Comparable *front = array[fromIndex+1];
int midIndex = (toIndex + fromIndex) / 2;
//int frontIndex = fromIndex;
//Comparable *back = array[toIndex - 1];
//int backIndex = toIndex - 1;
//Comparable *compare = front;
//int compareIndex = frontIndex;
SortFirstMiddleLast(array, fromIndex, midIndex, toIndex);
swap(array, midIndex, toIndex - 1);
int pivotIndex = toIndex - 1;
Comparable * pivot = array[pivotIndex];
int indexLeft = fromIndex + 1;
int indexRight = toIndex - 2;
bool sortFinished = false;
while (*array[indexLeft] < *pivot)
{
indexLeft++;
}
while (*array[indexRight] > *pivot)
{
indexRight--;
}
if ((*array[indexLeft] >= *pivot) && (*array[indexRight] <= *pivot))
{
if (indexLeft < indexRight)
{
swap(array, indexLeft, indexRight);
indexLeft++;
indexRight--;
sortFinished = true;
}
}
if (sortFinished == true)
{
swap(array, pivotIndex, indexLeft);
pivotIndex = indexLeft;
return pivotIndex;
}
// ++frontIndex; // advance to next element
// while (*array[frontIndex] < *array[backIndex])
// {
// // search forward for out of order element
// while ((*array[frontIndex] < *array[backIndex]) && (*array[fromIndex] > *array[frontIndex]))
// ++frontIndex;
// //search backward for out of order element
// while ((*array[frontIndex] < *array[backIndex]) && (*array[compareIndex] <= *array[backIndex]))
// --backIndex;
// swap(array, frontIndex, backIndex);
// }
// //insert mid position comparison element
// if (*array[compareIndex] >= *array[frontIndex])
// {
// swap(array, fromIndex, frontIndex);
// returnValue = frontIndex;
// }
// else
// {
// swap(array,fromIndex, (frontIndex - 1));
// returnValue = (frontIndex - 1);
// }
// return returnValue;
}
void swap(Comparable ** array, int swapIndex1, int swapIndex2)
{
Comparable * temp = array[swapIndex1];
array[swapIndex1] = array[swapIndex2];
array[swapIndex2] = temp;
}
void SortFirstMiddleLast(Comparable ** array, int fromIndex, int midIndex, int toIndex)
{
// first must be less than mid, must be less than last
if (*array[fromIndex] > *array[midIndex])
{
swap(array, fromIndex, midIndex);
}
if (*array[fromIndex] > *array[toIndex - 1])
{
swap(array, fromIndex, toIndex - 1);
}
if (*array[midIndex] > *array[toIndex - 1])
{
swap(array, midIndex, toIndex - 1);
}
}
void insertionSort(Comparable ** array, int fromIndex, int toIndex)
{
for (unsigned i = fromIndex + 1; i < toIndex; i++)
{
for (unsigned j = i; j > 0; j--)
{
if (*array[j] < *array[j - 1])
{
swap(array, j, j-1);
}
else
break;
}
}
}

Related

Quickselect Algorithm to return Kth smallest element. Problem with the parameters for recursion

I'm trying to write a quickselect function using a random pivot that returns the Kth smallest element, but I can't figure out the correct parameters for recursion. I believe the first portion of the function works perfectly since I adapted from a quicksort function, just the recursive calls I can't get it right.
int quickselect(int* A, int k, int n) {
int i, left_part, right_part, pivot, temp;
if (n > 1)
{
//Choosing a random pivot and putting it at the end of the array
i = rand() % n;
pivot = A[i];
A[i] = A[n - 1];
A[n - 1] = pivot;
left_part = 0; right_part = n - 1;
// While loop is responsible for the comparison of the elements
while (left_part < right_part)
{
for (; A[left_part] < pivot; left_part++)
;
for (; A[right_part] >= pivot && right_part > left_part; right_part--)
;
if (left_part != right_part)
{
temp = A[left_part];
A[left_part] = A[right_part];
A[right_part] = temp;
}
}
// Making sure pivot is in correct position
A[n - 1] = A[left_part];
A[left_part] = pivot;
if (i == k) {
return A[k];
}
else if (i > k) {
// Element is to the left of pivot
return quickselect(A, k, left_part);
}
else if (i < k) {
// Element is to the right of pivot
return quickselect(A + left_part + 1, k - left_part, n - left_part - 1);
}
}
}

C++ Binary search algorithm to work like lower_bound

I do have another question following my previous -
I am creating a version of lower_bound with something like binary search. With the BinarySearch function I find a place where to insert the new item and with the for cycle I do move the rest of the array and insert the right item so I can insert it to the right position.
But the following BinarySearch function does not work properly.
Can anyone see why?
bool CReg::AddCar ( const char *name){
CArr * tmp = new CArr(name); // an item I am going to insert
int pos = BinarySearch(name,0,len); //len = number of items in array
checkLen(); // whether I do have enough space to move the array right
if (length!=0)
for (int i = m_len; i>=pos; i-- )
Arr[i+1] = spzArr[i];
Arr[pos] = tmp;
length++;
checkLen();
return true;
}
int BinarySearch(const char * name, int firstindex, int lastindex) {
if (lenght == 0) return 0; //number of items in array
if (firstindex == lastindex) return lastindex;
int tmp = lastindex - firstindex;
int pos = firstindex + tmp / 2; //position the new item should go to
if (tmp % 2)++pos;
if (lastindex == pos || firstindex == pos) return pos;
if (strcmp(name, Arr[pos]) < 0) return BinarySearch(name, firstindex, pos - 1);
if (strcmp(name, Arr[pos]) > 0) return BinarySearch(name, pos + 1, lastindex);
return pos;
}
A fixed version of BinarySearch
int BinarySearch(const char* name, int firstindex, int lastindex)
{
if (firstindex == lastindex) return lastindex;
int dist = lastindex - firstindex;
int mid = firstindex + dist / 2; //position the new item should go to
if (strcmp(name, Arr[mid]) < 0) return BinarySearch(name, firstindex, mid);
if (strcmp(name, Arr[mid]) > 0) return BinarySearch(name, mid + 1, lastindex);
return mid;
}
But you may directly use std::lower_bound:
// Assuming std::vector<std::string> Arr;
void CReg::AddCar(const std::string& name)
{
auto it = std::lower_bound(Arr.begin(), Arr.end(), name);
Arr.insert(it, name);
}

Loop through 2D array diagonally with random board size

I was wondering how I can loop through a two dimentional array if the size of the array is random, e.g 6x6 or 10x10 etc. The idea is to search for four of the same kind of characters, 'x' or 'o'. This is typically needed for a board game.
int main() {
int array_size = 5; // Size of array
int array_height = array_size;
bool turn = true; // true = player 1, false = player 2
bool there_is_a_winner = false;
char** p_connect_four = new char*[array_size];
for (int i = 0; i < array_size; i++) // Initialise the 2D array
{ // At the same time set a value "_" as blank field
p_connect_four[i] = new char[array_size];
for (int j = 0; j < array_size; j++) {
p_connect_four[i][j] = '_';
}
}
}
This is what I have so far, checking from [3][0] to [0][3]. But this requires me to add 2 more for loops to check [4][0] to [0][4] and [4][1] to [1][4] IF the size of the board was 5x5.
for (int i = 3, j = 0; i > 0 && j < array_size; i--, j++ ) {// CHECK DOWN up right from 3,0 -> 0,3
if (p_connect_four[i][j] == p_connect_four[i - 1][j + 1] && p_connect_four[i][j] != '_' ) {
check_diagonalRight++;
if (check_diagonalRight == 3) {
there_is_a_winner = true;
break;
}
}
else {
check_diagonalRight = 0;
}
}
if (there_is_a_winner) { // Break while loop of game.
break;
}
Obviously I want to check the whole board diagonally to the right regardless of the size of the board. Is there any other way than having 3 separate for loops for checking
[3][0] -> [0][3] , [4][0] -> [0][4] and [4][1]-> [1][4] ?
for (i = array_size - 1, j = array_size - 2;
i < array_size && i >= 0, j < array_size && j >= 0; j--)
{ // starts from [4][3] and loops to the left if arraysize = 5x5
// but works on any size
int k = i, l = j;
for (k, l; k < array_size && k > 0, l < array_size && l > 0; k--, l++)
{ // checks diagonally to the right
if (check_diagonalRight == 3)
{
there_is_a_winner = true;
break;
}
if (p_connect_four[k][l] == p_connect_four[k - 1][l + 1] &&
p_connect_four[k][l] != '_')
{ //check up one square and right one square
check_diagonalRight++;
}
else
{
check_diagonalRight = 0;
// if its not equal, reset counter.
}
}
if (there_is_a_winner)
{
break; // break for loop
}
}
if (there_is_a_winner)
{
break; // break while loop of game
}
This checks up and right no matter the size, implement it for the other angles as well and it will work for any board size. You could potentially check right and left diagonal at once with nested loops.
This will work perfectly fine for your program! I hope so!
int arraySize = 8;
for(int i=0, j=0; i<arraySize && j<arraySize; i++, j++)
{
if((i == 0 && j == 0) || (i == arraySize - 1 && j == arraySize - 1))
{
continue;
}
else
{
int k = i;
int l = j;
//This Loop will check from central line (principal diagonal) to up right side (like slash sign / (representing direction))
for(k, l; k>0 && l < arraySize - 1; k--, l++)
{
//Here check your condition and increment to your variable. like:
if (p_connect_four[k][l] == p_connect_four[k - 1][l + 1] && p_connect_four[k][l] != '_' )
{
check_diagonalRight++;
}
}
//You can break the loop here if check_diagonalRight != k then break
k = i;
l = j;
//This Loop will check from central line (principal diagonal) to down left side (like slash sign / (representing direction))
for(k, l; k<arraySize - 1 && l > 0; k++, l--)
{
//Here check your condition and increment to your variable. like:
if (p_connect_four[k][l] == p_connect_four[k + 1][l - 1] && p_connect_four[k][l] != '_' )
{
check_diagonalRight++;
}
}
if(check_diagonalRight == i+j+1)
{
there_is_a_winner = true;
break;
}
}
}
I suggest to surround your board with extra special cases to avoid to check the bound.
To test each direction I suggest to use an array of offset to apply.
Following may help:
#include <vector>
using board_t = std::vector<std::vector<char>>;
constexpr const std::size_t MaxAlignment = 4;
enum Case {
Empty = '_',
X = 'X',
O = 'O',
Bound = '.'
};
enum class AlignmentResult { X, O, None };
// Create a new board, valid index would be [1; size] because of surrounding.
board_t new_board(std::size_t size)
{
// Create an empty board
board_t board(size + 2, std::vector<char>(size + 2, Case::Empty));
// Add special surround.
for (std::size_t i = 0; i != size + 2; ++i) {
board[0][i] = Case::Bound;
board[size + 1][i] = Case::Bound;
board[i][0] = Case::Bound;
board[i][size + 1] = Case::Bound;
}
return board_t;
}
// Test a winner from position in given direction.
AlignmentResult test(
const board_t& board,
std::size_t x, std::size_t y,
int offset_x, int offset_y)
{
if (board[x][y] == Case::Empty) {
return AlignmentResult::None;
}
for (std::size_t i = 1; i != MaxAlignment; ++i) {
// Following condition fails when going 'out of bound' thanks to Case::Bound,
// else you have also to check size...
if (board[x][y] != board[x + i * offset_x][y + i * offset_y]) {
return AlignmentResult::None;
}
}
if (board[x][y] == Case::X) {
return AlignmentResult::X;
} else {
return AlignmentResult::O;
}
}
// Test a winner on all the board
AlignmentResult test(const board_t& board)
{
// offset for direction. Use only 4 direction because of the symmetry.
const int offsets_x[] = {1, 1, 1, 0};
const int offsets_y[] = {-1, 0, 1, 1};
const std::size_t size = board.size() - 1;
for (std::size_t x = 1; x != size; ++x) {
for (std::size_t y = 1; y != size; ++y) {
for (std::size_t dir = 0; dir != 4; ++dir) { // for each directions
auto res = test(board, x, y, offsets_x[dir], offsets_y[y]);
if (res != AlignmentResult::None) {
return res;
}
}
}
}
return AlignmentResult::None;
}

Having trouble inserting into heap

Below is my C++ code for inserting into a heap. The value for k is inserted but it's inserted at the bottom. I expected it to heapify with that while loop.
void Insert(heap1* myHeap, int k)
{
(myHeap->size)++;
int i = myHeap->size;
while (i > 1 && myHeap->H[i/2].key < k)
{
myHeap->H[i].key = myHeap->H[i/2].key;
i = i/2;
}
myHeap->H[i].key = k;
}
I do have a heapify procedure that I tried to use for this before this attempt that I know works within my other heap procedures. I just can't get it to work within Insert so I went with the above route. Below is heapify just in case its useful:
void heapify(heap1* myHeap, int i)
{
int l = 2 * i;
int r = 2 * i + 1;
int largest;
if (l <= myHeap->size && myHeap->H[l].key > myHeap->H[i].key)
largest = l;
else
largest = i;
if (r <= myHeap->size && myHeap->H[r].key > myHeap->H[largest].key)
largest = r;
if (largest != i)
{
myHeap->H[i].key = myHeap->H[i].key + myHeap->H[largest].key;
myHeap->H[largest].key = myHeap->H[i].key - myHeap->H[largest].key;
myHeap->H[i].key = myHeap->H[i].key - myHeap->H[largest].key;
heapify(myHeap, largest);
}
}
If someone could lead me in the right direction on how to get it to restore its heap properties, I would largely appreciate it.
try using this code:
void insert(int heap[], int *n, int item){
(*n)++;
heap[*n] = item;
reheapify_upward(heap, *n);
}
void reheapify_upward(int heap[],int start){
int temp,parent;
if(start>1){
parent=start/2;
if(heap[parent]<heap[start]){
temp=heap[start];
heap[start]=heap[parent];
heap[parent]=temp;
reheapify_upward(heap,parent);
}
}
}

C++ Not Counting white beands

I need some help. I'm writing a code in C++ that will ultimately take a random string passed in, and it will do a break at every point in the string, and it will count the number of colors to the right and left of the break (r, b, and w). Here's the catch, the w can be either r or b when it breaks or when the strong passes it ultimately making it a hybrid. My problem is when the break is implemented and there is a w immediately to the left or right I can't get the program to go find the fist b or r. Can anyone help me?
#include <stdio.h>
#include "P2Library.h"
void doubleNecklace(char neck[], char doubleNeck[], int size);
int findMaxBeads(char neck2[], int size);
#define SIZE 7
void main(void)
{
char necklace[SIZE];
char necklace2[2 * SIZE];
int brk;
int maxBeads;
int leftI, rightI, leftCount = 0, rightCount=0, totalCount, maxCount = 0;
char leftColor, rightColor;
initNecklace(necklace, SIZE);
doubleNecklace(necklace, necklace2, SIZE);
maxBeads = findMaxBeads(necklace2, SIZE * 2);
checkAnswer(necklace, SIZE, maxBeads);
printf("The max number of beads is %d\n", maxBeads);
}
int findMaxBeads(char neck2[], int size)
{
int brk;
int maxBeads;
int leftI, rightI, leftCount = 0, rightCount=0, totalCount, maxCount = 0;
char leftColor, rightColor;
for(brk = 0; brk < 2 * SIZE - 1; brk++)
{
leftCount = rightCount = 0;
rightI = brk;
rightColor = neck2[rightI];
if(rightI == 'w')
{
while(rightI == 'w')
{
rightI++;
}
rightColor = neck2[rightI];
}
rightI = brk;
while(neck2[rightI] == rightColor || neck2[rightI] == 'w')
{
rightCount++;
rightI++;
}
if(brk > 0)
{
leftI = brk - 1;
leftColor = neck2[leftI];
if(leftI == 'w')
{
while(leftI == 'w')
{
leftI--;
}
leftColor = neck2[leftI];
}
leftI = brk - 1;
while(leftI >= 0 && neck2[leftI] == leftColor || neck2[leftI] == 'w')
{
leftCount++;
leftI--;
}
}
totalCount = leftCount + rightCount;
if(totalCount > maxCount)
{
maxCount = totalCount;
}
}
return maxCount;
}
void doubleNecklace(char neck[], char doubleNeck[], int size)
{
int i;
for(i = 0; i < size; i++)
{
doubleNeck[i] = neck[i];
doubleNeck[i+size] = neck[i];
}
}
I didn't study the code in detail, but something is not symmetric: in the for loop, the "left" code has an if but the "right" code doesn't. Maybe you should remove that -1 in the for condition and add it as an if for the "right" code:
for(brk = 0; brk < 2 * SIZE; brk++)
{
leftCount = rightCount = 0;
if (brk < 2 * SIZE - 1)
{
rightI = brk;
rightColor = neck2[rightI];
//...
}
if(brk > 0)
{
leftI = brk - 1;
leftColor = neck2[leftI];
//...
}
//...
Just guessing, though... :-/
Maybe you should even change those < for <=.